-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
112 lines (99 loc) · 2.38 KB
/
main.go
File metadata and controls
112 lines (99 loc) · 2.38 KB
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
package main
import (
"errors"
"fmt"
"github.com/cerence/Ark-cli/cmd"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"os"
)
const (
BackEndHost = "backend-host"
APIHost = "api-host"
)
func main() {
displayLogo()
app := &cli.App{
Name: Name,
Usage: Usage,
Version: Version,
Commands: []*cli.Command{
{
Name: "import",
Aliases: []string{"i"},
Usage: "Import new device",
Action: func(c *cli.Context) error {
backendHost := extractBackEndHost(c)
apiHost := extractAPIHost(c)
uniqueDeviceCode, err := extractFirstArg(c)
if err != nil {
return err
}
cmd.Info.Println(fmt.Sprintf("Unique device code is %s", uniqueDeviceCode))
return cmd.ImportNewDevice(apiHost, backendHost, uniqueDeviceCode)
},
},
{
Name: "unbind",
Aliases: []string{"u"},
Usage: "unbind device",
Action: func(c *cli.Context) error {
backendHost := extractBackEndHost(c)
uniqueDeviceCode, err := extractFirstArg(c)
if err != nil {
return err
}
cmd.Info.Println(fmt.Sprintf("Unique device code is %s", uniqueDeviceCode))
return cmd.UnbindDevice(backendHost, uniqueDeviceCode)
},
},
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: BackEndHost,
DefaultText: DefaultBackendHost,
Value: DefaultBackendHost,
Usage: "Ark cloud backend host",
Required: false,
},
&cli.StringFlag{
Name: APIHost,
DefaultText: DefaultAPIHost,
Value: DefaultAPIHost,
Usage: "Ark cloud api host",
Required: false,
},
},
}
err := app.Run(os.Args)
if err != nil {
cmd.Error.Fatal(err)
}
}
func extractBackEndHost(c *cli.Context) string {
host := c.String(BackEndHost)
cmd.Info.Println(fmt.Sprintf("Ark cloud backend server is %s", host))
return host
}
func extractAPIHost(c *cli.Context) string {
host := c.String(APIHost)
cmd.Info.Println(fmt.Sprintf("Ark cloud api server is %s", host))
return host
}
func extractFirstArg(c *cli.Context) (string, error) {
args := c.Args()
if args.Len() == 0 {
return "", errors.New("no args")
}
arg := args.First()
return arg, nil
}
func displayLogo() {
if len(os.Args) == 1 {
color.Cyan(logo, Version)
} else if len(os.Args) == 2 {
if os.Args[1] == "-h" || os.Args[1] == "--help" || os.Args[1] == "h" || os.Args[1] == "help" {
color.Cyan(logo, Version)
}
}
}