-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
92 lines (85 loc) · 1.74 KB
/
config.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package config
import (
"encoding/json"
"fmt"
"os"
"github.com/edgi-io/edgi-os/pkg/cc"
"github.com/edgi-io/edgi-os/pkg/config"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
var (
initrd = false
bootPhase = false
installPhase = false
dump = false
dumpJSON = false
)
// Command `config`
func Command() cli.Command {
return cli.Command{
Name: "config",
Usage: "configure EDGI",
ShortName: "cfg",
// Aliases: []string{
// "ccapply",
// },
Flags: []cli.Flag{
cli.BoolFlag{
Name: "initrd",
Destination: &initrd,
Usage: "Run initrd stage",
},
cli.BoolFlag{
Name: "boot",
Destination: &bootPhase,
Usage: "Run boot stage",
},
cli.BoolFlag{
Name: "install",
Destination: &installPhase,
Usage: "Run install stage",
},
cli.BoolFlag{
Name: "dump",
Destination: &dump,
Usage: "Print current configuration",
},
cli.BoolFlag{
Name: "dump-json",
Destination: &dumpJSON,
Usage: "Print current configuration in json",
},
},
Before: func(c *cli.Context) error {
if os.Getuid() != 0 {
return fmt.Errorf("must be run as root")
}
return nil
},
Action: func(*cli.Context) {
if err := Main(); err != nil {
logrus.Error(err)
}
},
}
}
// Main `config`
func Main() error {
cfg, err := config.ReadConfig()
if err != nil {
return err
}
if initrd {
return cc.InitApply(&cfg)
} else if bootPhase {
return cc.BootApply(&cfg)
} else if installPhase {
return cc.InstallApply(&cfg)
} else if dump {
return config.Write(cfg, os.Stdout)
} else if dumpJSON {
return json.NewEncoder(os.Stdout).Encode(&cfg)
}
return cc.RunApply(&cfg)
}