-
Notifications
You must be signed in to change notification settings - Fork 42
/
list.go
107 lines (95 loc) · 3.23 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
// Copyright 2022 Upbound Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package token
import (
"context"
"fmt"
"time"
"github.com/alecthomas/kong"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/google/uuid"
"github.com/pterm/pterm"
"k8s.io/apimachinery/pkg/util/duration"
"github.com/upbound/up-sdk-go/service/accounts"
"github.com/upbound/up-sdk-go/service/common"
"github.com/upbound/up-sdk-go/service/organizations"
"github.com/upbound/up-sdk-go/service/robots"
"github.com/upbound/up/internal/upbound"
"github.com/upbound/up/internal/upterm"
)
var fieldNames = []string{"NAME", "ID", "CREATED"}
// AfterApply sets default values in command after assignment and validation.
func (c *listCmd) AfterApply(kongCtx *kong.Context, upCtx *upbound.Context) error {
kongCtx.Bind(pterm.DefaultTable.WithWriter(kongCtx.Stdout).WithSeparator(" "))
return nil
}
// listCmd creates a robot on Upbound.
type listCmd struct {
RobotName string `arg:"" required:"" help:"Name of robot." predictor:"robots"`
}
// Run executes the list robot tokens command.
func (c *listCmd) Run(ctx context.Context, printer upterm.ObjectPrinter, p pterm.TextPrinter, ac *accounts.Client, oc *organizations.Client, rc *robots.Client, upCtx *upbound.Context) error { //nolint:gocyclo
a, err := ac.Get(ctx, upCtx.Account)
if err != nil {
return err
}
if a.Account.Type != accounts.AccountOrganization {
return errors.New(errUserAccount)
}
rs, err := oc.ListRobots(ctx, a.Organization.ID)
if err != nil {
return err
}
if len(rs) == 0 {
return errors.Errorf(errFindRobotFmt, c.RobotName, upCtx.Account)
}
// TODO(hasheddan): because this API does not guarantee name uniqueness, we
// must guarantee that exactly one robot exists in the specified account
// with the provided name. Logic should be simplified when the API is
// updated.
var rid *uuid.UUID
for _, r := range rs {
if r.Name == c.RobotName {
if rid != nil {
return errors.Errorf(errMultipleRobotFmt, c.RobotName, upCtx.Account)
}
// Pin range variable so that we can take address.
r := r
rid = &r.ID
}
}
if rid == nil {
return errors.Errorf(errFindRobotFmt, c.RobotName, upCtx.Account)
}
ts, err := rc.ListTokens(ctx, *rid)
if err != nil {
return err
}
if len(ts.DataSet) == 0 {
p.Printfln("No tokens found for robot %s in %s", c.RobotName, upCtx.Account)
return nil
}
return printer.Print(ts.DataSet, fieldNames, extractFields)
}
func extractFields(obj any) []string {
t := obj.(common.DataSet)
n := fmt.Sprint(t.AttributeSet["name"])
c := "n/a"
if ca, ok := t.Meta["createdAt"]; ok {
if ct, err := time.Parse(time.RFC3339, fmt.Sprint(ca)); err == nil {
c = duration.HumanDuration(time.Since(ct))
}
}
return []string{n, t.ID.String(), c}
}