-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmain.go
119 lines (86 loc) · 1.96 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
package main
import (
"bufio"
"flag"
"fmt"
"os"
"strings"
"golang.org/x/crypto/bcrypt"
"gopkg.in/yaml.v3"
"github.com/kdudkov/goatak/pkg/model"
)
func read(fn string) []*model.Device {
dat, err := os.ReadFile(fn)
if err != nil {
return nil
}
users := make([]*model.Device, 0)
if err := yaml.Unmarshal(dat, &users); err != nil {
panic(err.Error())
}
return users
}
func write(fn string, users []*model.Device) error {
f, err := os.Create(fn)
if err != nil {
return err
}
defer f.Close()
enc := yaml.NewEncoder(f)
return enc.Encode(users)
}
func main() {
file := flag.String("file", "users.yml", "file")
user := flag.String("user", "", "user")
passwd := flag.String("password", "", "password")
scope := flag.String("scope", "", "scope")
users := read(*file)
flag.Parse()
if *user == "" {
fmt.Printf("%-20s %-15s %-8s %-12s %-8s %s\n", "Login", "Callsign", "Team", "Role", "Scope", "Read scope")
fmt.Println(strings.Repeat("-", 90))
for _, user := range users {
fmt.Printf("%-20s %-15s %-8s %-12s %-8s %s\n",
user.Login, user.Callsign, user.Team, user.Role, user.Scope, strings.Join(user.ReadScope, ","))
}
return
}
pass := *passwd
if pass == "" {
reader := bufio.NewReader(os.Stdin)
fmt.Print("password: ")
p1, _ := reader.ReadString('\n')
fmt.Print("repeat password: ")
p2, _ := reader.ReadString('\n')
if p1 != p2 {
fmt.Println("\npassword mismatch")
return
}
pass = strings.TrimRight(p1, "\n\r")
}
bpass, err := bcrypt.GenerateFromPassword([]byte(pass), 14)
if err != nil {
panic(err)
}
var found bool
for _, u := range users {
if u.Login == *user {
found = true
u.Password = string(bpass)
if *scope != "" {
u.Scope = *scope
}
break
}
}
if !found {
sc := *scope
if sc == "" {
sc = "test"
}
users = append(users, &model.Device{Login: *user, Password: string(bpass), Scope: sc})
}
if err := write(*file, users); err != nil {
fmt.Println(err.Error())
}
}