summaryrefslogtreecommitdiff
path: root/srv/cfg/cfg.go
blob: 685248ca6075a0c572d4a4fd49c275d56214bd42 (plain)
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
// Package cfg implements a simple wrapper around go's flag package, in order to
// implement initialization hooks.
package cfg

import (
	"context"
	"flag"
	"os"
)

// Cfger is a component which can be used with Cfg to setup its initialization.
type Cfger interface {
	SetupCfg(*Cfg)
}

// Cfg is a wrapper around the stdlib's FlagSet and a set of initialization
// hooks.
type Cfg struct {
	*flag.FlagSet

	hooks []func(ctx context.Context) error
	args  []string
}

// New initializes and returns a new instance of *Cfg.
func New() *Cfg {
	return &Cfg{
		FlagSet: flag.NewFlagSet("", flag.ExitOnError),
		args:    os.Args[1:],
	}
}

// OnInit appends the given callback to the sequence of hooks which will run on
// a call to Init.
func (c *Cfg) OnInit(cb func(context.Context) error) {
	c.hooks = append(c.hooks, cb)
}

// Init runs all hooks registered using OnInit, in the same order OnInit was
// called. If one returns an error that error is returned and no further hooks
// are run.
func (c *Cfg) Init(ctx context.Context) error {
	if err := c.FlagSet.Parse(c.args); err != nil {
		return err
	}

	for _, h := range c.hooks {
		if err := h(ctx); err != nil {
			return err
		}
	}

	return nil
}

// SubCmd should be called _after_ Init. Init will have consumed all arguments
// up until the first non-flag argument. This non-flag argument is a
// sub-command, and is returned by this method. This method also resets Cfg's
// internal state so that new options can be added to it.
//
// If there is no sub-command following the initial set of flags then this will
// return empty string.
func (c *Cfg) SubCmd() string {
	c.args = c.FlagSet.Args()
	if len(c.args) == 0 {
		return ""
	}

	subCmd := c.args[0]

	c.FlagSet = flag.NewFlagSet(subCmd, flag.ExitOnError)
	c.hooks = nil
	c.args = c.args[1:]

	return subCmd
}