-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitialize.go
123 lines (105 loc) · 3.02 KB
/
initialize.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
119
120
121
122
123
// Package initialize is an initialization tool for chienote.
// It creates required directories and helps your setting process.
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
"gopkg.in/yaml.v2"
"github.com/pkg/errors"
)
func initialize() error {
fmt.Println("===== chienote initialization =====")
panicIfConfigExists()
var cfg config
stdin := bufio.NewReader(os.Stdin)
if err := askClientKey(&cfg, stdin); err != nil {
return err
}
if err := askClientSecret(&cfg, stdin); err != nil {
return err
}
if err := askDeveloperToken(&cfg, stdin); err != nil {
return err
}
if err := askNotebookName(&cfg, stdin); err != nil {
return err
}
if err := askEnvironment(&cfg, stdin); err != nil {
return err
}
if err := saveConfiguration(&cfg); err != nil {
return err
}
fmt.Println("Initialization succeeded.")
fmt.Println("You can edit your configration file if you want: " + configFilePath)
return nil
}
func panicIfConfigExists() error {
if _, err := os.Stat(configFilePath); err == nil {
return errors.Wrapf(err, "Can't create %v. Check if %v doesn't exists.", configFilePath, configFilePath)
}
return nil
}
func askClientKey(cfg *config, stdin *bufio.Reader) error {
fmt.Print("Enter your client key:")
if line, err := readLineTrimmed(stdin); err == nil && line != "" {
cfg.ClientKey = line
} else {
return errors.Errorf("Can't read your client key")
}
return nil
}
func askClientSecret(cfg *config, stdin *bufio.Reader) error {
fmt.Print("Enter your client secret:")
if line, err := readLineTrimmed(stdin); err == nil && line != "" {
cfg.ClientSecret = line
} else {
return errors.Errorf("Can't read your client secret")
}
return nil
}
func askDeveloperToken(cfg *config, stdin *bufio.Reader) error {
fmt.Print("Enter your developer token:")
if line, err := readLineTrimmed(stdin); err == nil && line != "" {
cfg.DeveloperToken = line
} else {
return errors.Errorf("Can't read your developer token")
}
return nil
}
func askNotebookName(cfg *config, stdin *bufio.Reader) error {
fmt.Print("Enter your notebook name to sync:")
if line, err := readLineTrimmed(stdin); err == nil && line != "" {
cfg.NotebookName = line
} else {
return errors.Errorf("Can't read your notebook name")
}
return nil
}
func askEnvironment(cfg *config, stdin *bufio.Reader) error {
fmt.Print("Is it the sandbox?[y/n]:")
if line, err := readLineTrimmed(stdin); err == nil && (line == "y" || line == "n") {
cfg.Sandbox = line == "y"
} else {
return errors.Errorf("Can't read your environment information")
}
return nil
}
func readLineTrimmed(stdin *bufio.Reader) (line string, err error) {
line, err = stdin.ReadString('\n')
line = strings.Trim(line, "\n\r")
return
}
func saveConfiguration(cfg *config) error {
bytes, err := yaml.Marshal(*cfg)
if err != nil {
return errors.Errorf("Can't yamlize your configuration")
}
if err := ioutil.WriteFile(configFilePath, bytes, os.ModePerm); err != nil {
return errors.Wrapf(err, "Can't save your configuration to %v", configFilePath)
}
return nil
}