-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
190 lines (160 loc) · 3.88 KB
/
main.go
File metadata and controls
190 lines (160 loc) · 3.88 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"crypto/rand"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/wzshiming/ctc"
)
func logError(err error) {
fmt.Print(ctc.ForegroundRed, err, ctc.Reset, "\n")
}
func main() {
rootCmd := &cobra.Command{
Use: "dev",
Short: "dev is a collection of utilities that might come handy when developing a software",
}
// namespaces
randomGen := &cobra.Command{
Use: "rand",
Short: "Random related generators",
}
rootCmd.AddCommand(randomGen)
golang := &cobra.Command{
Use: "go",
Short: "Go language related generators",
}
rootCmd.AddCommand(golang)
// go namespace
golangMain := &cobra.Command{
Use: "main",
Short: "Generate initial main()",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(golangMainTpl)
},
}
golang.AddCommand(golangMain)
// rand namespace
uuid4 := &cobra.Command{
Use: "uuid4",
Short: "Generates UUID v4",
Run: func(cmd *cobra.Command, args []string) {
uuid, err := uuid.NewUUID()
if err != nil {
logError(err)
os.Exit(1)
}
fmt.Println(uuid.String())
},
}
randomGen.AddCommand(uuid4)
randomBytes := &cobra.Command{
Use: "bytes",
Short: "Generates bytes encoded with base64",
Run: func(cmd *cobra.Command, args []string) {
size, err := cmd.Flags().GetUint32("size")
if err != nil {
logError(err)
}
b := make([]byte, size)
_, err = rand.Read(b)
if err != nil {
logError(err)
}
encoded := base64.StdEncoding.EncodeToString(b)
fmt.Println(encoded)
},
}
randomBytes.Flags().Uint32("size", 64, "specify bytes length")
randomGen.AddCommand(randomBytes)
// ssh namespace
sshTools := &cobra.Command{
Use: "ssh",
Short: "SSH tools",
}
rootCmd.AddCommand(sshTools)
sshKnownHosts := &cobra.Command{
Use: "known-hosts",
Short: "known_hosts",
}
sshTools.AddCommand(sshKnownHosts)
sshKnownHostsList := &cobra.Command{
Use: "diff",
Short: "Show diff between ~/.ssh/known_hosts and ~/.ssh/known_hosts.db",
Run: func(cmd *cobra.Command, args []string) {
trusted := sshKnownHostsRead(".db")
active := sshKnownHostsRead("")
// check diff
temp := make([]string, 0)
for _, a := range active {
found := false
for _, t := range trusted {
if t == a {
found = true
break
}
}
if !found {
temp = append(temp, a)
}
}
if len(temp) > 0 {
fmt.Print(ctc.ForegroundYellow, "New hosts (", len(temp), ")", ctc.Reset, "\n")
for _, s := range temp {
fmt.Println(s)
}
} else {
fmt.Print(ctc.ForegroundGreen, "No new hosts\n", ctc.Reset)
}
},
}
sshKnownHosts.AddCommand(sshKnownHostsList)
sshKnownHostsReset := &cobra.Command{
Use: "reset",
Short: "Replace ~/.ssh/known_hosts with ~/.ssh/known_hosts.db",
Run: func(cmd *cobra.Command, args []string) {
trusted := sshKnownHostsRead(".db")
sshKnownHostsWrite("", trusted)
fmt.Printf("%s%d hosts written%s\n", ctc.ForegroundGreen, len(trusted), ctc.Reset)
},
}
sshKnownHosts.AddCommand(sshKnownHostsReset)
//
if err := rootCmd.Execute(); err != nil {
logError(err)
os.Exit(1)
}
}
const golangMainTpl = `package main
import "fmt"
func main() {
fmt.Println("Hello")
}`
func sshKnownHostsRead(suffix string) []string {
res := make([]string, 0)
if f, err := os.Open(fmt.Sprint(os.Getenv("HOME"), "/.ssh/known_hosts", suffix)); err == nil {
defer f.Close()
b, err := ioutil.ReadAll(f)
if err == nil {
for _, s := range strings.Split(string(b), "\n") {
if strings.TrimSpace(s) != "" {
res = append(res, s)
}
}
}
}
return res
}
func sshKnownHostsWrite(suffix string, lines []string) {
if f, err := os.OpenFile(fmt.Sprint(os.Getenv("HOME"), "/.ssh/known_hosts", suffix), os.O_WRONLY|os.O_APPEND, 0644); err == nil {
defer f.Close()
f.Truncate(0)
for _, line := range lines {
fmt.Fprintln(f, line)
}
}
}