-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
70 lines (55 loc) · 1.33 KB
/
util.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/bloveless/tweetgo"
)
type config struct {
OAuthConsumerKey string `json:"oauth_consumer_key"`
OAuthConsumerSecret string `json:"oauth_consumer_secret"`
OAuthAccessToken string `json:"oauth_access_token"`
OAuthAccessTokenSecret string `json:"oauth_access_token_secret"`
}
func loadConfig() (config, error) {
wd, _ := os.Getwd()
fmt.Printf("Loading config from %s/config.json\n", wd)
configBytes, err := ioutil.ReadFile(wd + "/config.json")
if err != nil {
return config{}, err
}
c := config{}
err = json.Unmarshal(configBytes, &c)
if err != nil {
return config{}, err
}
fmt.Printf("Config loaded successfully: %+v\n", c)
return c, nil
}
func saveConfig(c config) error {
wd, _ := os.Getwd()
bytes, err := json.Marshal(c)
if err != nil {
return err
}
fmt.Printf("Saving new config to %s/config.json\n", wd)
f, err := os.OpenFile(wd+"/config.json", os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
_, err = f.Write(bytes)
if err != nil {
return err
}
fmt.Printf("Successfully wrote new config")
return nil
}
func getTwitterClient(c config) tweetgo.Client {
tc := tweetgo.NewClient(
c.OAuthConsumerKey,
c.OAuthConsumerSecret,
)
tc.SetAccessKeys(c.OAuthAccessToken, c.OAuthAccessTokenSecret)
return tc
}