forked from phrase/phraseapp-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
158 lines (126 loc) · 3.04 KB
/
config.go
File metadata and controls
158 lines (126 loc) · 3.04 KB
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
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path"
"github.com/phrase/phraseapp-api-client/Godeps/_workspace/src/gopkg.in/yaml.v2"
"github.com/phrase/phraseapp-go/phraseapp"
)
const configName = ".phraseapp.yml"
const defaultDir = "./"
func ConfigDefaultCredentials() (*phraseapp.AuthCredentials, error) {
content, err := ConfigContent()
if err != nil {
content = "{}"
}
return parseCredentials(content)
}
func ConfigDefaultParams() (phraseapp.DefaultParams, error) {
content, err := ConfigContent()
if err != nil {
content = "{}"
}
return parseDefaults(content)
}
func ConfigCallArgs() (map[string]string, error) {
content, err := ConfigContent()
if err != nil {
content = "{}"
}
return parseCallArgs(content)
}
// Paths and content
func ConfigContent() (string, error) {
path, err := phraseConfigPath()
if err != nil {
return "", err
}
bytes, err := readFile(path)
if err != nil {
return "", err
}
return string(bytes), nil
}
func phraseConfigPath() (string, error) {
callerPath, err := os.Getwd()
if err != nil {
return "", err
}
possiblePath := path.Join(callerPath, configName)
if _, err := os.Stat(possiblePath); err == nil {
return possiblePath, nil
}
return defaultConfigDir()
}
func readFile(path string) ([]byte, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return ioutil.ReadAll(file)
}
func defaultConfigDir() (string, error) {
usr, err := user.Current()
if err != nil {
return "", nil
}
return path.Join(usr.HomeDir, defaultDir, configName), nil
}
// Parsing
type credentialConf struct {
Phraseapp struct {
AccessToken string `yaml:"access_token"`
Host string `yaml:"host"`
Debug bool `yaml:"verbose"`
Username string
TFA bool
}
}
func parseCredentials(yml string) (*phraseapp.AuthCredentials, error) {
var conf *credentialConf
if err := yaml.Unmarshal([]byte(yml), &conf); err != nil {
fmt.Println("Could not parse .phraseapp.yml")
return nil, err
}
phrase := conf.Phraseapp
credentials := &phraseapp.AuthCredentials{Token: phrase.AccessToken, Username: phrase.Username, TFA: phrase.TFA, Host: phrase.Host, Debug: phrase.Debug}
return credentials, nil
}
type defaultsConf struct {
Phraseapp struct {
Defaults phraseapp.DefaultParams
}
}
func parseDefaults(yml string) (phraseapp.DefaultParams, error) {
var conf *defaultsConf
err := yaml.Unmarshal([]byte(yml), &conf)
if err != nil {
fmt.Println("Could not parse .phraseapp.yml")
return nil, err
}
return conf.Phraseapp.Defaults, nil
}
type CallArgs struct {
Phraseapp struct {
AccessToken string `yaml:"access_token"`
ProjectId string `yaml:"project_id"`
Page int
PerPage int
}
}
func parseCallArgs(yml string) (map[string]string, error) {
var callArgs *CallArgs
err := yaml.Unmarshal([]byte(yml), &callArgs)
if err != nil {
return nil, err
}
m := make(map[string]string)
if callArgs != nil {
m["ProjectId"] = callArgs.Phraseapp.ProjectId
m["AccessToken"] = callArgs.Phraseapp.AccessToken
}
return m, nil
}