-
Notifications
You must be signed in to change notification settings - Fork 63
/
list.go
165 lines (136 loc) · 3.88 KB
/
list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2023, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package list
import (
"context"
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/MakeNowJust/heredoc"
"github.com/dustin/go-humanize"
"github.com/spf13/cobra"
kraftcloud "sdk.kraft.cloud"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/tableprinter"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
)
type ListOptions struct {
All bool `long:"all" usage:"Show all images by their digest"`
Output string `long:"output" short:"o" usage:"Set output format" default:"table"`
metro string
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&ListOptions{}, cobra.Command{
Short: "List all images at a metro for your account",
Use: "ls",
Aliases: []string{"list"},
Long: heredoc.Doc(`
List all images in your account.
`),
Example: heredoc.Doc(`
# List all images in your account.
$ kraft cloud img ls
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "kraftcloud-img",
},
})
if err != nil {
panic(err)
}
return cmd
}
func (opts *ListOptions) Pre(cmd *cobra.Command, _ []string) error {
opts.metro = cmd.Flag("metro").Value.String()
if opts.metro == "" {
opts.metro = os.Getenv("KRAFTCLOUD_METRO")
}
if opts.metro == "" {
return fmt.Errorf("kraftcloud metro is unset")
}
log.G(cmd.Context()).WithField("metro", opts.metro).Debug("using")
return nil
}
func (opts *ListOptions) Run(ctx context.Context, args []string) error {
auth, err := config.GetKraftCloudAuthConfigFromContext(ctx)
if err != nil {
return fmt.Errorf("could not retrieve credentials: %w", err)
}
client := kraftcloud.NewImagesClient(
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*auth)),
)
images, err := client.WithMetro(opts.metro).List(ctx)
if err != nil {
return fmt.Errorf("could not list images: %w", err)
}
err = iostreams.G(ctx).StartPager()
if err != nil {
log.G(ctx).Errorf("error starting pager: %v", err)
}
defer iostreams.G(ctx).StopPager()
cs := iostreams.G(ctx).ColorScheme()
table, err := tableprinter.NewTablePrinter(ctx,
tableprinter.WithMaxWidth(iostreams.G(ctx).TerminalWidth()),
tableprinter.WithOutputFormatFromString(opts.Output),
)
if err != nil {
return err
}
// Sort the features alphabetically. This ensures that comparisons between
// versions are symmetric.
sort.Slice(images, func(i, j int) bool {
// Check if we have numbers, sort them accordingly
if z, err := strconv.Atoi(images[i].Digest); err == nil {
if y, err := strconv.Atoi(images[j].Digest); err == nil {
return y < z
}
// If we get only one number, alway say its greater than letter
return true
}
// Compare letters normally
return images[j].Digest > images[i].Digest
})
// Header row
table.AddField("NAME", cs.Bold)
table.AddField("VERSION", cs.Bold)
if opts.Output != "table" {
table.AddField("PUBLIC", cs.Bold)
table.AddField("ARGS", cs.Bold)
}
table.AddField("SIZE", cs.Bold)
table.EndRow()
for _, image := range images {
if len(image.Tags) == 0 && !opts.All {
continue
}
var name string
var versions []string
if opts.All {
split := strings.Split(image.Digest, "@sha256:")
name = split[0]
versions = append(versions, split[1])
}
if len(image.Tags) > 0 {
for _, tag := range image.Tags {
split := strings.Split(tag, ":")
name = split[0]
versions = append(versions, split[1])
}
}
table.AddField(name, nil)
table.AddField(strings.Join(versions, ", "), nil)
if opts.Output != "table" {
table.AddField(fmt.Sprintf("%v", image.Public), nil)
table.AddField(image.Args, nil)
}
table.AddField(humanize.Bytes(uint64(image.SizeInBytes)), nil)
table.EndRow()
}
return table.Render(iostreams.G(ctx).Out)
}