Skip to content

Commit 9ea9db3

Browse files
committed
Refactored 'logout' command (scaleway#80)
1 parent 91617d1 commit 9ea9db3

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

pkg/cli/logout.go

+8-16
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
package cli
66

77
import (
8-
"os"
9-
10-
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
11-
12-
"github.com/scaleway/scaleway-cli/pkg/utils"
8+
"github.com/scaleway/scaleway-cli/pkg/commands"
9+
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
1310
)
1411

1512
var cmdLogout = &Command{
@@ -26,23 +23,18 @@ func init() {
2623
// FLags
2724
var logoutHelp bool // -h, --help flag
2825

29-
func runLogout(cmd *Command, args []string) {
26+
func runLogout(cmd *Command, rawArgs []string) {
3027
if logoutHelp {
3128
cmd.PrintUsage()
3229
}
33-
if len(args) != 0 {
30+
if len(rawArgs) != 0 {
3431
cmd.PrintShortUsage()
3532
}
3633

37-
scwrcPath, err := utils.GetConfigFilePath()
34+
args := commands.LogoutArgs{}
35+
ctx := cmd.GetContext(rawArgs)
36+
err := commands.RunLogout(ctx, args)
3837
if err != nil {
39-
log.Fatalf("Unable to get scwrc config file path: %v", err)
40-
}
41-
42-
if _, err = os.Stat(scwrcPath); err == nil {
43-
err = os.Remove(scwrcPath)
44-
if err != nil {
45-
log.Fatalf("Unable to remove scwrc config file: %v", err)
46-
}
38+
logrus.Fatalf("Cannot execute 'logout': %v", err)
4739
}
4840
}

pkg/commands/logout.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (C) 2015 Scaleway. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package commands
6+
7+
import (
8+
"fmt"
9+
"os"
10+
11+
"github.com/scaleway/scaleway-cli/pkg/utils"
12+
)
13+
14+
// LogoutArgs are flags for the `RunLogout` function
15+
type LogoutArgs struct{}
16+
17+
// RunLogout is the handler for 'scw logout'
18+
func RunLogout(ctx CommandContext, args LogoutArgs) error {
19+
// FIXME: ask if we need to remove the local ssh key on the account
20+
scwrcPath, err := utils.GetConfigFilePath()
21+
if err != nil {
22+
return fmt.Errorf("unable to get scwrc config file path: %v", err)
23+
}
24+
25+
if _, err = os.Stat(scwrcPath); err == nil {
26+
err = os.Remove(scwrcPath)
27+
if err != nil {
28+
return fmt.Errorf("unable to remove scwrc config file: %v", err)
29+
}
30+
}
31+
return nil
32+
}

0 commit comments

Comments
 (0)