-
Notifications
You must be signed in to change notification settings - Fork 4
/
configure.go
53 lines (39 loc) · 1.06 KB
/
configure.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
package main
import (
"bufio"
"fmt"
"os"
"syscall"
"github.com/Jeffail/gabs/v2"
"golang.org/x/term"
)
func runConfigure() {
conf := gabs.New()
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Scalr Hostname [ex: example.scalr.io]: ")
scanner.Scan()
conf.Set(scanner.Text(), "hostname")
fmt.Print("Scalr Token (not echoed!): ")
bytepw, _ := term.ReadPassword(int(syscall.Stdin))
conf.Set(string(bytepw), "token")
fmt.Print("\nDefault Scalr Account-ID [ex: acc-tq8cgt2hu6hpfuj]: ")
scanner.Scan()
value := scanner.Text()
if value != "" {
conf.Set(scanner.Text(), "account")
}
home, err := os.UserHomeDir()
checkErr(err)
home = home + "/.scalr/"
config := "scalr.conf"
if _, err := os.Stat(home); os.IsNotExist(err) {
os.MkdirAll(home, 0700)
}
//Create a empty file
file, err := os.OpenFile(home+config, os.O_RDWR|os.O_CREATE, 0600)
checkErr(err)
defer file.Close()
fmt.Println("\nConfiguration saved in " + home + config)
file.WriteString(conf.StringIndent("", " ") + "\n")
file.Sync()
}