-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloud.go
208 lines (166 loc) · 3.72 KB
/
cloud.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"fmt"
"github.com/hephbuild/heph/bootstrap"
"github.com/hephbuild/heph/cloudclient"
"github.com/hephbuild/heph/log/log"
"github.com/spf13/cobra"
"golang.org/x/term"
"strings"
"syscall"
)
var metas *[]string
func init() {
cloudCmd.AddCommand(cloudLoginCmd)
cloudCmd.AddCommand(cloudLogoutCmd)
cloudCmd.AddCommand(cloudAuthActorCmd)
cloudCmd.AddCommand(cloudStartFlowCmd)
cloudCmd.AddCommand(cloudStopFlowCmd)
cloudCmd.AddCommand(cloudFlowGetCmd)
metas = cloudStartFlowCmd.PersistentFlags().StringArrayP("metas", "m", nil, "Flow meta key=value")
}
var cloudCmd = &cobra.Command{
Use: "cloud",
}
var cloudLoginCmd = &cobra.Command{
Use: "login",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
bs, err := bootstrapInit(ctx)
if err != nil {
return err
}
if bs.Cloud.Client == nil {
return fmt.Errorf("cloud not configured")
}
fmt.Println(bs.Config.Cloud.URL)
var email, pass string
if len(args) < 2 {
fmt.Print("Email: ")
_, err = fmt.Scanln(&email)
if err != nil {
return err
}
fmt.Print("Password: ")
passb, err := term.ReadPassword(syscall.Stdin)
fmt.Println()
if err != nil {
return err
}
pass = string(passb)
} else {
email, pass = args[0], args[1]
}
res, err := cloudclient.Login(ctx, bs.Cloud.Client, email, pass)
if err != nil {
return err
}
tok := res.Login.Token
user := res.Login.User
err = bs.Cloud.StoreCloudAuthData(bootstrap.CloudAuthData{
Token: tok,
UserID: user.Id,
UserEmail: user.Email,
})
if err != nil {
return err
}
log.Infof("Successfully authenticated as %v", user.Email)
return nil
},
}
var cloudLogoutCmd = &cobra.Command{
Use: "logout",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
bs, err := bootstrapInit(ctx)
if err != nil {
return err
}
return bs.Cloud.DeleteCloudAuthData()
},
}
var cloudAuthActorCmd = &cobra.Command{
Use: "auth-actor",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
bs, err := bootstrapInit(ctx)
if err != nil {
return err
}
res, err := cloudclient.AuthActor(ctx, bs.Cloud.AuthClient)
if err != nil {
return err
}
actor := res.Auth.Actor
if actor == nil {
return fmt.Errorf("no actor")
}
log.Info(actor.GetActor_type(), actor.GetActor_id())
return nil
},
}
var cloudStartFlowCmd = &cobra.Command{
Use: "start-flow",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
bs, err := bootstrapInit(ctx)
if err != nil {
return err
}
metasm := map[string]string{}
for _, s := range *metas {
parts := strings.SplitN(s, "=", 2)
if len(parts) != 2 {
return fmt.Errorf("malformed meta `%v`", s)
}
metasm[parts[0]] = parts[1]
}
err = bs.Cloud.StartFlow(ctx, args[0], metasm)
if err != nil {
return err
}
return nil
},
}
var cloudStopFlowCmd = &cobra.Command{
Use: "stop-flow",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
bs, err := bootstrapInit(ctx)
if err != nil {
return err
}
err = bs.Cloud.StopCurrentFlow(ctx)
if err != nil {
return err
}
return nil
},
}
var cloudFlowGetCmd = &cobra.Command{
Use: "get",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
bs, err := bootstrapInit(ctx)
if err != nil {
return err
}
flow, err := bs.Cloud.GetCurrentFlowDetails(ctx)
if err != nil {
return err
}
switch args[0] {
case "id":
fmt.Println(flow.ID)
case "url":
fmt.Println(flow.URL)
default:
return fmt.Errorf("invalid flow attribute %v", args[0])
}
return nil
},
}