-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
69 lines (64 loc) · 1.77 KB
/
Copy pathoptions.go
File metadata and controls
69 lines (64 loc) · 1.77 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
package main
import (
"context"
"io/ioutil"
"log"
"net/http"
"time"
"gopkg.in/yaml.v2"
)
type args struct {
Root string `positional-arg-name:"path"`
}
type Options struct {
ConfigUrl string `short:"u" env:"CONFIG_URL" description:"URL with remote JSON config" default:"https://hsecode.com/stdlib/docs/linter.yaml"`
ConfigPath string `short:"c" env:"CONFIG_PATH" description:"path to local config that will be used instead of remote one"`
Args args `positional-args:"args"`
Verbose bool `short:"v" description:"print more errors"`
Sanitise bool `long:"sanitise" description:"remove dangerous code and overwrite files in-place"`
}
func (opts *Options) GetConfig() *Config {
var config Config
if opts.ConfigPath != "" {
content, err := ioutil.ReadFile(opts.ConfigPath)
if err != nil {
log.Fatalf("could not open %v: %v", opts.ConfigPath, err)
}
if err := yaml.Unmarshal(content, &config); err != nil {
log.Fatalf("could not parse %v: %v", opts.ConfigPath, err)
}
return &config
}
err := func() error {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", opts.ConfigUrl, nil)
if err != nil {
return err
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
//noinspection GoUnhandledErrorResult
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if err := yaml.Unmarshal(content, &config); err != nil {
return err
}
return nil
}()
if err != nil {
log.Printf("[WARN] could not read remote config")
if opts.Verbose {
log.Print(err)
}
log.Print("[WARN] using default config (may be incomplete)")
return &defaultConfig
}
return &config
}