-
Notifications
You must be signed in to change notification settings - Fork 2
/
file-to-machineconfig.go
62 lines (49 loc) · 2.17 KB
/
file-to-machineconfig.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
package main
import (
"flag"
"fmt"
"os"
"runtime"
"github.com/e-minguez/file-to-machineconfig/pkg/converter"
)
func printUsage() {
fmt.Printf("Usage: %s --file /local/path/to/my/file.txt [options]\n", os.Args[0])
fmt.Println("Options:")
flag.PrintDefaults()
fmt.Printf("Example:\n%s --file /local/path/to/my/file.txt --remote /path/to/remote/file.txt --plain --label \"machineconfiguration.openshift.io/role: master\",\"example.com/foo: bar\"\n", os.Args[0])
os.Exit(1)
}
func main() {
data := converter.Parameters{}
// https://coreos.com/ignition/docs/latest/configuration-v2_2.html
flag.StringVar(&data.LocalPath, "file", "", "The path to the local file [Required]")
flag.StringVar(&data.RemotePath, "remote", "", "The absolute path to the remote file [Required if running on Windows]")
flag.StringVar(&data.Name, "name", "", "MachineConfig object name [Required if running on Windows]")
flag.StringVar(&data.Labels, "labels", "", "MachineConfig metadata labels (separted by ,)")
flag.StringVar(&data.User, "user", "", "The user name of the owner")
flag.StringVar(&data.Group, "group", "", "The group name of the owner")
flag.StringVar(&data.Filesystem, "filesystem", "", "The internal identifier of the filesystem in which to write the file")
flag.StringVar(&data.APIVer, "apiversion", "", "MachineConfig API version")
flag.StringVar(&data.IgnitionVer, "ignitionversion", "", "Ignition version")
flag.IntVar(&data.Mode, "mode", 0, "File's permission mode in octal")
flag.BoolVar(&data.Yaml, "yaml", false, "Use yaml output instead JSON (false by default)")
flag.Parse()
// if user does not supply flags, print usage
if flag.NFlag() == 0 || data.LocalPath == "" {
printUsage()
}
if runtime.GOOS == "windows" && (data.LocalPath == "" || data.RemotePath == "" || data.Name == "") {
printUsage()
}
// Some sanity checks/normalization
converter.CheckParameters(&data)
// Fill the machine-config struct
mc := converter.NewMachineConfig(data)
// Convert and print the machine-config struct to json or yaml
switch {
case data.Yaml == true:
fmt.Println(converter.MachineConfigOutput(mc, "yaml"))
default:
fmt.Println(converter.MachineConfigOutput(mc, "json"))
}
}