Skip to content

Commit 6365a24

Browse files
committed
Merge pull request #4169 from Joshua-Anderson/refactor-auth
ref(client-go): break up controller/client package
2 parents 861052f + a095dca commit 6365a24

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
@@ -8,6 +8,7 @@ import (
88
"syscall"
99

1010
"github.com/deis/deis/client-go/controller/client"
11+
"github.com/deis/deis/client-go/controller/models/auth"
1112
"golang.org/x/crypto/ssh/terminal"
1213
)
1314

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

1819
u, err := url.Parse(controller)
20+
httpClient := client.CreateHTTPClient(sslVerify)
1921

2022
if err != nil {
2123
return err
@@ -27,7 +29,7 @@ func Register(controller string, username string, password string, email string,
2729
return err
2830
}
2931

30-
if err = client.CheckConection(client.CreateHTTPClient(sslVerify), controllerURL); err != nil {
32+
if err = client.CheckConection(httpClient, controllerURL); err != nil {
3133
return err
3234
}
3335

@@ -57,7 +59,42 @@ func Register(controller string, username string, password string, email string,
5759
fmt.Scanln(&email)
5860
}
5961

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

63100
// Login to a Deis controller.
@@ -69,12 +106,13 @@ func Login(controller string, username string, password string, sslVerify bool)
69106
}
70107

71108
controllerURL, err := chooseScheme(*u)
109+
httpClient := client.CreateHTTPClient(sslVerify)
72110

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

77-
if err = client.CheckConection(client.CreateHTTPClient(sslVerify), controllerURL); err != nil {
115+
if err = client.CheckConection(httpClient, controllerURL); err != nil {
78116
return err
79117
}
80118

@@ -93,17 +131,28 @@ func Login(controller string, username string, password string, sslVerify bool)
93131
}
94132
}
95133

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

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

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

108157
if password == "" && username == "" {
109158
fmt.Print("current password: ")
@@ -132,7 +181,14 @@ func Passwd(username string, password string, newPassword string) error {
132181
}
133182
}
134183

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

138194
// Cancel deletes a user's account.
@@ -171,7 +227,18 @@ func Cancel(username string, password string, yes bool) error {
171227
return nil
172228
}
173229

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

177244
// Whoami prints the logged in user.
@@ -188,7 +255,30 @@ func Whoami() error {
188255

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

194284
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)