/
history.go
145 lines (118 loc) · 3.72 KB
/
history.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
// 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 cluster
import (
"context"
"encoding/json"
"time"
"github.com/spf13/cobra"
"namespacelabs.dev/foundation/internal/cli/fncobra"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/providers/nscloud/api"
)
func NewListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Short: "Lists all of your instances.",
Args: cobra.NoArgs,
}
output := cmd.Flags().StringP("output", "o", "plain", "One of plain or json.")
labels := cmd.Flags().StringToString("label", nil, "Constrain list to the specified labels.")
all := cmd.Flags().Bool("all", false, "If true, returl all instances, not just manually created ones.")
cmd.Flags().MarkHidden("label")
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
history := false
clusters, err := api.ListClusters(ctx, api.Methods, api.ListOpts{
PreviousRuns: history,
Labels: *labels,
All: *all,
})
if err != nil {
return err
}
if *output == "json" {
stdout := console.Stdout(ctx)
enc := json.NewEncoder(stdout)
enc.SetIndent("", " ")
if err := enc.Encode(transform(clusters.Clusters)); err != nil {
return fnerrors.InternalError("failed to encode instance list as JSON output: %w", err)
}
return nil
}
if len(clusters.Clusters) == 0 {
PrintCreateClusterMsg(ctx)
return nil
}
return staticTableClusters(ctx, clusters.Clusters, history)
})
return cmd
}
func transform(mds []api.KubernetesClusterMetadata) []map[string]any {
var x []map[string]any
for _, md := range mds {
x = append(x, transformForOutput(md))
}
return x
}
// We keep a validated set of output fields which should not change.
func transformForOutput(md api.KubernetesClusterMetadata) map[string]any {
m := map[string]any{
"cluster_id": md.ClusterId,
"created_at": md.Created,
"shape": md.Shape,
"ingress_domain": md.IngressDomain,
"labels": md.UserLabel,
}
if md.DestroyedAt != "" {
m["destroyed_at"] = md.DestroyedAt
}
if len(md.Platform) == 1 {
m["platform"] = md.Platform[0]
}
if md.CreatorId != "" {
m["creator_id"] = md.CreatorId
}
return m
}
func newHistoryCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "history",
Short: "History of your previous running instances.",
Args: cobra.NoArgs,
}
output := cmd.Flags().StringP("output", "o", "plain", "One of plain or json.")
labels := cmd.Flags().StringToString("label", nil, "Constrain list to the specified labels.")
since := cmd.Flags().Duration("since", time.Hour*24*7, "Contrain list to selected duration.")
all := cmd.Flags().Bool("all", false, "If true, returl all instances, not just manually created ones.")
cmd.Flags().MarkHidden("label")
cmd.RunE = fncobra.RunE(func(ctx context.Context, args []string) error {
history := true
startTs := time.Now().Add(-*since)
clusters, err := api.ListClusters(ctx, api.Methods, api.ListOpts{
PreviousRuns: history,
NotOlderThan: &startTs,
Labels: *labels,
All: *all,
})
if err != nil {
return err
}
if *output == "json" {
stdout := console.Stdout(ctx)
enc := json.NewEncoder(stdout)
enc.SetIndent("", " ")
if err := enc.Encode(clusters.Clusters); err != nil {
return fnerrors.InternalError("failed to encode instance history as JSON output: %w", err)
}
return nil
}
if len(clusters.Clusters) == 0 {
PrintCreateClusterMsg(ctx)
return nil
}
return staticTableClusters(ctx, clusters.Clusters, history)
})
return cmd
}