@@ -14,25 +14,75 @@ import (
14
14
"syscall"
15
15
"time"
16
16
17
+ "github.com/machinebox/graphql"
17
18
"gopkg.in/yaml.v2"
19
+ )
18
20
19
- "github.com/machinebox/graphql"
21
+ const (
22
+ defaultOWMAPIEndpoint = "https://api.openweathermap.org/data/2.5/weather?appid={api-key}&units=metric"
23
+ defaultGitHubAPIEndpoint = "https://api.github.com/graphql"
24
+ defaultGitHubClientID = "github/weather"
20
25
)
21
26
22
27
type Config struct {
23
- ExpirationTime uint8 `yaml:"expirationTime "`
24
- Github struct {
25
- ClientID string `yaml:"clientID "`
28
+ ExpirationTime uint8 `yaml:"expiration_time "`
29
+ GitHub struct {
30
+ ClientID string `yaml:"client_id "`
26
31
Endpoint string `yaml:"endpoint"`
27
32
Token string `yaml:"token"`
28
33
} `yaml:"github"`
29
- Owm struct {
34
+ OWM struct {
30
35
ApiKey string `yaml:"api_key"`
31
36
Endpoint string `yaml:"endpoint"`
32
37
Query string `yaml:"query"`
33
38
} `yaml:"owm"`
34
39
}
35
40
41
+ func ConfigFromFile (configPath string ) (Config , error ) {
42
+ f , err := os .Open (configPath )
43
+ if err != nil {
44
+ return Config {}, fmt .Errorf ("error opening configuration file %q: %v" , configPath , err )
45
+ }
46
+ defer f .Close ()
47
+
48
+ var cfg Config
49
+ if err := yaml .NewDecoder (f ).Decode (& cfg ); err != nil {
50
+ return cfg , fmt .Errorf ("error parsing configuration file %q: %v" , configPath , err )
51
+ }
52
+
53
+ if cfg .OWM .Endpoint == "" {
54
+ cfg .OWM .Endpoint = defaultOWMAPIEndpoint
55
+ }
56
+
57
+ if cfg .GitHub .Endpoint == "" {
58
+ cfg .GitHub .Endpoint = defaultGitHubAPIEndpoint
59
+ }
60
+
61
+ if cfg .GitHub .ClientID == "" {
62
+ cfg .GitHub .ClientID = defaultGitHubClientID
63
+ }
64
+
65
+ if cfg .ExpirationTime == 0 {
66
+ cfg .ExpirationTime = 30
67
+ }
68
+
69
+ if err := validateConfig (cfg ); err != nil {
70
+ return cfg , fmt .Errorf ("error validating configuration file: %v" , err )
71
+ }
72
+
73
+ return cfg , nil
74
+ }
75
+
76
+ func validateConfig (cfg Config ) error {
77
+ if cfg .OWM .ApiKey == "" {
78
+ return fmt .Errorf ("owm api key is empty" )
79
+ }
80
+ if cfg .GitHub .Token == "" {
81
+ return fmt .Errorf ("github api token is empty" )
82
+ }
83
+ return nil
84
+ }
85
+
36
86
func main () {
37
87
ctx , cancel := context .WithCancel (context .Background ())
38
88
defer cancel ()
@@ -64,44 +114,28 @@ func run(ctx context.Context, args []string) error {
64
114
return err
65
115
}
66
116
67
- f , err := os . Open (configPath )
117
+ cfg , err := ConfigFromFile (configPath )
68
118
if err != nil {
69
- return fmt .Errorf ("error opening configuration file: %v" , err )
70
- }
71
- defer f .Close ()
72
-
73
- var cfg Config
74
- dec := yaml .NewDecoder (f )
75
- err = dec .Decode (& cfg )
76
- if err != nil {
77
- return fmt .Errorf ("error parsing configuration file: %v" , err )
78
- }
79
-
80
- if cfg .Owm .ApiKey == "" || cfg .Github .Token == "" {
81
- return fmt .Errorf ("no API credentials passed: OpenWeather %q, GitHub %q" , cfg .Owm .ApiKey , cfg .Github .Token )
82
- }
83
-
84
- if cfg .ExpirationTime == 0 {
85
- cfg .ExpirationTime = 30
119
+ return err
86
120
}
87
121
88
- owm := NewOWMClient (cfg .Owm .Endpoint , cfg .Owm .ApiKey )
89
- gh := NewGitHubClient (cfg .Github .Endpoint , cfg .Github .Token )
122
+ owm := NewOWMClient (cfg .OWM .Endpoint , cfg .OWM .ApiKey )
123
+ gh := NewGitHubClient (cfg .GitHub .Endpoint , cfg .GitHub .Token )
90
124
if debug {
91
125
gh .client .Log = func (s string ) {
92
126
log .Println (s )
93
127
}
94
128
}
95
129
96
- wr , err := owm .Weather (ctx , cfg .Owm .Query )
130
+ wr , err := owm .Weather (ctx , cfg .OWM .Query )
97
131
if err != nil {
98
132
return err
99
133
}
100
134
101
135
log .Printf ("got owm response: %+v\n " , wr )
102
136
103
137
status := ChangeUserStatusInput {
104
- ClientMutationID : cfg .Github .ClientID ,
138
+ ClientMutationID : cfg .GitHub .ClientID ,
105
139
Emoji : wr .Emoji (),
106
140
Message : wr .ShortString (),
107
141
ExpiresAt : time .Now ().UTC ().Add (time .Duration (cfg .ExpirationTime ) * time .Minute ),
0 commit comments