-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (113 loc) · 2.64 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"os"
"time"
"github.com/fatih/color"
"github.com/rodaine/table"
)
func main() {
// Clear the screen
fmt.Print("\033[H\033[2J")
// CLI title
fmt.Printf("=== %s =====================\n\n", "Zebra password changer")
fmt.Printf("Env: %s \n", infoColor(appEnv))
//
// Password
//
// Read password from txt file
password, err := readFileContent(passwordFile)
if err != nil {
fmt.Println(err)
//os.Exit(0)
// Exit with key
exit()
}
fmt.Printf("New password: %s \n", infoColor(password))
// Password validation / 4 digit number
if err := validatePassword(password, 4); err != nil {
fmt.Println(err)
// Exit with key
exit()
}
//
// ZPL code
//
// Prepare the password change zpl code
zplCode := fmt.Sprintf("^XA^KP%s^JUS^XZ", password)
//fmt.Printf("ZPL code: %s \n\n", zplCode)
//
// IP
//
// Read ip's from txt and convert to slice, so that it can be looped
ipAddresses, err := fileToSlice(printerIpFile)
if err != nil {
fmt.Println(err)
// Exit with key
exit()
}
// Delete errors.txt file if exists
if err := deleteErrorFileIfExists(errorFile); err != nil {
fmt.Println("Error:", err)
// Handle the error
}
// Create errors.txt file
if err := createErrorFile(errorFile); err != nil {
fmt.Println(err)
// Exit with key
exit()
}
//
// Prepare the table
// https://github.com/rodaine/table
//
// Set table options
headerFmt := color.New(color.FgCyan, color.Bold, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()
// Set table header
fmt.Print("\n")
tbl := table.New("Ip", "Result")
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)
//
// Loop
//
for _, ip := range ipAddresses {
//fmt.Println(ip)
var resultStatus string
// Validate the IP address
err := validateIpAddress(ip)
if err != nil {
resultStatus = errorColor("INVALID IP")
} else {
// Change passwords
if appEnv == "test" {
resultStatus = infoColor("TEST")
} else { // if is not test, Send Data
// Send the password change request
err := sendDataOverTcp(ip, "9100", zplCode, time.Second*1)
if err == nil { // No error
resultStatus = successColor("OK")
} else {
resultStatus = errorColor("NET ERROR")
// Write ip addresses to the errors.txt file
if err := writeIPToErrorFile(ip, errorFile); err != nil {
fmt.Println(err)
// Handle the error
}
}
}
}
// Add results to the table
tbl.AddRow(ip, resultStatus)
}
// Print the table
tbl.Print()
// Exit with key
exit()
}
func exit() {
// Exit
fmt.Println("\n\nPress ENTER key for exit.")
fmt.Scanln()
os.Exit(0)
}