-
Notifications
You must be signed in to change notification settings - Fork 2
/
sakuraio-cli.go
170 lines (147 loc) · 8.04 KB
/
sakuraio-cli.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package main
import (
"fmt"
"os"
"path"
"github.com/Sirupsen/logrus"
"github.com/mattn/go-colorable"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/sakuraio/sakuraio-cli/commands"
"github.com/sakuraio/sakuraio-cli/commands/service"
"github.com/sakuraio/sakuraio-cli/lib"
)
var (
_, commandName = path.Split(os.Args[0])
app = kingpin.New(commandName, "sakuraio client command")
appToken = app.Flag("api-token", "API Token").String()
appSecret = app.Flag("api-secret", "API Secret").String()
/////// auth
authCmd = app.Command("auth", "Authentication")
authConfigToken = authCmd.Arg("token", "API Token").String()
authConfigSecret = authCmd.Arg("secret", "API Secret").String()
/////// project
projectsCmd = app.Command("project", "Project management")
listProjectsCmd = projectsCmd.Command("list", "List of projects").Alias("ls")
showProjectsCmd = projectsCmd.Command("show", "Lookup projects")
showProjectIDs = showProjectsCmd.Arg("id", "Show projectID").Required().Strings()
addProjectCmd = projectsCmd.Command("add", "Add Project")
addProjectName = addProjectCmd.Arg("name", "project name").Required().String()
deleteProjectCmd = projectsCmd.Command("remove", "Remove Project").Alias("delete").Alias("rm")
deleteProjectForce = deleteProjectCmd.Flag("force", "Project force remove").Short('f').Bool()
deleteProjectID = deleteProjectCmd.Arg("ID", "Remove project ID").Required().String()
/////// module
modulesCmd = app.Command("module", "Modules managemet")
listModulesCmd = modulesCmd.Command("list", "List of modules").Alias("ls")
showModulesCmd = modulesCmd.Command("show", "Lookup modules")
showModuleIDs = showModulesCmd.Arg("ID", "Modlue ID list").Required().Strings()
addModuleCmd = modulesCmd.Command("add", "Add module")
addModuleID = addModuleCmd.Arg("id", "Register ID").Required().String()
addModulePW = addModuleCmd.Arg("password", "Register Password").Required().String()
addModuleProject = addModuleCmd.Arg("project-id", "Project ID").Required().Int()
addModuleName = addModuleCmd.Arg("name", "Module name").String()
/////// service
servicesCmd = app.Command("service", "Service management")
listServicesCmd = servicesCmd.Command("list", "List of services").Alias("ls")
showServiceCmd = servicesCmd.Command("show", "show service")
showServiceIDs = showServiceCmd.Arg("id", "Show projectID").Required().Strings()
deleteServiceCmd = servicesCmd.Command("remove", "Remove Project").Alias("delete").Alias("rm")
deleteServiceForce = deleteServiceCmd.Flag("force", "Force remove").Short('f').Bool()
deleteServiceID = deleteServiceCmd.Arg("ID", "Remove service ID").Required().String()
addServiceCmd = servicesCmd.Command("add", "Add Service")
addServiceType = addServiceCmd.Arg("type", "Service type").Required().String()
addServiceProject = addServiceCmd.Arg("project id", "Project ID").Required().Int()
addServiceOptions = addServiceCmd.Arg("option", "Service option").Strings()
)
/////// data store
var (
dataStoreCmd = servicesCmd.Command("datastore", "Data Store Service")
DataStoreChannelsCmd = dataStoreCmd.Command("channels", "Get channel data")
DataStoreMessagesCmd = dataStoreCmd.Command("messages", "Get message data")
)
var dataStoreChannelOption = service.DataStoreChannelOptions{
Module: DataStoreChannelsCmd.Flag("module", "Module ID").Short('m').Default("").String(),
Size: DataStoreChannelsCmd.Flag("size", "Total fetch count. When 0 is specified fetch unlimited").Short('s').Default("100").Int(),
Order: DataStoreChannelsCmd.Flag("order", "Order asc/desc").String(),
Token: DataStoreChannelsCmd.Flag("token", "Service Token").String(),
Cursor: DataStoreChannelsCmd.Flag("cursor", "Cursor").String(),
After: DataStoreChannelsCmd.Flag("after", "Datetime range from").String(),
Before: DataStoreChannelsCmd.Flag("before", "Datetime range to").String(),
Channel: DataStoreChannelsCmd.Flag("channel", "Channel").String(),
Project: DataStoreChannelsCmd.Flag("project", "Project ID").String(),
RawOutput: DataStoreChannelsCmd.Flag("raw", "Raw JSON output").Default("false").Bool(),
NoRecursive: DataStoreChannelsCmd.Flag("no-rec", "Un use recursive fetch").Default("false").Bool(),
MaxReq: DataStoreChannelsCmd.Flag("max-req", "Max recursive request count").Default("100").Int(),
BatchSize: DataStoreChannelsCmd.Flag("batch-size", "Fetch size per request").Default("100").Int(),
}
var dataStoreMessageOption = service.DataStoreMessagesOption{
Module: DataStoreMessagesCmd.Flag("module", "Module ID").Short('m').Default("").String(),
Size: DataStoreMessagesCmd.Flag("size", "Total fetch count. When 0 is specified fetch unlimited").Short('s').Default("100").Int(),
Order: DataStoreMessagesCmd.Flag("order", "Order asc/desc").String(),
Cursor: DataStoreMessagesCmd.Flag("cursor", "Cursor").String(),
After: DataStoreMessagesCmd.Flag("after", "Datetime range from").String(),
Before: DataStoreMessagesCmd.Flag("before", "Datetime range to").String(),
Project: DataStoreMessagesCmd.Flag("project", "Project ID").String(),
RawOutput: DataStoreMessagesCmd.Flag("raw", "Raw JSON output").Default("false").Bool(),
Token: DataStoreMessagesCmd.Flag("token", "Service Token").String(),
NoRecursive: DataStoreMessagesCmd.Flag("no-rec", "Un use recursive fetch").Default("false").Bool(),
MaxReq: DataStoreMessagesCmd.Flag("max-req", "Max recursive request count").Default("100").Int(),
BatchSize: DataStoreMessagesCmd.Flag("batch-size", "Fetch size per request").Default("100").Int(),
}
var (
websocketCmd = servicesCmd.Command("websocket", "Websocket Service").Alias("ws")
websocketListenCmd = websocketCmd.Command("listen", "Listen to Websocket")
)
var websocketListneOption = service.WebsocketListenOptions{
Project: websocketListenCmd.Flag("project", "Project ID").String(),
// RawOutput: websocketListenCmd.Flag("raw", "Raw JSON output").Default("false").Bool(),
Token: websocketListenCmd.Flag("token", "Service Token").String(),
}
/////// END Flags
func init() {
logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true})
logrus.SetOutput(colorable.NewColorableStdout())
}
func main() {
app.Version(commands.Version)
app.UsageTemplate(kingpin.CompactUsageTemplate)
app.VersionFlag.Action(func(c *kingpin.ParseContext) error {
fmt.Println("PreAction")
fmt.Println(*c)
return nil
})
parseResult := kingpin.MustParse(app.Parse(os.Args[1:]))
lib.OverrideSettings.APIToken = *appToken
lib.OverrideSettings.APISecret = *appSecret
switch parseResult {
case authCmd.FullCommand(): // Auth Command
commands.AuthConfigCommand(*authConfigToken, *authConfigSecret)
case listProjectsCmd.FullCommand(): // Project Command
commands.ListProjectsCommand()
case showProjectsCmd.FullCommand():
commands.ShowProjectsCommand(*showProjectIDs)
case addProjectCmd.FullCommand():
commands.AddProjectCommand(*addProjectName)
case deleteProjectCmd.FullCommand():
commands.DeleteProject(*deleteProjectForce, *deleteProjectID)
case listModulesCmd.FullCommand(): // Module Command
commands.ListModulesCommand()
case showModulesCmd.FullCommand():
commands.ShowModulesCommand(*showModuleIDs)
case addModuleCmd.FullCommand():
commands.AddModuleCommand(*addModuleID, *addModulePW, *addModuleProject, *addModuleName)
case listServicesCmd.FullCommand(): // Service Command
commands.ListServicesCommand()
case showServiceCmd.FullCommand():
commands.ShowServicesCommand(*showServiceIDs)
case deleteServiceCmd.FullCommand():
commands.DeleteServiceCommand(*deleteServiceForce, *deleteServiceID)
case addServiceCmd.FullCommand():
commands.AddServiceCommand(*addServiceType, *addServiceProject, *addServiceOptions)
case DataStoreChannelsCmd.FullCommand(): // Service Data Store Command
service.DataStoreChannelsCommand(dataStoreChannelOption)
case DataStoreMessagesCmd.FullCommand():
service.DataStoreMessagesCmd(dataStoreMessageOption)
case websocketListenCmd.FullCommand(): // Service Websocket Command
service.WebsocketListenCommand(websocketListneOption)
}
}