forked from hashicorp/levant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.go
53 lines (43 loc) · 1.14 KB
/
meta.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
package command
import (
"bufio"
"flag"
"io"
"github.com/jrasell/levant/helper"
"github.com/mitchellh/cli"
)
// FlagSetFlags is an enum to define what flags are present in the
// default FlagSet returned by Meta.FlagSet
type FlagSetFlags uint
// Consts which helps us track meta CLI falgs.
const (
FlagSetNone FlagSetFlags = 0
FlagSetBuildFilter FlagSetFlags = 1 << iota
FlagSetVars
)
// Meta contains the meta-options and functionality that nearly every
// Levant command inherits.
type Meta struct {
UI cli.Ui
// These are set by command-line flags
flagVars map[string]string
}
// FlagSet returns a FlagSet with the common flags that every
// command implements.
func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet {
f := flag.NewFlagSet(n, flag.ContinueOnError)
// FlagSetVars tells us what variables to use
if fs&FlagSetVars != 0 {
f.Var((*helper.Flag)(&m.flagVars), "var", "")
}
// Create an io.Writer that writes to our Ui properly for errors.
errR, errW := io.Pipe()
errScanner := bufio.NewScanner(errR)
go func() {
for errScanner.Scan() {
m.UI.Error(errScanner.Text())
}
}()
f.SetOutput(errW)
return f
}