-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
115 lines (102 loc) · 2.61 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
106
107
108
109
110
111
112
113
114
115
package main
import (
"os"
"path"
"regexp"
"strings"
"github.com/belgaied2/harvester-cli/cmd"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)
var VERSION = "dev"
func main() {
if err := mainErr(); err != nil {
logrus.Fatal(err)
}
}
func mainErr() error {
userHome, err := os.UserHomeDir()
if err != nil {
logrus.Warn("Not able to determine home folder of current user!")
}
app := cli.NewApp()
app.Name = "harvester"
app.Usage = "Harvester CLI to easily manage infrastructure"
// app.Before = func(ctx *cli.Context) error {
// if ctx.GlobalBool("debug") {
// logrus.SetLevel(logrus.DebugLevel)
// }
// return nil
// }
app.Version = VERSION
app.Authors = append(app.Authors, &cli.Author{
Name: "Mohamed Belgaied Hassine",
Email: "[email protected]"})
app.Flags = []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Usage: "Debug logging",
},
&cli.StringFlag{
Name: "harvester-config, hconf",
Usage: "Path to Harvester's config file",
EnvVars: []string{"HARVESTER_CONFIG"},
Value: path.Join(userHome, ".harvester", "config"),
},
&cli.StringFlag{
Name: "config, rconf",
Usage: "Path to Rancher's config file",
EnvVars: []string{"RANCHER_CONFIG"},
Value: path.Join(userHome, ".rancher"),
},
// cli.StringFlag{
// Name: "loglevel",
// Usage: "Defines the log level to be used, possible values are error, info, warn, debug and trace",
// EnvVar: "HARVESTER_LOG",
// Value: "info",
// },
}
app.Commands = []*cli.Command{
cmd.LoginCommand(),
cmd.ConfigCommand(),
cmd.VMCommand(),
cmd.ShellCommand(),
cmd.TemplateCommand(),
cmd.ImageCommand(),
cmd.KeypairCommand(),
cmd.ImportCommand(),
cmd.CompleteCommand(),
}
app.EnableBashCompletion = true
parsed, err := parseArgs(os.Args)
if err != nil {
logrus.Error(err)
os.Exit(1)
}
return app.Run(parsed)
}
var singleAlphaLetterRegxp = regexp.MustCompile("[a-zA-Z]")
func parseArgs(args []string) ([]string, error) {
result := []string{}
for _, arg := range args {
if strings.HasPrefix(arg, "-") && !strings.HasPrefix(arg, "--") && len(arg) > 1 {
for i, c := range arg[1:] {
if string(c) == "=" {
if i < 1 {
return nil, errors.New("invalid input with '-' and '=' flag")
}
result[len(result)-1] = result[len(result)-1] + arg[i+1:]
break
} else if singleAlphaLetterRegxp.MatchString(string(c)) {
result = append(result, "-"+string(c))
} else {
return nil, errors.Errorf("invalid input %v in flag", string(c))
}
}
} else {
result = append(result, arg)
}
}
return result, nil
}