-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
105 lines (83 loc) · 1.72 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"github.com/urfave/cli/v2"
grpc_codes "google.golang.org/grpc/codes"
grpc_status "google.golang.org/grpc/status"
)
const kvmrundSock = "unix:@/run/kvmrund.sock"
var (
progname string
Info, Error *log.Logger
)
func init() {
progname = filepath.Base(os.Args[0])
switch progname {
case "ifup", "ifdown":
default:
progname = "vnetctl"
}
Info = log.New(os.Stdout, progname+": ", 0)
Error = log.New(os.Stderr, progname+": error: ", 0)
}
func main() {
//
// ifup/ifdown mode
//
switch progname {
case "ifup", "ifdown":
if err := ifupdownMain(); err != nil {
Error.Fatalln(err)
}
return
}
//
// Standard mode
//
app := cli.NewApp()
app.Name = "vnetctl"
app.Usage = "interface for management virtual networks"
app.HideHelpCommand = true
app.EnableBashCompletion = true
app.Flags = []cli.Flag{
&cli.BoolFlag{Name: "json", Aliases: []string{"j"}, Usage: "show output in the JSON format if possible"},
}
app.Commands = []*cli.Command{
{
Name: "version",
Usage: "print the version information",
Category: "Other",
Action: func(c *cli.Context) error {
fmt.Printf("v%s, (built %s)\n", "1", runtime.Version())
return nil
},
},
}
if err := app.Run(os.Args); err != nil {
exitWithError(err)
}
}
func exitWithError(err error) {
var exitcode int
var exitdesc string
if e, ok := grpc_status.FromError(err); ok {
switch e.Code() {
case grpc_codes.AlreadyExists, grpc_codes.NotFound:
exitcode = 2
case grpc_codes.Unimplemented:
exitcode = 3
default:
exitcode = 5
}
exitdesc = e.Message()
} else {
exitcode = 1
exitdesc = err.Error()
}
Error.Println(exitdesc)
os.Exit(exitcode)
}