/
util.go
56 lines (45 loc) · 1.13 KB
/
util.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
package check
import (
"flag"
"fmt"
"os"
// common resources shared between commands
"github.com/leprechau/ipman/common"
)
// setupFlags initializes the instance configuration
func (c *Command) setupFlags(args []string) error {
var cmdFlags *flag.FlagSet // instance flagset
var err error
// init config if needed
if c.config == nil {
c.config = new(config)
}
// init flagset
cmdFlags = flag.NewFlagSet("check", flag.ContinueOnError)
cmdFlags.Usage = func() { fmt.Fprint(os.Stdout, c.Help()); os.Exit(0) }
// declare flags
cmdFlags.BoolVar(&c.config.v4, "4", false,
"Check IPv4")
cmdFlags.BoolVar(&c.config.v6, "6", false,
"Check IPv6")
cmdFlags.StringVar(&c.config.ipbe, "ipbe", "ipify",
"IP lookup backend")
// parse flags and ignore error
if err = cmdFlags.Parse(args); err != nil {
return nil
}
// check for remaining garbage
if cmdFlags.NArg() > 0 {
return common.ErrUnknownArg
}
// default to v4 if not specified
if !c.config.v4 && !c.config.v6 {
c.config.v4 = true
}
// init ip backend
if c.ip, err = common.GetIPBackend(c.config.ipbe); err != nil {
return err
}
// all okay
return nil
}