-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
80 lines (65 loc) · 1.66 KB
/
config.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
71
72
73
74
75
76
77
78
79
80
// Copyright 2020 murosan. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gollect
import (
"errors"
"io"
"os"
"strings"
"github.com/atotto/clipboard"
"gopkg.in/yaml.v3"
)
// Config is a configuration.
type Config struct {
// path to main.go or glob of main package files
InputFile string `yaml:"inputFile"`
// list of output paths
// filepath, 'stdout' or 'clipboard' are available
OutputPaths []string `yaml:"outputPaths"`
// package path prefixes treat as same as builtin packages.
ThirdPartyPackagePathPrefixes []string `yaml:"thirdPartyPackagePathPrefixes"`
output io.Writer // used by test
}
func DefaultConfig() *Config {
return &Config{
InputFile: "main.go",
OutputPaths: []string{"stdout"},
ThirdPartyPackagePathPrefixes: []string{
"golang.org/x/exp",
"github.com/emirpasic/gods",
"github.com/liyue201/gostl",
"gonum.org/v1/gonum",
},
}
}
// LoadConfig loads config from yaml file.
func LoadConfig(path string) *Config {
if path == "" {
return DefaultConfig()
}
bytes, err := os.ReadFile(path)
if err != nil {
panic(err)
}
return UnmarshalConfig(bytes)
}
func UnmarshalConfig(b []byte) *Config {
c := *DefaultConfig()
if err := yaml.Unmarshal(b, &c); err != nil {
panic(err)
}
return &c
}
// Validate validates configuration.
func (c *Config) Validate() error {
if c.InputFile == "" {
return errors.New("input file is empty")
}
for _, out := range c.OutputPaths {
if strings.ToLower(out) == "clipboard" && clipboard.Unsupported {
return errors.New("no clipboard option provided for your operating system")
}
}
return nil
}