-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_.go
118 lines (102 loc) · 2.64 KB
/
utils_.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
package cmd
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
yaml "gopkg.in/yaml.v2"
"github.com/fatih/color"
"github.com/spf13/cobra"
"github.com/tunedmystic/konnect/engine"
"github.com/tunedmystic/konnect/proxy"
)
func getDefaultConfig() string {
return "./konnect.yml"
}
func getVersion() string {
version := "0.0.1"
return version
}
// Remove duplicate elements from a string slice.
// https://goo.gl/ttDAg2
func removeDuplicates(elements []string) []string {
encountered := map[string]bool{}
result := []string{}
for _, host := range elements {
if encountered[host] == false {
encountered[host] = true
result = append(result, host)
}
}
return result
}
// Resolve the config filename from cmd flags.
// Fallback to default filename.
// Validate that the file exists.
func resolveFilename(cmd *cobra.Command) string {
// Get config filename from flags.
filename, _ := cmd.Flags().GetString("filename")
wasProvided := true
// If filename is not specified, then set
// to default config filename.
if filename == "" {
wasProvided = false
filename = getDefaultConfig()
}
// Check if the filename exists.
if _, err := os.Stat(filename); err != nil {
// Could not find a config file.
if wasProvided == false {
err = errors.New("Could not find a " +
"konnect.yml configuration file " +
"in this directory")
} else {
// Could not find the config file that the user specified.
err = fmt.Errorf("Config %v does not exist", filename)
}
log.Fatal(err)
}
return filename
}
func makeDefaultConfig(filename string) {
// Make default proxylist and konnect engine.
proxyList := map[string]*proxy.SSHProxy{
"app": &proxy.SSHProxy{
User: "root",
Host: "127.0.0.1",
Port: 22,
Key: "/home/app/key",
},
"database": &proxy.SSHProxy{
User: "admin",
Host: "192.168.99.100",
Port: 89,
Key: "~/.ssh/id_rsa",
},
}
konnect := engine.New()
konnect.Hosts = proxyList
// Marshal konnect struct to a byte slice.
byteSlice, err := yaml.Marshal(konnect)
if err != nil {
log.Fatal(err)
}
// Make config header.
header := []byte{}
header = append(header, []byte(`# Autogenerated configuration file.`)...)
header = append(header, 10)
header = append(header, []byte(`# Define your hosts here and connect to them with konnect cli.`)...)
header = append(header, 10, 10)
// Make config body.
data := []byte{}
data = append(data, header...)
data = append(data, byteSlice...)
// Write byte slice to file.
if err = ioutil.WriteFile(filename, data, 0644); err != nil {
log.Fatal(err)
}
fmt.Println("Created configuration file at:")
c := color.New(color.FgCyan, color.Bold)
c.Printf("%v\n", filename)
}