Skip to content

Commit a095dca

Browse files
author
Joshua Anderson
committed
ref(client-go): break up controller/client package
This makes the code easier to follow and gives go bindings for auth functions
1 parent 15330f1 commit a095dca

20 files changed

Lines changed: 560 additions & 626 deletions

File tree

client-go/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ include ../includes.mk
44
repo_path = github.com/deis/deis/client-go
55

66
GO_FILES = $(wildcard *.go)
7-
GO_PACKAGES = parser cmd controller/api controller/client $(wildcard controller/models/*)
7+
GO_PACKAGES = parser cmd controller/api controller/client $(wildcard controller/models/*) $(wildcard pkg/*)
88
GO_PACKAGES_REPO_PATH = $(addprefix $(repo_path)/,$(GO_PACKAGES))
99

1010
COMPONENT = $(notdir $(repo_path))
1111
IMAGE = $(IMAGE_PREFIX)/$(COMPONENT):$(BUILD_TAG)
1212

1313
build:
1414
CGO_ENABLED=0 godep go build -a -installsuffix cgo -ldflags '-s' -o deis .
15-
@$(call check-static-binary,deis)
15+
@$(call check-static-binary,deis)
1616

1717
install:
1818
godep go install -v .

client-go/cmd/apps.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/deis/deis/client-go/controller/client"
1414
"github.com/deis/deis/client-go/controller/models/apps"
1515
"github.com/deis/deis/client-go/controller/models/config"
16+
"github.com/deis/deis/client-go/pkg/git"
17+
"github.com/deis/deis/client-go/pkg/webbrowser"
1618
)
1719

1820
// AppCreate creates an app.
@@ -44,10 +46,10 @@ func AppCreate(id string, buildpack string, remote string, noRemote bool) error
4446
}
4547

4648
if !noRemote {
47-
return c.CreateRemote(remote, app.ID)
49+
return git.CreateRemote(c.ControllerURL.Host, remote, app.ID)
4850
}
4951

50-
fmt.Println("remote available at", c.RemoteURL(app.ID))
52+
fmt.Println("remote available at", git.RemoteURL(c.ControllerURL.Host, app.ID))
5153

5254
return nil
5355
}
@@ -121,7 +123,7 @@ func AppOpen(appID string) error {
121123

122124
u.Scheme = "http"
123125

124-
return client.Webbrowser(u.String())
126+
return webbrowser.Webbrowser(u.String())
125127
}
126128

127129
// AppLogs returns the logs from an app.
@@ -182,7 +184,7 @@ func AppDestroy(appID, confirm string) error {
182184
}
183185

184186
if appID == "" {
185-
appID, err = c.DetectApp()
187+
appID, err = git.DetectAppName(c.ControllerURL.Host)
186188

187189
if err != nil {
188190
return err
@@ -215,7 +217,7 @@ func AppDestroy(appID, confirm string) error {
215217
fmt.Printf("done in %ds\n", int(time.Since(startTime).Seconds()))
216218

217219
if gitSession {
218-
return c.DeleteRemote(appID)
220+
return git.DeleteRemote(appID)
219221
}
220222

221223
return nil

client-go/cmd/auth.go

Lines changed: 99 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/deis/deis/client-go/controller/client"
10+
"github.com/deis/deis/client-go/controller/models/auth"
1011
"golang.org/x/crypto/ssh/terminal"
1112
)
1213

@@ -15,6 +16,7 @@ func Register(controller string, username string, password string, email string,
1516
sslVerify bool) error {
1617

1718
u, err := url.Parse(controller)
19+
httpClient := client.CreateHTTPClient(sslVerify)
1820

1921
if err != nil {
2022
return err
@@ -26,7 +28,7 @@ func Register(controller string, username string, password string, email string,
2628
return err
2729
}
2830

29-
if err = client.CheckConection(client.CreateHTTPClient(sslVerify), controllerURL); err != nil {
31+
if err = client.CheckConection(httpClient, controllerURL); err != nil {
3032
return err
3133
}
3234

@@ -56,7 +58,42 @@ func Register(controller string, username string, password string, email string,
5658
fmt.Scanln(&email)
5759
}
5860

59-
return client.Register(controllerURL, username, password, email, sslVerify, true)
61+
c := &client.Client{ControllerURL: controllerURL, SSLVerify: sslVerify, HTTPClient: httpClient}
62+
63+
tempClient, err := client.New()
64+
65+
if err == nil {
66+
c.Token = tempClient.Token
67+
}
68+
69+
err = auth.Register(c, username, password, email)
70+
71+
if err != nil {
72+
return err
73+
}
74+
75+
fmt.Printf("Registered %s\n", username)
76+
return doLogin(c, username, password)
77+
}
78+
79+
func doLogin(c *client.Client, username, password string) error {
80+
token, err := auth.Login(c, username, password)
81+
82+
if err != nil {
83+
return err
84+
}
85+
86+
c.Token = token
87+
c.Username = username
88+
89+
err = c.Save()
90+
91+
if err != nil {
92+
return nil
93+
}
94+
95+
fmt.Printf("Logged in as %s\n", username)
96+
return nil
6097
}
6198

6299
// Login to a Deis controller.
@@ -68,12 +105,13 @@ func Login(controller string, username string, password string, sslVerify bool)
68105
}
69106

70107
controllerURL, err := chooseScheme(*u)
108+
httpClient := client.CreateHTTPClient(sslVerify)
71109

72110
if err != nil {
73111
return err
74112
}
75113

76-
if err = client.CheckConection(client.CreateHTTPClient(sslVerify), controllerURL); err != nil {
114+
if err = client.CheckConection(httpClient, controllerURL); err != nil {
77115
return err
78116
}
79117

@@ -92,17 +130,28 @@ func Login(controller string, username string, password string, sslVerify bool)
92130
}
93131
}
94132

95-
return client.Login(controllerURL, username, password, sslVerify)
133+
c := &client.Client{ControllerURL: controllerURL, SSLVerify: sslVerify, HTTPClient: httpClient}
134+
135+
return doLogin(c, username, password)
96136
}
97137

98138
// Logout from a Deis controller.
99139
func Logout() error {
100-
return client.Logout()
140+
if err := client.Delete(); err != nil {
141+
return err
142+
}
143+
144+
fmt.Println("Logged out")
145+
return nil
101146
}
102147

103148
// Passwd changes a user's password.
104149
func Passwd(username string, password string, newPassword string) error {
105-
var err error
150+
c, err := client.New()
151+
152+
if err != nil {
153+
return err
154+
}
106155

107156
if password == "" {
108157
fmt.Print("current password: ")
@@ -131,7 +180,14 @@ func Passwd(username string, password string, newPassword string) error {
131180
}
132181
}
133182

134-
return client.Passwd(username, password, newPassword)
183+
err = auth.Passwd(c, username, password, newPassword)
184+
185+
if err != nil {
186+
return err
187+
}
188+
189+
fmt.Println("Password change succeeded.")
190+
return nil
135191
}
136192

137193
// Cancel deletes a user's account.
@@ -170,7 +226,18 @@ func Cancel(username string, password string, yes bool) error {
170226
return nil
171227
}
172228

173-
return client.Cancel()
229+
err = auth.Delete(c)
230+
231+
if err != nil {
232+
return err
233+
}
234+
235+
if err := client.Delete(); err != nil {
236+
return err
237+
}
238+
239+
fmt.Println("Account cancelled")
240+
return nil
174241
}
175242

176243
// Whoami prints the logged in user.
@@ -187,7 +254,30 @@ func Whoami() error {
187254

188255
// Regenerate regenenerates a user's token.
189256
func Regenerate(username string, all bool) error {
190-
return client.Regenerate(username, all)
257+
c, err := client.New()
258+
259+
if err != nil {
260+
return err
261+
}
262+
263+
token, err := auth.Regenerate(c, username, all)
264+
265+
if err != nil {
266+
return err
267+
}
268+
269+
if username == "" && all == false {
270+
c.Token = token
271+
272+
err = c.Save()
273+
274+
if err != nil {
275+
return err
276+
}
277+
}
278+
279+
fmt.Println("Token Regenerated")
280+
return nil
191281
}
192282

193283
func readPassword() (string, error) {

client-go/cmd/git.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package cmd
22

3+
import (
4+
"github.com/deis/deis/client-go/pkg/git"
5+
)
6+
37
// GitRemote creates a git remote for a deis app.
48
func GitRemote(appID, remote string) error {
59
c, appID, err := load(appID)
@@ -8,5 +12,5 @@ func GitRemote(appID, remote string) error {
812
return err
913
}
1014

11-
return c.CreateRemote(remote, appID)
15+
return git.CreateRemote(c.ControllerURL.Host, remote, appID)
1216
}

client-go/cmd/perms.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/deis/deis/client-go/controller/client"
77
"github.com/deis/deis/client-go/controller/models/perms"
8+
"github.com/deis/deis/client-go/pkg/git"
89
)
910

1011
// PermsList prints which users have permissions.
@@ -100,7 +101,7 @@ func permsLoad(appID string, admin bool) (*client.Client, string, error) {
100101
}
101102

102103
if !admin && appID == "" {
103-
appID, err = c.DetectApp()
104+
appID, err = git.DetectAppName(c.ControllerURL.Host)
104105

105106
if err != nil {
106107
return nil, "", err

client-go/cmd/utils.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/deis/deis/client-go/controller/client"
10+
"github.com/deis/deis/client-go/pkg/git"
1011
)
1112

1213
func progress() chan bool {
@@ -58,7 +59,7 @@ func load(appID string) (*client.Client, string, error) {
5859
}
5960

6061
if appID == "" {
61-
appID, err = c.DetectApp()
62+
appID, err = git.DetectAppName(c.ControllerURL.Host)
6263

6364
if err != nil {
6465
return nil, "", err

0 commit comments

Comments
 (0)