-
Notifications
You must be signed in to change notification settings - Fork 8
/
add.go
77 lines (64 loc) · 1.56 KB
/
add.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
package cmd
import (
"strings"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
func Add() *cli.Command {
return &cli.Command{
Name: "add",
Aliases: []string{"a"},
Usage: "Add an entry to the hostsfile",
Action: add,
ArgsUsage: "[IP] [HOST] ([HOST]...)",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "clean",
Aliases: []string{"c"},
Usage: "Clean the hostsfile after adding an entry. See clean command for more details",
},
&cli.BoolFlag{
Name: "dry-run",
Usage: "Dry run only, will output contents of the new hostsfile without writing the changes.",
},
},
}
}
func add(c *cli.Context) error {
args := c.Args()
if args.Len() < 2 {
logrus.Infof("adding a hostsfile entry requires an ip and a hostname.")
return nil
}
hf, err := loadHostsfile(c, false)
if err != nil {
return err
}
ip := args.Slice()[0]
uniqueHosts := map[string]bool{}
var hostEntries []string
for i := 1; i < args.Len(); i++ {
uniqueHosts[args.Slice()[i]] = true
}
for key := range uniqueHosts {
hostEntries = append(hostEntries, key)
}
err = hf.Add(ip, hostEntries...)
if err != nil {
return cli.Exit(err.Error(), 2)
}
if c.Bool("clean") {
hf.Clean()
}
if c.Bool("dry-run") {
logrus.Debugln("performing a dry run, writing output")
outputHostsfile(hf, true)
return debugFooter(c)
}
logrus.Debugln("flushing hosts file to disk")
if err := hf.Flush(); err != nil {
return cli.Exit(err.Error(), 2)
}
logrus.Infof("hosts entry added: %s %s\n", ip, strings.Join(hostEntries, " "))
return debugFooter(c)
}