/
init.go
146 lines (124 loc) · 4.04 KB
/
init.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
package cblib
import (
//"flag"
"fmt"
"os"
cb "github.com/clearblade/Go-SDK"
"github.com/clearblade/cblib/internal/remote"
"github.com/clearblade/cblib/internal/types"
)
var (
flagInitRemoteName string
)
func init() {
usage :=
`
Initializes your filesystem with your ClearBlade Platform System or targets your local system to a different remote system within a ClearBlade Platform
`
example :=
`
cb-cli init
cb-cli init -url=https://platform.clearblade.com -messaging-url=platform.clearblade.com -system-key=8abcd6aa0baadcd8bbe3fabca29301 -email=dev@dev.com -password=pw
`
systemDotJSON = map[string]interface{}{}
svcCode = map[string]interface{}{}
myInitCommand := &SubCommand{
name: "init",
usage: usage,
needsAuth: false,
run: doInit,
example: example,
}
myInitCommand.flags.StringVar(&URL, "url", "", "Clearblade platform url for target system")
myInitCommand.flags.StringVar(&MsgURL, "messaging-url", "", "Clearblade messaging url for target system")
myInitCommand.flags.StringVar(&SystemKey, "system-key", "", "System Key for target system, ex 9b9eea9c0bda8896a3dab5aeec9601")
myInitCommand.flags.StringVar(&Email, "email", "", "Developer email for login")
myInitCommand.flags.StringVar(&Password, "password", "", "Developer password")
myInitCommand.flags.StringVar(&DevToken, "dev-token", "", "Developer token to use instead of email/password")
myInitCommand.flags.BoolVar(&SkipUpdateMapNameToIdFiles, "skip-update-map-name-to-id", false, "Set this to true to skip pulling the IDs for roles, collections, and users. This is useful if the system has lots of these types of assets and the goal is to retrieve the schema for the tables after initialization.")
myInitCommand.flags.StringVar(&flagInitRemoteName, "name", "init", "Name of the initial remote")
AddCommand("init", myInitCommand)
}
func doInit(cmd *SubCommand, client *cb.DevClient, args ...string) error {
if len(args) != 0 {
fmt.Printf("init command takes no arguments; only options: '%+v'\n", args)
os.Exit(1)
}
defaults := setupInitDefaults()
MetaInfo = nil
client, err := Authorize(defaults)
if err != nil {
return err
}
return reallyInit(cmd, client, SystemKey)
}
func reallyInit(cmd *SubCommand, cli *cb.DevClient, sysKey string) error {
SetRootDir(".")
if err := setupDirectoryStructure(); err != nil {
return err
}
systemMeta, err := pullSystemMeta(sysKey, cli)
if err != nil {
return err
}
err = initRemote(cmd, systemMeta, cli)
if err != nil {
return err
}
if !SkipUpdateMapNameToIdFiles {
logInfo("Updating map name to ID files...")
updateMapNameToIDFiles(systemMeta, cli)
}
fmt.Printf("\nSystem '%s' has been initialized in the current directory.\n", systemMeta.Name)
return nil
}
func initRemote(cmd *SubCommand, systemMeta *types.System_meta, cli *cb.DevClient) error {
cmd.remotes = remote.NewRemotes()
initRemote := &remote.Remote{
Name: flagInitRemoteName,
PlatformURL: cli.HttpAddr,
MessagingURL: cli.MqttAddr,
SystemKey: systemMeta.Key,
SystemSecret: systemMeta.Secret,
Token: cli.DevToken,
}
cmd.remotes.Put(initRemote)
systemJSON := systemMetaToMap(systemMeta)
cbmeta := map[string]interface{}{
"platform_url": cli.HttpAddr,
"messaging_url": cli.MqttAddr,
"developer_email": cli.Email,
"token": cli.DevToken,
}
return useRemoteByMerging(systemJSON, cbmeta, initRemote)
}
type DefaultInfo struct {
url string
email string
systemKey string
msgUrl string
}
func setupInitDefaults() *DefaultInfo {
meta, err := getSysMeta()
if err != nil || MetaInfo == nil {
return nil
}
platform_url, ok := MetaInfo["platformURL"].(string)
if !ok {
platform_url = MetaInfo["platform_url"].(string)
}
email, ok := MetaInfo["developerEmail"].(string)
if !ok {
email = MetaInfo["developer_email"].(string)
}
messaging_url, ok := MetaInfo["messagingURL"].(string)
if !ok {
messaging_url = MetaInfo["messaging_url"].(string)
}
return &DefaultInfo{
url: platform_url,
email: email,
systemKey: meta.Key,
msgUrl: messaging_url,
}
}