-
Notifications
You must be signed in to change notification settings - Fork 9
/
cmd.go
115 lines (91 loc) · 3.04 KB
/
cmd.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package workspace
import (
"context"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnapi"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
)
func NewWorkspaceCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "workspace",
Short: "Interact with Namespace workspace.",
}
cmd.AddCommand(newDescribeCmd())
return cmd
}
func newDescribeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "describe",
Short: "Describe current workspace details.",
Args: cobra.NoArgs,
}
output := cmd.Flags().StringP("output", "o", "plain", "One of plain or json.")
jsonKey := cmd.Flags().StringP("key", "k", "", "Select a field to print if in json output mode.")
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
if *jsonKey != "" && *output != "json" {
return fnerrors.New("--key requires --output=json")
}
res, err := fnapi.GetTenant(ctx)
if err != nil {
return err
}
imgReg, err := api.GetImageRegistry(ctx, api.Methods)
if err != nil {
return err
}
stdout := console.Stdout(ctx)
switch *output {
case "json":
out := jsonOut{Tenant: res.Tenant}
if nscr := imgReg.NSCR; nscr != nil {
out.RegistryUrl = fmt.Sprintf("%s/%s", nscr.EndpointAddress, nscr.Repository)
}
if *jsonKey == "" {
d := json.NewEncoder(stdout)
d.SetIndent("", " ")
if err := d.Encode(out); err != nil {
return fnerrors.InternalError("failed to encode tenant as JSON output: %w", err)
}
return nil
}
data, err := json.Marshal(out)
if err != nil {
return fnerrors.InternalError("failed to encode tenant as JSON output: %w", err)
}
// XXX All selectable keys are strings for now.
// Parsing into string to make it obvious if this assumption ever breaks.
parsed := map[string]string{}
if err := json.Unmarshal(data, &parsed); err != nil {
return fnerrors.InternalError("failed to decode JSON: %w", err)
}
selected, ok := parsed[*jsonKey]
if !ok {
return fnerrors.New("selected json key %q not found in response", *jsonKey)
}
// As all selectable values are strings, we do not JSON marshal here, to keep
// the output easy to consume programatically (e.g. no quotation of plain strings).
fmt.Fprintf(stdout, "%v\n", selected)
default:
fmt.Fprintf(stdout, "\nWorkspace details:\n\n")
fmt.Fprintf(stdout, "Name: %s\n", res.Tenant.Name)
fmt.Fprintf(stdout, "Tenant ID: %s\n", res.Tenant.TenantId)
if nscr := imgReg.NSCR; nscr != nil {
fmt.Fprintf(stdout, "Registry URL: %s/%s\n", nscr.EndpointAddress, nscr.Repository)
}
}
return nil
})
return cmd
}
type jsonOut struct {
*fnapi.Tenant
RegistryUrl string `json:"registry_url,omitempty"`
}