forked from plexdrive/plexdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
61 lines (52 loc) · 1.75 KB
/
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
. "github.com/claudetech/loggo/default"
)
// Config describes the basic configuration architecture
type Config struct {
ClientID string
ClientSecret string
}
// ReadConfig reads the configuration based on a filesystem path
func ReadConfig(configPath string) (*Config, error) {
configFile, err := ioutil.ReadFile(configPath)
if nil != err {
return nil, fmt.Errorf("Could not read config file in %v", configPath)
}
var config Config
json.Unmarshal(configFile, &config)
return &config, nil
}
// CreateConfig creates the configuration by requesting from stdin
func CreateConfig(configPath string) (*Config, error) {
fmt.Println("1. Please go to https://console.developers.google.com/")
fmt.Println("2. Create a new project")
fmt.Println("3. Go to library and activate the Google Drive API")
fmt.Println("4. Go to credentials and create an OAuth client ID")
fmt.Println("5. Set the application type to 'other'")
fmt.Println("6. Specify some name and click create")
fmt.Printf("7. Enter your generated client ID: ")
var config Config
if _, err := fmt.Scan(&config.ClientID); err != nil {
Log.Debugf("%v", err)
return nil, fmt.Errorf("Unable to read client id")
}
fmt.Printf("8. Enter your generated client secret: ")
if _, err := fmt.Scan(&config.ClientSecret); err != nil {
Log.Debugf("%v", err)
return nil, fmt.Errorf("Unable to read client secret")
}
configJSON, err := json.Marshal(&config)
if nil != err {
Log.Debugf("%v", err)
return nil, fmt.Errorf("Could not generate config.json content")
}
if err := ioutil.WriteFile(configPath, configJSON, 0766); nil != err {
Log.Debugf("%v", err)
return nil, fmt.Errorf("Could not generate config.json file")
}
return &config, nil
}