Skip to content

Commit cac92ab

Browse files
committed
Refactored 'tag' command (scaleway#80)
1 parent b1429dc commit cac92ab

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

pkg/cli/tag.go

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

77
import (
8-
"fmt"
9-
10-
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
8+
"github.com/scaleway/scaleway-cli/pkg/commands"
9+
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
1110
)
1211

1312
var cmdTag = &Command{
@@ -24,23 +23,21 @@ func init() {
2423
// Flags
2524
var tagHelp bool // -h, --help flag
2625

27-
func runTag(cmd *Command, args []string) {
26+
func runTag(cmd *Command, rawArgs []string) {
2827
if tagHelp {
2928
cmd.PrintUsage()
3029
}
31-
if len(args) != 2 {
30+
if len(rawArgs) != 2 {
3231
cmd.PrintShortUsage()
3332
}
3433

35-
snapshotID := cmd.API.GetSnapshotID(args[0])
36-
snapshot, err := cmd.API.GetSnapshot(snapshotID)
37-
if err != nil {
38-
log.Fatalf("Cannot fetch snapshot: %v", err)
34+
args := commands.TagArgs{
35+
Snapshot: rawArgs[0],
36+
Name: rawArgs[1],
3937
}
40-
41-
image, err := cmd.API.PostImage(snapshot.Identifier, args[1])
38+
ctx := cmd.GetContext(rawArgs)
39+
err := commands.RunTag(ctx, args)
4240
if err != nil {
43-
log.Fatalf("Cannot create image: %v", err)
41+
logrus.Fatalf("Cannot execute 'tag': %v", err)
4442
}
45-
fmt.Println(image)
4643
}

pkg/commands/tag.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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 "fmt"
8+
9+
// TagArgs are flags for the `RunTag` function
10+
type TagArgs struct {
11+
Snapshot string
12+
Name string
13+
}
14+
15+
// RunTag is the handler for 'scw tag'
16+
func RunTag(ctx CommandContext, args TagArgs) error {
17+
snapshotID := ctx.API.GetSnapshotID(args.Snapshot)
18+
snapshot, err := ctx.API.GetSnapshot(snapshotID)
19+
if err != nil {
20+
return fmt.Errorf("cannot fetch snapshot: %v", err)
21+
}
22+
23+
image, err := ctx.API.PostImage(snapshot.Identifier, args.Name)
24+
if err != nil {
25+
return fmt.Errorf("cannot create image: %v", err)
26+
}
27+
fmt.Fprintln(ctx.Stdout, image)
28+
return nil
29+
}

0 commit comments

Comments
 (0)