-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
60 lines (55 loc) · 1.35 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
package composectl
import (
"encoding/json"
"fmt"
v1 "github.com/foundriesio/composeapp/pkg/compose/v1"
"github.com/spf13/cobra"
)
var listCmd = &cobra.Command{
Use: "ls",
Short: "list apps found in the store",
Long: ``,
Args: cobra.NoArgs,
}
type (
listOptions struct {
Format string
}
appJsonOutput struct {
Name string `json:"name"`
URI string `json:"uri"`
}
)
func init() {
opts := listOptions{}
listCmd.Flags().StringVar(&opts.Format, "format", "plain", "Format the output. Values: [plain | json]")
listCmd.Run = func(cmd *cobra.Command, args []string) {
if opts.Format != "plain" && opts.Format != "json" {
DieNotNil(fmt.Errorf("invalid value of `--format` option: %s", opts.Format))
}
listApps(cmd, args, &opts)
}
rootCmd.AddCommand(listCmd)
}
func listApps(cmd *cobra.Command, args []string, opts *listOptions) {
cs, err := v1.NewAppStore(config.StoreRoot, config.Platform)
DieNotNil(err)
apps, err := cs.ListApps(cmd.Context())
DieNotNil(err)
if opts.Format == "json" {
var lsOutput []appJsonOutput
for _, app := range apps {
lsOutput = append(lsOutput, appJsonOutput{
Name: app.Name,
URI: app.String(),
})
}
if b, err := json.MarshalIndent(lsOutput, "", " "); err == nil {
fmt.Println(string(b))
}
} else {
for _, a := range apps {
fmt.Printf("%s -> %s\n", a.Name, a.String())
}
}
}