...
1 package config
2
3 import (
4 "flag"
5 "fmt"
6 "os"
7
8 "github.com/peterbourgon/ff/v3"
9 )
10
11 type Config struct {
12 Github Github
13 Port int
14 }
15
16 type Github struct {
17 App App
18 Token string
19 APIv3URL string
20 }
21
22 type App struct {
23 ID int64 `split_words:"true"`
24 PrivateKey string `split_words:"true"`
25 }
26
27 func New() (*Config, error) {
28 cfg := &Config{
29 Github: Github{App: App{}, APIv3URL: "https://api.github.com/"},
30 Port: 8080,
31 }
32 fs := flag.NewFlagSet("github-exporter", flag.ContinueOnError)
33
34
35 fs.Int64Var(&cfg.Github.App.ID, "github-app-id", 0, "integration ID for github app")
36
37 fs.StringVar(&cfg.Github.App.PrivateKey, "github-app-private-key", "", "private key for github app")
38 fs.StringVar(&cfg.Github.Token, "github-token", "", "github token to use for authentication. mutually exclusive with app config")
39
40 fs.IntVar(&cfg.Port, "port", cfg.Port, "port to serve on")
41
42 if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarNoPrefix()); err != nil {
43 return nil, err
44 }
45
46 if err := cfg.Validate(); err != nil {
47 return nil, err
48 }
49
50 return cfg, nil
51 }
52
53 func (c *Config) Validate() error {
54 if c.Github.App == (App{}) && c.Github.Token == "" {
55 return fmt.Errorf("either github app credentials or token is required")
56 }
57
58 return nil
59 }
60
View as plain text