/
load.go
42 lines (39 loc) · 928 Bytes
/
load.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
package ssh
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"log"
"os"
"path/filepath"
)
func init() {
Ssh.AddCommand(Load)
}
var (
Load = &cobra.Command{
Use: "load [profile name]",
Args: cobra.ExactArgs(1),
Short: "Write existing profile to current ssh configuration",
Long: "Write existing profile to current ssh configuration",
Run: func(cmd *cobra.Command, args []string) {
name := args[0]
if err := viper.ReadInConfig(); err == nil {
if h, err := os.UserHomeDir(); err == nil {
config_file := filepath.Join(h, ".ssh", "config")
profile_contents := viper.GetString(name)
if err := ioutil.WriteFile(config_file, []byte(profile_contents), 500); err == nil {
fmt.Println("Successfully loaded new profile!")
} else {
log.Fatalln(err)
}
} else {
log.Fatalln(err)
}
} else {
log.Fatalln(err)
}
},
}
)