-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
add.go
71 lines (59 loc) · 1.52 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
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/guumaster/hostctl/pkg/host"
)
// addFromFileCmd represents the fromFile command
var addFromFileCmd = &cobra.Command{
Use: "add",
Short: "Add content to a profile in your hosts file.",
Long: `
Reads from a file and set content to a profile in your hosts file.
If the profile already exists it will be added to it.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
profile, _ := cmd.Flags().GetString("profile")
if profile == "" {
return host.MissingProfileError
}
if profile == "default" {
return host.DefaultProfileError
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
src, _ := cmd.Flags().GetString("host-file")
from, _ := cmd.Flags().GetString("from")
profile, _ := cmd.Flags().GetString("profile")
var err error
if isPiped() {
fmt.Println("IS PIPED")
err = host.AddFromReader(os.Stdin, &host.AddFromFileOptions{
Dst: src,
Profile: profile,
Reset: false,
})
} else {
fmt.Println("FROM FILE")
err = host.AddFromFile(&host.AddFromFileOptions{
From: from,
Dst: src,
Profile: profile,
Reset: false,
})
}
if err != nil {
return err
}
return host.ListProfiles(src, &host.ListOptions{
Profile: profile,
})
},
}
func init() {
rootCmd.AddCommand(addFromFileCmd)
addFromFileCmd.Flags().StringP("from", "f", "", "file to read")
addFromFileCmd.AddCommand(addDomainsCmd)
addDomainsCmd.Flags().String("ip", "127.0.0.1", "domains ip")
}