-
Notifications
You must be signed in to change notification settings - Fork 0
/
save.go
60 lines (52 loc) · 1.35 KB
/
save.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
package ssh
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"log"
"os"
"path/filepath"
)
func init() {
Ssh.AddCommand(New)
}
var (
New = &cobra.Command{
Use: "save [profile]",
Args: cobra.ExactArgs(1),
Short: "Save a new profile",
Long: "Save a new profile",
Run: func(cmd *cobra.Command, args []string) {
name := args[0]
if err := viper.ReadInConfig(); err == nil {
if debug {
log.Println("Successfully read in existing configuration file: ", viper.ConfigFileUsed())
}
} else {
log.Fatalln("Error reading viper config: ", err)
}
// Check if file exists
if h, err := os.UserHomeDir(); err == nil {
config_file := filepath.Join(h, ".ssh", "config")
if debug {
log.Printf("Checking SSH Config at: %s\n", config_file)
}
if _, err := os.Stat(config_file); err == nil {
log.Printf("File exists, creating SSH profile entry\n")
if data, err := ioutil.ReadFile(config_file); err == nil {
viper.Set(name, string(data))
if err := viper.WriteConfig(); err == nil {
log.Println("Successfully wrote new profile!")
} else {
log.Fatalln("Error writing to profiler: ", err)
}
} else {
log.Fatalln("Error reading ssh config: ", err)
}
}
} else {
log.Fatalln("Error finding ssh directory: ", err)
}
},
}
)