· 6 years ago · Nov 26, 2018, 02:10 PM
1package main
2
3import (
4 "flag"
5 "fmt"
6 "io/ioutil"
7 "net"
8 "strings"
9)
10
11var nic, ip, gateway, ns, filename string
12
13func main() {
14 flag.StringVar(&filename, "f", "xyz", "Enter the filename ")
15 flag.StringVar(&nic, "interface", "eth0", "Enter the Ethernet Card (for example : 'ens0N' or 'eth0') ")
16 flag.StringVar(&ip, "ip", "192.168.1.122", "Enter the IP address along with the DNS in the format xxx.xxx.x.xxx/yy")
17 flag.StringVar(&gateway, "gateway", "192.168.0.1", "Enter the gateway in the format xxx.xxx.x.x")
18 flag.StringVar(&ns, "DNS", "8.8.8.8,8.8.4.4", "Enter the nameservers seperated by comma(,) in the format x.x.x.x,y.y.y.y")
19 flag.Parse()
20 testInput := net.ParseIP(ip)
21 if testInput.To4() == nil {
22 fmt.Printf("%v is not a valid IPv4 addressn", testInput)
23 } else {
24 fmt.Printf("It is a valid IP addressn")
25
26 b, err := ioutil.ReadFile("01-network-manager-all.yaml") // just pass the file name
27 if err != nil {
28 fmt.Print(err)
29 }
30 str := string(b) // convert content to a 'string'
31
32 // Create replacer with pairs as arguments.
33 r := strings.NewReplacer("{interface}", nic,
34 "IPaddress", ip,
35 "{gateway}", gateway,
36 "DNS", ns)
37
38 // Replace all pairs.
39 result := r.Replace(str)
40 fmt.Println(result)
41 newContents := []byte(result)
42 err = ioutil.WriteFile("newtemplate.yaml", newContents, 0664)
43 if err != nil {
44 panic(err)
45 }
46 }
47}
48
49$ go run main.go --foo asdf --bar xxx --baz ccc
50map[--foo:asdf --bar:xxx --baz:ccc]
51jsandrew-Mac:osarg jsandrew$ cat main.go
52package main
53
54import (
55 "fmt"
56 "os"
57)
58
59func manyRandomArg() map[string]string {
60 rv := make(map[string]string)
61 for ix, x := range os.Args {
62 if x[:2] == "--" {
63 rv[x] = os.Args[ix+1]
64 }
65 }
66 return rv
67}
68
69func main() {
70 fmt.Printf("%vn", manyRandomArg())
71}
72
73package main
74
75import (
76 "fmt"
77 "io/ioutil"
78 "log"
79 "os"
80 "regexp"
81 "strings"
82)
83
84var key0, key1, key2, key3, key4, filename string
85var fileext = regexp.MustCompile(`([a-z]+).yaml`)
86
87func manyRandomArg() map[string]string {
88 rv := make(map[string]string)
89 for ix, x := range os.Args {
90 if x[:2] == "--" {
91 rv[x] = os.Args[ix+1]
92 }
93 }
94 return rv
95}
96
97func main() {
98 fmt.Printf("n%vn", manyRandomArg())
99 readargs()
100}
101func readargs() {
102
103 rv := manyRandomArg()
104 keys := make([]string, 0, len(rv))
105 for key, _ := range rv {
106 keys = append(keys, key)
107 }
108 // Convert map to slice of values.
109 values := []string{}
110 for _, value := range rv {
111 values = append(values, value)
112 }
113
114 for keys, values := range rv {
115 fmt.Printf("key[%s] value[%s]n", keys, values)
116 }
117 if fileext.MatchString(values[0]) {
118 fmt.Printf("Value %sn", values[0])
119 filename = values[0]
120 } else if fileext.MatchString(values[1]) {
121 fmt.Printf("Value %sn", values[1])
122 filename = values[1]
123 } else if fileext.MatchString(values[2]) {
124 fmt.Printf("Value %sn", values[2])
125 filename = values[2]
126 } else if fileext.MatchString(values[3]) {
127 fmt.Printf("Value %sn", values[3])
128 filename = values[3]
129 } else if fileext.MatchString(values[4]) {
130 fmt.Printf("Value %sn", values[4])
131 filename = values[4]
132 } else {
133 log.Fatal("index 4 fail")
134 os.Exit(1)
135 }
136
137 b, err := ioutil.ReadFile(filename) // just pass the file name
138 if err != nil {
139 fmt.Print(err)
140 }
141 str := string(b) // convert content to a 'string'
142 key0 = trimLeftChars(keys[0], 2)
143 key1 = trimLeftChars(keys[1], 2)
144 key2 = trimLeftChars(keys[2], 2)
145 key3 = trimLeftChars(keys[3], 2)
146 key4 = trimLeftChars(keys[4], 2)
147
148 // Create replacer with pairs as arguments.
149 r := strings.NewReplacer(key0, values[0], key1, values[1], key2, values[2], key3, values[3], key4, values[4])
150
151 // Replace all pairs.
152 result := r.Replace(str)
153 fmt.Println(result)
154
155 newContents := []byte(result)
156 err = ioutil.WriteFile("new3.yaml", newContents, 0664)
157 if err != nil {
158 panic(err)
159 }
160}
161func trimLeftChars(s string, n int) string {
162 m := 0
163 for i := range s {
164 if m >= n {
165 return s[i:]
166 }
167 m++
168 }
169 return s[:0]
170}