This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
186 lines (181 loc) · 18.9 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"bytes"
"encoding/json"
"os"
"github.com/dimiro1/banner"
"github.com/fcfcqloow/go-advance/ftil"
"github.com/fcfcqloow/go-advance/log"
"github.com/mattn/go-colorable"
. "github.com/optim-corp/cios-cli/cli"
"github.com/optim-corp/cios-cli/cli/account"
"github.com/optim-corp/cios-cli/cli/authorization"
"github.com/optim-corp/cios-cli/cli/device"
"github.com/optim-corp/cios-cli/cli/filestorage"
"github.com/optim-corp/cios-cli/cli/geography"
"github.com/optim-corp/cios-cli/cli/group"
"github.com/optim-corp/cios-cli/cli/publishsubscribe"
"github.com/optim-corp/cios-cli/cli/resourceowner"
"github.com/optim-corp/cios-cli/cli/tool"
"github.com/optim-corp/cios-cli/models"
"github.com/optim-corp/cios-cli/utils"
sdkmodel "github.com/optim-corp/cios-golang-sdk/model"
ciossdk "github.com/optim-corp/cios-golang-sdk/sdk"
"github.com/tidwall/gjson"
"github.com/urfave/cli/v2"
)
const (
NAME = "Could IoT OS CLI"
VERSION = "0.3.2"
COPYRIGHT = "OPTiM Corporation"
APPLICATION_LOGO = `
{{.AnsiColor.Black}}
█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
████████{{.AnsiColor.Cyan}}██████████████{{.AnsiColor.Black}}█████{{.AnsiColor.Cyan}}████████{{.AnsiColor.Black}}████████{{.AnsiColor.Cyan}}███████████████{{.AnsiColor.Black}}███████████{{.AnsiColor.Cyan}}████████████████{{.AnsiColor.Black}}████████████████████████████████████████████████
█████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}███████{{.AnsiColor.Cyan}}█████████████████████{{.AnsiColor.Black}}█████{{.AnsiColor.Cyan}}███████████████████{{.AnsiColor.Black}}████████████████████████████████████████████████
█████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}███████{{.AnsiColor.Cyan}}█████{{.AnsiColor.Black}}███████████{{.AnsiColor.Cyan}}█████{{.AnsiColor.Black}}████{{.AnsiColor.Cyan}}██████{{.AnsiColor.Black}}██████████████████████████████████████████████████████████████
█████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}███████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}█████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}█████{{.AnsiColor.Cyan}}█████{{.AnsiColor.Black}}██████████████████████████████████████████████████████████████
█████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}███████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}█████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}███████{{.AnsiColor.Cyan}}█████{{.AnsiColor.Black}}████████████████████████████████████████████████████████████
█████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}███████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}█████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}███████████{{.AnsiColor.Cyan}}██████████████{{.AnsiColor.Black}}███████████████████████████████████████████████
█████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}███████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}█████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}██████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}██████████████████████████████████████████████
█████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}███████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}█████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}███████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}█████████████████████████████████████████████
█████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}███████{{.AnsiColor.Cyan}}█████{{.AnsiColor.Black}}███████████{{.AnsiColor.Cyan}}█████{{.AnsiColor.Black}}██████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}██████████████████████████████████████████████
█████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}███████{{.AnsiColor.Cyan}}█████████████████████{{.AnsiColor.Black}}████████████████████{{.AnsiColor.Cyan}}████{{.AnsiColor.Black}}████████████████████████████████████████████████
████████{{.AnsiColor.Cyan}}██████████████{{.AnsiColor.Black}}█████{{.AnsiColor.Cyan}}████████{{.AnsiColor.Black}}████████{{.AnsiColor.Cyan}}███████████████{{.AnsiColor.Black}}███████████{{.AnsiColor.Cyan}}██████████████{{.AnsiColor.Black}}██████████████████████████████████████████████████
█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
██████████████████████████████████████████████████████████████████{{.AnsiColor.BrightMagenta}}█████████████{{.AnsiColor.Black}}██████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}████████████████████{{.AnsiColor.BrightMagenta}}████████{{.AnsiColor.Black}}████████████████
████████████████████████████████████████████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}█████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████
████████████████████████████████████████████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}█████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████
████████████████████████████████████████████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}█████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████
████████████████████████████████████████████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}█████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████
████████████████████████████████████████████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}█████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████
████████████████████████████████████████████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}█████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████
████████████████████████████████████████████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}█████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████
████████████████████████████████████████████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}█████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████
████████████████████████████████████████████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}█████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████████{{.AnsiColor.BrightMagenta}}████{{.AnsiColor.Black}}██████████████████
█████████████████████████████████████████████████████████████████{{.AnsiColor.BrightMagenta}}██████████████{{.AnsiColor.Black}}██████{{.AnsiColor.BrightMagenta}}█████████████████████{{.AnsiColor.Black}}███{{.AnsiColor.BrightMagenta}}████████{{.AnsiColor.Black}}████████████████
█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████
`
)
var (
config models.Config
topDir = models.TopDir
configPath = models.ConfigPath
urlDir = models.UrlPath
is = utils.Is
path = ftil.Path
assert = utils.EAssert
)
func init() {
if os.Getenv("AUTH_TYPE") == "client" {
setClientType()
} else {
assert(path(topDir).CreateDir()).Log()
configFile := path(configPath)
urlFile := path(urlDir)
file, configErr := configFile.ReadFile()
urls, urlErr := urlFile.ReadFile()
assert(urlErr).
OnErr(func() { assert(urlFile.CreateFile()).Log() }).
OnErr(func() { assert(urlFile.WriteFileAsString(models.URL_JSON)) })
assert(configErr).
OnErr(func() { assert(configFile.CreateFile()).Log() }).
ExitWith(0)
assert(json.Unmarshal(file, &config)).
NoneErr(func() { setConfig(config, urls, config.Stage) })
}
}
func main() {
app := &cli.App{
Name: NAME,
Version: VERSION,
Copyright: COPYRIGHT,
Action: func(context *cli.Context) error {
banner.Init(colorable.NewColorableStdout(), true, true, bytes.NewBufferString(APPLICATION_LOGO))
println("\n\n\nPlease $cios help !!!!\n\n")
return nil
},
Commands: []*cli.Command{
publishsubscribe.GetDataStoreCommand(),
publishsubscribe.GetMessagingCommand(),
publishsubscribe.GetChannelCommand(),
device.GetDeviceMonitoringCommand(),
device.GetDeviceInventoryCommand(),
device.GetDeviceLifecycleCommand(),
device.GetDevicePolicyCommand(),
device.GetDeviceEntityCommand(),
device.GetDeviceModelsCommand(),
device.GetDeviceCommand(),
resourceowner.GetResourceOwnerCommand(),
authorization.GetLoginCommand(),
filestorage.GetBucketCommand(),
filestorage.GetFileCommand(),
filestorage.GetNodeCommand(),
geography.GetPointCommand(),
group.GetGroupCommand(),
//video.GetVideoCommand(),
tool.GetTimestampCommand(),
tool.GetConfigCommand(),
tool.GetSwitchCommand(),
tool.GetTokenCommand(),
tool.GetURLCommand(),
tool.GetLogCommand(),
account.GetMeCommand(),
},
}
if err := app.Run(os.Args); err != nil {
log.Error(err.Error())
}
}
func setClientType() {
logLevel := os.Getenv("LOG_LEVEL")
log.SetLevelOrDefault(is(logLevel == "").T("info").F(logLevel).Value.(string), log.LOG_LEVEL_WARN)
Client = ciossdk.NewCiosClient(ciossdk.CiosClientConfig{
Debug: log.GetLevel() == log.LOG_LEVEL_DEBUG,
AuthConfig: ciossdk.ClientAuthConf(os.Getenv("CIOS_CLIENT_ID"), os.Getenv("CIOS_CLIENT_SECRET"), os.Getenv("CIOS_SCOPE")),
AutoRefresh: true,
Urls: sdkmodel.CIOSUrl{
MessagingUrl: os.Getenv("CIOS_MESSAGING_URL"),
LocationUrl: os.Getenv("CIOS_LOCATION_URL"),
AccountsUrl: os.Getenv("CIOS_ACCOUNT_URL"),
StorageUrl: os.Getenv("CIOS_STORAGE_URL"),
IamUrl: os.Getenv("CIOS_IAM_URL"),
AuthUrl: os.Getenv("CIOS_AUTH_URL"),
VideoStreamingUrl: os.Getenv("CIOS_STREAMING_URL"),
DeviceManagementUrl: os.Getenv("CIOS_DEVICE_URL"),
DeviceMonitoringUrl: os.Getenv("CIOS_MONITORING_URL"),
DeviceAssetManagementUrl: os.Getenv("CIOS_ASSET_URL"),
},
})
}
func setConfig(config models.Config, urls []byte, stage string) {
_ = models.SetStage(stage)
log.SetLevelOrDefault(config.LogLevel, log.LOG_LEVEL_WARN)
https := "https://"
domain := func(name string) string {
return gjson.GetBytes(urls, stage+"."+name).String()
}
Client = ciossdk.NewCiosClient(ciossdk.CiosClientConfig{
Debug: log.GetLevel() == log.LOG_LEVEL_DEBUG,
AuthConfig: ciossdk.RefreshTokenAuth(
config.ClientID,
config.ClientSecret,
config.Refresh,
models.FullScope,
),
AutoRefresh: true,
Urls: sdkmodel.CIOSUrl{
MessagingUrl: https + domain("messaging"),
LocationUrl: https + domain("location"),
AccountsUrl: https + domain("account"),
StorageUrl: https + domain("storage"),
IamUrl: https + domain("iam"),
AuthUrl: https + domain("auth"),
VideoStreamingUrl: https + domain("video_streaming"),
DeviceMonitoringUrl: https + domain("monitoring"),
DeviceManagementUrl: https + domain("device"),
DeviceAssetManagementUrl: https + domain("device_asset"),
},
})
}