· 6 years ago · Oct 17, 2018, 01:44 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}