forked from virtualzone/onedrive-uploader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive-config.go
147 lines (135 loc) · 3.41 KB
/
interactive-config.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/virtualzone/onedrive-uploader/sdk"
)
type InteractiveConfig struct {
TargetPath string
}
func (c *InteractiveConfig) readChar() rune {
reader := bufio.NewReader(os.Stdin)
char, _, err := reader.ReadRune()
if err != nil {
fmt.Printf("error reading from input: " + err.Error())
os.Exit(1)
}
return char
}
func (c *InteractiveConfig) readString() string {
reader := bufio.NewReader(os.Stdin)
s, _ := reader.ReadString('\n')
s = strings.TrimSuffix(s, "\n")
s = strings.TrimSuffix(s, "\r")
return s
}
func (c *InteractiveConfig) promptClientId(config *sdk.Config) {
for config.ClientID == "" {
fmt.Printf("OneDrive Client ID: ")
config.ClientID = c.readString()
}
}
func (c *InteractiveConfig) promptClientSecret(config *sdk.Config) {
for config.ClientSecret == "" {
fmt.Printf("OneDrive Client Secret: ")
config.ClientSecret = c.readString()
}
}
func (c *InteractiveConfig) promptScopes(config *sdk.Config) {
fmt.Println("Available scopes:")
fmt.Println("1) Default (Files.Read, Files.ReadWrite, Files.Read.All, Files.ReadWrite.All, offline_access)")
fmt.Println("2) App Root (Files.ReadWrite.AppFolder, offline_access)")
fmt.Println("3) Custom")
scope := ' '
for scope == ' ' {
fmt.Printf("Select Scopes [1]: ")
scope = c.readChar()
switch scope {
case '1', '\n':
config.Scopes = []string{
"Files.Read",
"Files.ReadWrite",
"Files.Read.All",
"Files.ReadWrite.All",
"offline_access",
}
case '2':
config.Scopes = []string{
"Files.ReadWrite.AppFolder",
"offline_access",
}
case '3':
fmt.Printf("Scopes: ")
splitFunc := func(r rune) bool {
return r == ',' || r == ' '
}
scopes := c.readString()
scopesArray := strings.FieldsFunc(scopes, splitFunc)
config.Scopes = make([]string, 0)
for _, scopeItem := range scopesArray {
scopeItem = strings.TrimSpace(scopeItem)
if scopeItem != "" {
config.Scopes = append(config.Scopes, scopeItem)
}
}
default:
scope = ' '
}
}
}
func (c *InteractiveConfig) promptRoot(config *sdk.Config) {
fmt.Println("Available Drive Roots:")
fmt.Println("1) Default (/drive/root)")
fmt.Println("2) App Root (/drive/special/approot)")
fmt.Println("3) Custom")
root := ' '
for root == ' ' {
fmt.Printf("Select Drive Root [1]: ")
root = c.readChar()
switch root {
case '1', '\n':
config.Root = "/drive/root"
case '2':
config.Root = "/drive/special/approot"
case '3':
fmt.Printf("Custom Drive Root: ")
config.Root = c.readString()
default:
root = ' '
}
}
}
func (c *InteractiveConfig) promptRedirectURL(config *sdk.Config) {
fmt.Printf("Redirect URL? [http://localhost:53682/] ")
config.RedirectURL = c.readString()
if config.RedirectURL == "" {
config.RedirectURL = "http://localhost:53682/"
}
}
func (c *InteractiveConfig) promptSave(config *sdk.Config) {
save := ""
for save == "" {
fmt.Printf("Save config? [" + c.TargetPath + "] ")
save = c.readString()
if save == "" {
save = c.TargetPath
}
config.ConfigFilePath = save
if err := config.Write(); err != nil {
logError("Could not write config: " + err.Error())
return
}
fmt.Printf("Config written to: %s\n", save)
}
}
func (c *InteractiveConfig) Run() {
config := &sdk.Config{}
c.promptClientId(config)
c.promptClientSecret(config)
c.promptScopes(config)
c.promptRoot(config)
c.promptRedirectURL(config)
c.promptSave(config)
}