This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconair.go
148 lines (122 loc) · 2.9 KB
/
conair.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"flag"
"fmt"
"os"
"os/user"
"text/tabwriter"
)
const (
cliName = "conair"
cliDescription = "conair is a command-line interface to systemd-nspawn containers. much like docker."
bridge = "nspawn0"
destination = "192.168.13.0/24"
home = "/var/lib/machines"
hub = "http://conair.teemow.com/images"
)
var (
out *tabwriter.Writer
globalFlagset = flag.NewFlagSet(cliName, flag.ExitOnError)
// top level commands
commands []*Command
// flags used by all commands
globalFlags = struct {
Debug bool
Version bool
}{}
projectVersion string
)
type stringSlice []string
func (s *stringSlice) String() string {
return fmt.Sprintf("%v", *s)
}
func (s *stringSlice) Set(value string) error {
*s = append(*s, value)
return nil
}
func init() {
user, err := user.Current()
if err != nil {
fmt.Fprintln(os.Stderr, "Can't find current user. Need to be root.")
os.Exit(1)
}
if user.Uid != "0" {
fmt.Fprintln(os.Stderr, "Please run conair as root.")
os.Exit(1)
}
globalFlagset.BoolVar(&globalFlags.Debug, "debug", false, "Print out more debug information to stderr")
globalFlagset.BoolVar(&globalFlags.Version, "version", false, "Print the version and exit")
}
type Command struct {
Name string // Name of the Command and the string to use to invoke it
Summary string // One-sentence summary of what the Command does
Usage string // Usage options/arguments
Description string // Detailed description of command
Flags flag.FlagSet // Set of flags associated with this command
Run func(args []string) int // Run a command with the given arguments, return exit status
}
func init() {
out = new(tabwriter.Writer)
out.Init(os.Stdout, 0, 8, 1, '\t', 0)
commands = []*Command{
cmdAttach,
cmdInit,
cmdDestroy,
cmdPs,
cmdImages,
cmdRun,
cmdStop,
cmdStart,
cmdRm,
cmdRmi,
cmdCommit,
cmdStatus,
cmdBuild,
cmdPull,
cmdBootstrap,
cmdInspect,
cmdIp,
cmdSnapshot,
cmdHelp,
cmdVersion,
}
}
func getAllFlags() (flags []*flag.Flag) {
return getFlags(globalFlagset)
}
func getFlags(flagset *flag.FlagSet) (flags []*flag.Flag) {
flags = make([]*flag.Flag, 0)
flagset.VisitAll(func(f *flag.Flag) {
flags = append(flags, f)
})
return
}
func main() {
globalFlagset.Parse(os.Args[1:])
var args = globalFlagset.Args()
if len(args) < 1 {
args = append(args, "help")
}
// deal specially with --version
if globalFlags.Version {
args[0] = "version"
}
var cmd *Command
// determine which Command should be run
for _, c := range commands {
if c.Name == args[0] {
cmd = c
if err := c.Flags.Parse(args[1:]); err != nil {
fmt.Println(err.Error())
os.Exit(2)
}
break
}
}
if cmd == nil {
fmt.Printf("%v: unknown subcommand: %q\n", cliName, args[0])
fmt.Printf("Run '%v help' for usage.\n", cliName)
os.Exit(2)
}
os.Exit(cmd.Run(cmd.Flags.Args()))
}