aboutsummaryrefslogtreecommitdiffstats
path: root/config/config.go
blob: 33451e2cf81105621faa50299019793e53bdeb21 (plain)
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
package config

import (
    "fmt"
    "io/ioutil"
    "encoding/json"
    "../util"
)

// Client ID and secrect for installed applications
const (
    ClientId = "367116221053-7n0vf5akeru7on6o2fjinrecpdoe99eg.apps.googleusercontent.com"
    ClientSecret = "1qsNodXNaWq1mQuBjUjmvhoO"
)

type Config struct {
    ClientId string
    ClientSecret string
}

func defaultConfig() *Config {
    return &Config{
        ClientId: ClientId,
        ClientSecret: ClientSecret,
    }
}

func promptUser() *Config {
    return &Config{
        ClientId: util.Prompt("Enter Client Id: "),
        ClientSecret: util.Prompt("Enter Client Secret: "),
    }
}

func load(fname string) (*Config, error) {
    data, err := ioutil.ReadFile(fname)
    if err != nil {
        return nil, err
    }
    config := &Config{}
    return config, json.Unmarshal(data, config)
}

func save(fname string, config *Config) error {
    data, err := json.MarshalIndent(config, "", "    ")
    if err != nil {
        return err
    }

    if err = util.Mkdir(fname); err != nil {
        return err
    }
    return ioutil.WriteFile(fname, data, 0600)
}

func Load(fname string, advancedUser bool) *Config {
    config, err := load(fname)
    if err != nil {
        // Unable to read existing config, lets start from scracth
        // Get config from user input for advanced users, or just use default settings
        if advancedUser {
            config = promptUser()
        } else {
            config = defaultConfig()
        }
        
        // Save config to file
        err := save(fname, config)
        if err != nil {
            fmt.Printf("Failed to save config (%s)\n", err)
        }
    }
    return config
}