Skip to content

Commit 5030b00

Browse files
ref(*): use go sdk instead of built in code (#84)
1 parent 1d67881 commit 5030b00

63 files changed

Lines changed: 469 additions & 4845 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ DIST_DIR := _dist
1919

2020
GO_FILES = $(wildcard *.go)
2121
GO_LDFLAGS = -ldflags "-s -X ${repo_path}/version.BuildVersion=${VERSION}"
22-
GO_PACKAGES = cmd controller/api controller/client $(wildcard controller/models/*) parser $(wildcard pkg/*)
22+
GO_PACKAGES = cmd parser $(wildcard pkg/*)
2323
GO_PACKAGES_REPO_PATH = $(addprefix $(repo_path)/,$(GO_PACKAGES))
2424
GOFMT = gofmt -e -l -s
2525
GOTEST = go test --cover --race -v

cmd/apps.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ import (
88

99
"github.com/deis/pkg/prettyprint"
1010

11-
"github.com/deis/workflow-cli/controller/api"
12-
"github.com/deis/workflow-cli/controller/client"
13-
"github.com/deis/workflow-cli/controller/models/apps"
14-
"github.com/deis/workflow-cli/controller/models/config"
11+
"github.com/deis/controller-sdk-go/api"
12+
"github.com/deis/controller-sdk-go/apps"
13+
"github.com/deis/controller-sdk-go/config"
1514
"github.com/deis/workflow-cli/pkg/git"
1615
"github.com/deis/workflow-cli/pkg/webbrowser"
16+
"github.com/deis/workflow-cli/settings"
1717
)
1818

1919
// AppCreate creates an app.
2020
func AppCreate(id string, buildpack string, remote string, noRemote bool) error {
21-
c, err := client.New()
21+
c, err := settings.Load()
2222
if err != nil {
2323
return err
2424
}
@@ -30,7 +30,7 @@ func AppCreate(id string, buildpack string, remote string, noRemote bool) error
3030
quit <- true
3131
<-quit
3232

33-
if err != nil {
33+
if checkAPICompatibility(c, err) != nil {
3434
return err
3535
}
3636

@@ -42,7 +42,7 @@ func AppCreate(id string, buildpack string, remote string, noRemote bool) error
4242
"BUILDPACK_URL": buildpack,
4343
},
4444
}
45-
if _, err = config.Set(c, app.ID, configValues); err != nil {
45+
if _, err = config.Set(c, app.ID, configValues); checkAPICompatibility(c, err) != nil {
4646
return err
4747
}
4848
}
@@ -68,7 +68,7 @@ func AppCreate(id string, buildpack string, remote string, noRemote bool) error
6868

6969
// AppsList lists apps on the Deis controller.
7070
func AppsList(results int) error {
71-
c, err := client.New()
71+
c, err := settings.Load()
7272

7373
if err != nil {
7474
return err
@@ -79,8 +79,7 @@ func AppsList(results int) error {
7979
}
8080

8181
apps, count, err := apps.List(c, results)
82-
83-
if err != nil {
82+
if checkAPICompatibility(c, err) != nil {
8483
return err
8584
}
8685

@@ -101,8 +100,7 @@ func AppInfo(appID string) error {
101100
}
102101

103102
app, err := apps.Get(c, appID)
104-
105-
if err != nil {
103+
if checkAPICompatibility(c, err) != nil {
106104
return err
107105
}
108106

@@ -140,8 +138,7 @@ func AppOpen(appID string) error {
140138
}
141139

142140
app, err := apps.Get(c, appID)
143-
144-
if err != nil {
141+
if checkAPICompatibility(c, err) != nil {
145142
return err
146143
}
147144

@@ -162,8 +159,7 @@ func AppLogs(appID string, lines int) error {
162159
}
163160

164161
logs, err := apps.Logs(c, appID, lines)
165-
166-
if err != nil {
162+
if checkAPICompatibility(c, err) != nil {
167163
return err
168164
}
169165

@@ -197,8 +193,7 @@ func AppRun(appID, command string) error {
197193
fmt.Printf("Running '%s'...\n", command)
198194

199195
out, err := apps.Run(c, appID, command)
200-
201-
if err != nil {
196+
if checkAPICompatibility(c, err) != nil {
202197
return err
203198
}
204199

@@ -216,7 +211,7 @@ func AppRun(appID, command string) error {
216211
func AppDestroy(appID, confirm string) error {
217212
gitSession := false
218213

219-
c, err := client.New()
214+
c, err := settings.Load()
220215

221216
if err != nil {
222217
return err
@@ -249,7 +244,7 @@ func AppDestroy(appID, confirm string) error {
249244
startTime := time.Now()
250245
fmt.Printf("Destroying %s...\n", appID)
251246

252-
if err = apps.Delete(c, appID); err != nil {
247+
if err = apps.Delete(c, appID); checkAPICompatibility(c, err) != nil {
253248
return err
254249
}
255250

@@ -273,8 +268,7 @@ func AppTransfer(appID, username string) error {
273268
fmt.Printf("Transferring %s to %s... ", appID, username)
274269

275270
err = apps.Transfer(c, appID, username)
276-
277-
if err != nil {
271+
if checkAPICompatibility(c, err) != nil {
278272
return err
279273
}
280274

cmd/auth.go

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,37 @@ package cmd
33
import (
44
"errors"
55
"fmt"
6-
"net/url"
76
"os"
87
"reflect"
98
"strings"
109
"syscall"
1110

12-
"github.com/deis/workflow-cli/controller/client"
13-
"github.com/deis/workflow-cli/controller/models/auth"
11+
client "github.com/deis/controller-sdk-go"
12+
"github.com/deis/controller-sdk-go/auth"
13+
"github.com/deis/workflow-cli/settings"
1414
"golang.org/x/crypto/ssh/terminal"
1515
)
1616

1717
// Register creates a account on a Deis controller.
1818
func Register(controller string, username string, password string, email string,
1919
sslVerify bool) error {
2020

21-
u, err := url.Parse(controller)
22-
httpClient := client.CreateHTTPClient(sslVerify)
21+
c, err := client.New(sslVerify, controller, "", "")
2322

2423
if err != nil {
2524
return err
2625
}
2726

28-
controllerURL, err := chooseScheme(*u)
27+
tempClient, err := settings.Load()
2928

30-
if err != nil {
31-
return err
29+
if err == nil && tempClient.ControllerURL.Host == c.ControllerURL.Host {
30+
c.Token = tempClient.Token
3231
}
3332

34-
if err = client.CheckConnection(httpClient, controllerURL); err != nil {
33+
// Set user agent for temporary client.
34+
c.UserAgent = settings.UserAgent
35+
36+
if err = c.CheckConnection(); checkAPICompatibility(c, err) != nil {
3537
return err
3638
}
3739

@@ -61,19 +63,11 @@ func Register(controller string, username string, password string, email string,
6163
fmt.Scanln(&email)
6264
}
6365

64-
c := &client.Client{ControllerURL: controllerURL, SSLVerify: sslVerify, HTTPClient: httpClient}
65-
66-
tempClient, err := client.New()
67-
68-
if err == nil {
69-
c.Token = tempClient.Token
70-
}
71-
7266
err = auth.Register(c, username, password, email)
7367

7468
c.Token = ""
7569

76-
if err != nil {
70+
if checkAPICompatibility(c, err) != nil {
7771
fmt.Fprint(os.Stderr, "Registration failed: ")
7872
return err
7973
}
@@ -84,15 +78,14 @@ func Register(controller string, username string, password string, email string,
8478

8579
func doLogin(c *client.Client, username, password string) error {
8680
token, err := auth.Login(c, username, password)
87-
88-
if err != nil {
81+
if checkAPICompatibility(c, err) != nil {
8982
return err
9083
}
9184

9285
c.Token = token
9386
c.Username = username
9487

95-
err = c.Save()
88+
err = settings.Save(c)
9689

9790
if err != nil {
9891
return nil
@@ -104,20 +97,16 @@ func doLogin(c *client.Client, username, password string) error {
10497

10598
// Login to a Deis controller.
10699
func Login(controller string, username string, password string, sslVerify bool) error {
107-
u, err := url.Parse(controller)
100+
c, err := client.New(sslVerify, controller, "", "")
108101

109102
if err != nil {
110103
return err
111104
}
112105

113-
controllerURL, err := chooseScheme(*u)
114-
httpClient := client.CreateHTTPClient(sslVerify)
106+
// Set user agent for temporary client.
107+
c.UserAgent = settings.UserAgent
115108

116-
if err != nil {
117-
return err
118-
}
119-
120-
if err = client.CheckConnection(httpClient, controllerURL); err != nil {
109+
if err = c.CheckConnection(); checkAPICompatibility(c, err) != nil {
121110
return err
122111
}
123112

@@ -136,14 +125,12 @@ func Login(controller string, username string, password string, sslVerify bool)
136125
}
137126
}
138127

139-
c := &client.Client{ControllerURL: controllerURL, SSLVerify: sslVerify, HTTPClient: httpClient}
140-
141128
return doLogin(c, username, password)
142129
}
143130

144131
// Logout from a Deis controller.
145132
func Logout() error {
146-
if err := client.Delete(); err != nil {
133+
if err := settings.Delete(); err != nil {
147134
return err
148135
}
149136

@@ -153,7 +140,7 @@ func Logout() error {
153140

154141
// Passwd changes a user's password.
155142
func Passwd(username string, password string, newPassword string) error {
156-
c, err := client.New()
143+
c, err := settings.Load()
157144

158145
if err != nil {
159146
return err
@@ -187,8 +174,7 @@ func Passwd(username string, password string, newPassword string) error {
187174
}
188175

189176
err = auth.Passwd(c, username, password, newPassword)
190-
191-
if err != nil {
177+
if checkAPICompatibility(c, err) != nil {
192178
fmt.Fprint(os.Stderr, "Password change failed: ")
193179
return err
194180
}
@@ -199,7 +185,7 @@ func Passwd(username string, password string, newPassword string) error {
199185

200186
// Cancel deletes a user's account.
201187
func Cancel(username string, password string, yes bool) error {
202-
c, err := client.New()
188+
c, err := settings.Load()
203189

204190
if err != nil {
205191
return err
@@ -208,15 +194,15 @@ func Cancel(username string, password string, yes bool) error {
208194
if username == "" || password != "" {
209195
fmt.Println("Please log in again in order to cancel this account")
210196

211-
if err = Login(c.ControllerURL.String(), username, password, c.SSLVerify); err != nil {
197+
if err = Login(c.ControllerURL.String(), username, password, c.VerifySSL); err != nil {
212198
return err
213199
}
214200
}
215201

216202
if yes == false {
217203
confirm := ""
218204

219-
c, err = client.New()
205+
c, err = settings.Load()
220206

221207
if err != nil {
222208
return err
@@ -244,15 +230,15 @@ func Cancel(username string, password string, yes bool) error {
244230
err = auth.Delete(c, username)
245231
cleanup := fmt.Errorf("\n%s %s\n\n", "409", "Conflict")
246232
if reflect.DeepEqual(err, cleanup) {
247-
fmt.Printf("%s still has application associated with it. Transfer ownership or delete them first\n", username)
233+
fmt.Printf("%s still has applications associated with it. Transfer ownership or delete them first\n", username)
248234
return nil
249-
} else if err != nil {
235+
} else if checkAPICompatibility(c, err) != nil {
250236
return err
251237
}
252238

253239
// If user targets themselves, logout.
254240
if username == "" || c.Username == username {
255-
if err := client.Delete(); err != nil {
241+
if err := settings.Delete(); err != nil {
256242
return err
257243
}
258244
}
@@ -263,7 +249,7 @@ func Cancel(username string, password string, yes bool) error {
263249

264250
// Whoami prints the logged in user.
265251
func Whoami() error {
266-
c, err := client.New()
252+
c, err := settings.Load()
267253

268254
if err != nil {
269255
return err
@@ -275,22 +261,21 @@ func Whoami() error {
275261

276262
// Regenerate regenenerates a user's token.
277263
func Regenerate(username string, all bool) error {
278-
c, err := client.New()
264+
c, err := settings.Load()
279265

280266
if err != nil {
281267
return err
282268
}
283269

284270
token, err := auth.Regenerate(c, username, all)
285-
286-
if err != nil {
271+
if checkAPICompatibility(c, err) != nil {
287272
return err
288273
}
289274

290275
if username == "" && all == false {
291276
c.Token = token
292277

293-
err = c.Save()
278+
err = settings.Save(c)
294279

295280
if err != nil {
296281
return err
@@ -306,18 +291,3 @@ func readPassword() (string, error) {
306291

307292
return string(password), err
308293
}
309-
310-
func chooseScheme(u url.URL) (url.URL, error) {
311-
if u.Scheme == "" {
312-
u.Scheme = "http"
313-
u, err := url.Parse(u.String())
314-
315-
if err != nil {
316-
return url.URL{}, err
317-
}
318-
319-
return *u, nil
320-
}
321-
322-
return u, nil
323-
}

0 commit comments

Comments
 (0)