Skip to content

Commit 7695f08

Browse files
committed
Refactored 'info' command (scaleway#80)
1 parent 2c5a4f9 commit 7695f08

File tree

2 files changed

+55
-31
lines changed

2 files changed

+55
-31
lines changed

pkg/cli/info.go

+10-31
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55
package cli
66

77
import (
8-
"fmt"
9-
"os"
10-
"runtime"
11-
12-
"github.com/scaleway/scaleway-cli/vendor/github.com/kardianos/osext"
13-
14-
"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"
1510
)
1611

1712
var cmdInfo = &Command{
@@ -28,34 +23,18 @@ func init() {
2823
// Flags
2924
var infoHelp bool // -h, --help flag
3025

31-
func runInfo(cmd *Command, args []string) {
26+
func runInfo(cmd *Command, rawArgs []string) {
3227
if infoHelp {
3328
cmd.PrintUsage()
3429
}
35-
if len(args) != 0 {
30+
if len(rawArgs) != 0 {
3631
cmd.PrintShortUsage()
3732
}
3833

39-
// FIXME: fmt.Printf("Servers: %s\n", "quantity")
40-
// FIXME: fmt.Printf("Images: %s\n", "quantity")
41-
fmt.Printf("Debug mode (client): %v\n", os.Getenv("DEBUG") != "")
42-
43-
fmt.Printf("Organization: %s\n", cmd.API.Organization)
44-
// FIXME: add partially-masked token
45-
fmt.Printf("API Endpoint: %s\n", os.Getenv("scaleway_api_endpoint"))
46-
configPath, _ := utils.GetConfigFilePath()
47-
fmt.Printf("RC file: %s\n", configPath)
48-
fmt.Printf("User: %s\n", os.Getenv("USER"))
49-
fmt.Printf("CPUs: %d\n", runtime.NumCPU())
50-
hostname, _ := os.Hostname()
51-
fmt.Printf("Hostname: %s\n", hostname)
52-
cliPath, _ := osext.Executable()
53-
fmt.Printf("CLI Path: %s\n", cliPath)
54-
55-
fmt.Printf("Cache: %s\n", cmd.API.Cache.Path)
56-
fmt.Printf(" Servers: %d\n", cmd.API.Cache.GetNbServers())
57-
fmt.Printf(" Images: %d\n", cmd.API.Cache.GetNbImages())
58-
fmt.Printf(" Snapshots: %d\n", cmd.API.Cache.GetNbSnapshots())
59-
fmt.Printf(" Volumes: %d\n", cmd.API.Cache.GetNbVolumes())
60-
fmt.Printf(" Bootscripts: %d\n", cmd.API.Cache.GetNbBootscripts())
34+
args := commands.InfoArgs{}
35+
ctx := cmd.GetContext(rawArgs)
36+
err := commands.RunInfo(ctx, args)
37+
if err != nil {
38+
logrus.Fatalf("Cannot execute 'info': %v", err)
39+
}
6140
}

pkg/commands/info.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
"runtime"
11+
12+
"github.com/scaleway/scaleway-cli/vendor/github.com/kardianos/osext"
13+
14+
"github.com/scaleway/scaleway-cli/pkg/utils"
15+
)
16+
17+
// InfoArgs are flags for the `RunInfo` function
18+
type InfoArgs struct{}
19+
20+
// RunInfo is the handler for 'scw info'
21+
func RunInfo(ctx CommandContext, args InfoArgs) error {
22+
// FIXME: fmt.Fprintf(ctx.Stdout, "Servers: %s\n", "quantity")
23+
// FIXME: fmt.Fprintf(ctx.Stdout, "Images: %s\n", "quantity")
24+
fmt.Fprintf(ctx.Stdout, "Debug mode (client): %v\n", ctx.Getenv("DEBUG") != "")
25+
26+
fmt.Fprintf(ctx.Stdout, "Organization: %s\n", ctx.API.Organization)
27+
// FIXME: add partially-masked token
28+
fmt.Fprintf(ctx.Stdout, "API Endpoint: %s\n", ctx.Getenv("scaleway_api_endpoint"))
29+
configPath, _ := utils.GetConfigFilePath()
30+
fmt.Fprintf(ctx.Stdout, "RC file: %s\n", configPath)
31+
fmt.Fprintf(ctx.Stdout, "User: %s\n", ctx.Getenv("USER"))
32+
fmt.Fprintf(ctx.Stdout, "CPUs: %d\n", runtime.NumCPU())
33+
hostname, _ := os.Hostname()
34+
fmt.Fprintf(ctx.Stdout, "Hostname: %s\n", hostname)
35+
cliPath, _ := osext.Executable()
36+
fmt.Fprintf(ctx.Stdout, "CLI Path: %s\n", cliPath)
37+
38+
fmt.Fprintf(ctx.Stdout, "Cache: %s\n", ctx.API.Cache.Path)
39+
fmt.Fprintf(ctx.Stdout, " Servers: %d\n", ctx.API.Cache.GetNbServers())
40+
fmt.Fprintf(ctx.Stdout, " Images: %d\n", ctx.API.Cache.GetNbImages())
41+
fmt.Fprintf(ctx.Stdout, " Snapshots: %d\n", ctx.API.Cache.GetNbSnapshots())
42+
fmt.Fprintf(ctx.Stdout, " Volumes: %d\n", ctx.API.Cache.GetNbVolumes())
43+
fmt.Fprintf(ctx.Stdout, " Bootscripts: %d\n", ctx.API.Cache.GetNbBootscripts())
44+
return nil
45+
}

0 commit comments

Comments
 (0)