Skip to content

Commit 3b3f6aa

Browse files
committed
Merge branch 'size-services' of https://github.com/metal-stack/cli into size-services
2 parents 5daa557 + 51efe0f commit 3b3f6aa

7 files changed

Lines changed: 195 additions & 11 deletions

File tree

cmd/admin/v2/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func AddCmds(cmd *cobra.Command, c *config.Config) {
2323
adminCmd.AddCommand(newProjectCmd(c))
2424
adminCmd.AddCommand(newVPNCmd(c))
2525
adminCmd.AddCommand(newMachineCmd(c))
26+
adminCmd.AddCommand(newTaskCmd(c))
2627

2728
cmd.AddCommand(adminCmd)
2829
}

cmd/admin/v2/task.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package v2
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
8+
"github.com/metal-stack/cli/cmd/config"
9+
"github.com/metal-stack/metal-lib/pkg/genericcli"
10+
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
11+
"github.com/metal-stack/metal-lib/pkg/pointer"
12+
"github.com/spf13/cobra"
13+
"github.com/spf13/viper"
14+
)
15+
16+
type task struct {
17+
c *config.Config
18+
}
19+
20+
func newTaskCmd(c *config.Config) *cobra.Command {
21+
w := &task{
22+
c: c,
23+
}
24+
25+
cmdsConfig := &genericcli.CmdsConfig[any, any, *adminv2.TaskInfo]{
26+
BinaryName: config.BinaryName,
27+
GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs),
28+
Singular: "task",
29+
Plural: "tasks",
30+
Description: "get task insights",
31+
DescribePrinter: func() printers.Printer { return c.DescribePrinter },
32+
ListPrinter: func() printers.Printer { return c.ListPrinter },
33+
DescribeCmdMutateFn: func(cmd *cobra.Command) {
34+
cmd.Flags().String("queue", "default", "the queue for which tasks should be described")
35+
},
36+
ListCmdMutateFn: func(cmd *cobra.Command) {
37+
cmd.Flags().String("queue", "", "the queue for which tasks should be listed")
38+
},
39+
DeleteCmdMutateFn: func(cmd *cobra.Command) {
40+
cmd.Flags().String("queue", "default", "the queue of the task which should be delete")
41+
},
42+
OnlyCmds: genericcli.OnlyCmds(genericcli.ListCmd, genericcli.DescribeCmd, genericcli.DeleteCmd),
43+
}
44+
45+
queueCmd := &cobra.Command{
46+
Use: "queues",
47+
Short: "list all queues",
48+
RunE: func(cmd *cobra.Command, args []string) error {
49+
return w.queues()
50+
},
51+
}
52+
53+
return genericcli.NewCmds(cmdsConfig, queueCmd)
54+
}
55+
56+
func (t *task) queues() error {
57+
ctx, cancel := t.c.NewRequestContext()
58+
defer cancel()
59+
60+
req := &adminv2.TaskServiceQueuesRequest{}
61+
62+
resp, err := t.c.Client.Adminv2().Task().Queues(ctx, req)
63+
if err != nil {
64+
return fmt.Errorf("failed to get task queues: %w", err)
65+
}
66+
67+
_, _ = fmt.Fprint(t.c.Out, strings.Join(resp.Queues, "\n"))
68+
_, _ = fmt.Fprint(t.c.Out, "\n")
69+
return err
70+
}
71+
72+
func (t *task) Get(id string) (*adminv2.TaskInfo, error) {
73+
ctx, cancel := t.c.NewRequestContext()
74+
defer cancel()
75+
76+
req := &adminv2.TaskServiceGetRequest{TaskId: id, Queue: viper.GetString("queue")}
77+
78+
resp, err := t.c.Client.Adminv2().Task().Get(ctx, req)
79+
if err != nil {
80+
return nil, fmt.Errorf("failed to get task: %w", err)
81+
}
82+
83+
return resp.Task, nil
84+
}
85+
func (t *task) List() ([]*adminv2.TaskInfo, error) {
86+
ctx, cancel := t.c.NewRequestContext()
87+
defer cancel()
88+
89+
req := &adminv2.TaskServiceListRequest{}
90+
if viper.IsSet("queue") {
91+
req.Queue = pointer.Pointer(viper.GetString("queue"))
92+
}
93+
94+
resp, err := t.c.Client.Adminv2().Task().List(ctx, req)
95+
if err != nil {
96+
return nil, fmt.Errorf("failed to list tasks: %w", err)
97+
}
98+
99+
return resp.Tasks, nil
100+
}
101+
102+
func (t *task) Create(rq any) (*adminv2.TaskInfo, error) {
103+
panic("unimplemented")
104+
}
105+
106+
func (t *task) Delete(id string) (*adminv2.TaskInfo, error) {
107+
ctx, cancel := t.c.NewRequestContext()
108+
defer cancel()
109+
110+
req := &adminv2.TaskServiceDeleteRequest{TaskId: id, Queue: viper.GetString("queue")}
111+
112+
_, err := t.c.Client.Adminv2().Task().Delete(ctx, req)
113+
if err != nil {
114+
return nil, fmt.Errorf("failed to get task: %w", err)
115+
}
116+
117+
return nil, nil
118+
}
119+
120+
func (t *task) Convert(r *adminv2.TaskInfo) (string, any, any, error) {
121+
panic("unimplemented")
122+
}
123+
124+
func (t *task) Update(rq any) (*adminv2.TaskInfo, error) {
125+
panic("unimplemented")
126+
}

cmd/admin/v2/vpn.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (v *vpn) authKey() error {
7070
return err
7171
}
7272

73-
_, _ = fmt.Fprintf(v.c.Out, "authkey: %s ephemeral:%s created at:%s expires at:%s\n", resp.AuthKey, resp.Ephemeral, resp.CreatedAt, resp.ExpiresAt)
73+
_, _ = fmt.Fprintf(v.c.Out, "authkey: %s ephemeral:%t created at:%s expires at:%s\n", resp.AuthKey, resp.Ephemeral, resp.CreatedAt, resp.ExpiresAt)
7474
_, _ = fmt.Fprintf(v.c.Out, "vpn endpoint: %s\n", resp.Address)
7575

7676
return nil

cmd/tableprinters/common.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"time"
88

9+
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
910
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
1011
"github.com/metal-stack/cli/cmd/config"
1112
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
@@ -82,6 +83,11 @@ func (t *TablePrinter) ToHeaderAndRows(data any, wide bool) ([]string, [][]strin
8283
case []*apiv2.ProjectMember:
8384
return t.ProjectMemberTable(d, wide)
8485

86+
case *adminv2.TaskInfo:
87+
return t.TaskTable(pointer.WrapInSlice(d), wide)
88+
case []*adminv2.TaskInfo:
89+
return t.TaskTable(d, wide)
90+
8591
case *apiv2.Token:
8692
return t.TokenTable(pointer.WrapInSlice(d), wide)
8793
case []*apiv2.Token:

cmd/tableprinters/task.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package tableprinters
2+
3+
import (
4+
"time"
5+
6+
"github.com/google/uuid"
7+
"github.com/metal-stack/api/go/enum"
8+
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
9+
"github.com/metal-stack/metal-lib/pkg/pointer"
10+
)
11+
12+
func (t *TablePrinter) TaskTable(data []*adminv2.TaskInfo, wide bool) ([]string, [][]string, error) {
13+
var (
14+
rows [][]string
15+
)
16+
header := []string{"ID", "Queue", "When", "Type", "State"}
17+
18+
if wide {
19+
header = []string{"ID", "Queue", "When", "Type", "State", "Issued At", "Payload", "Result"}
20+
}
21+
for _, task := range data {
22+
id := task.Id
23+
queue := task.Queue
24+
typeString := task.Type
25+
state, err := enum.GetStringValue(task.State)
26+
if err != nil {
27+
state = pointer.Pointer("unknown")
28+
}
29+
payload := string(task.Payload)
30+
result := string(task.Result)
31+
32+
parsed, err := uuid.Parse(id)
33+
if err != nil {
34+
return nil, nil, err
35+
}
36+
37+
sec, nano := parsed.Time().UnixTime()
38+
issuedAt := time.Unix(sec, nano)
39+
when := humanizeDuration(time.Since(issuedAt))
40+
41+
if wide {
42+
rows = append(rows, []string{id, queue, when, typeString, *state, issuedAt.String(), payload, result})
43+
} else {
44+
rows = append(rows, []string{id, queue, when, typeString, *state})
45+
}
46+
}
47+
48+
t.t.DisableAutoWrap(false)
49+
50+
return header, rows, nil
51+
}

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ require (
66
github.com/dustin/go-humanize v1.0.1
77
github.com/fatih/color v1.18.0
88
github.com/google/go-cmp v0.7.0
9-
github.com/metal-stack/api v0.0.41-0.20260125160643-6b22ee77f479
9+
github.com/google/uuid v1.6.0
10+
github.com/metal-stack/api v0.0.41-0.20260128065401-f4165348f8b5
1011
github.com/metal-stack/metal-lib v0.23.5
1112
github.com/metal-stack/v v1.0.3
1213
github.com/spf13/afero v1.15.0
@@ -34,7 +35,6 @@ require (
3435
github.com/go-viper/mapstructure/v2 v2.5.0 // indirect
3536
github.com/goccy/go-yaml v1.19.2 // indirect
3637
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
37-
github.com/google/uuid v1.6.0 // indirect
3838
github.com/inconshreveable/mousetrap v1.1.0 // indirect
3939
github.com/klauspost/compress v1.18.3 // indirect
4040
github.com/klauspost/connect-compress/v2 v2.1.1 // indirect
@@ -45,7 +45,7 @@ require (
4545
github.com/oklog/ulid v1.3.1 // indirect
4646
github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 // indirect
4747
github.com/olekukonko/errors v1.2.0 // indirect
48-
github.com/olekukonko/ll v0.1.4-0.20260115111900-9e59c2286df0 // indirect
48+
github.com/olekukonko/ll v0.1.4 // indirect
4949
github.com/olekukonko/tablewriter v1.1.3 // indirect
5050
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
5151
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
@@ -60,7 +60,7 @@ require (
6060
golang.org/x/net v0.49.0 // indirect
6161
golang.org/x/sys v0.40.0 // indirect
6262
golang.org/x/text v0.33.0 // indirect
63-
google.golang.org/genproto/googleapis/rpc v0.0.0-20260122232226-8e98ce8d340d // indirect
63+
google.golang.org/genproto/googleapis/rpc v0.0.0-20260128011058-8636f8732409 // indirect
6464
gopkg.in/inf.v0 v0.9.1 // indirect
6565
gopkg.in/yaml.v3 v3.0.1 // indirect
6666
k8s.io/apimachinery v0.35.0 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
5959
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
6060
github.com/mattn/go-runewidth v0.0.19 h1:v++JhqYnZuu5jSKrk9RbgF5v4CGUjqRfBm05byFGLdw=
6161
github.com/mattn/go-runewidth v0.0.19/go.mod h1:XBkDxAl56ILZc9knddidhrOlY5R/pDhgLpndooCuJAs=
62-
github.com/metal-stack/api v0.0.41-0.20260125160643-6b22ee77f479 h1:m5+3w37cOiBJBED1O6mXLsc/D7lrroguTjt+c/0ubmI=
63-
github.com/metal-stack/api v0.0.41-0.20260125160643-6b22ee77f479/go.mod h1:Vim6qn4+WOCYH4iH+XnMpaobYkdZtgplKOSfFK/775w=
62+
github.com/metal-stack/api v0.0.41-0.20260128065401-f4165348f8b5 h1:+VxqCp/g5CSDunNUOoAwe+JXJL4tzyApqoGyq9eaPtU=
63+
github.com/metal-stack/api v0.0.41-0.20260128065401-f4165348f8b5/go.mod h1:Vim6qn4+WOCYH4iH+XnMpaobYkdZtgplKOSfFK/775w=
6464
github.com/metal-stack/metal-lib v0.23.5 h1:ozrkB3DNr3Cqn8nkBvmzc/KKpYqC1j1mv2OVOj8i7Ac=
6565
github.com/metal-stack/metal-lib v0.23.5/go.mod h1:7uyHIrE19dkLwCZyeh2jmd7IEq5pEpzrzUGLoMN1eqY=
6666
github.com/metal-stack/v v1.0.3 h1:Sh2oBlnxrCUD+mVpzfC8HiqL045YWkxs0gpTvkjppqs=
@@ -73,8 +73,8 @@ github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6 h1:zrbMGy9YXpIeTnGj
7373
github.com/olekukonko/cat v0.0.0-20250911104152-50322a0618f6/go.mod h1:rEKTHC9roVVicUIfZK7DYrdIoM0EOr8mK1Hj5s3JjH0=
7474
github.com/olekukonko/errors v1.2.0 h1:10Zcn4GeV59t/EGqJc8fUjtFT/FuUh5bTMzZ1XwmCRo=
7575
github.com/olekukonko/errors v1.2.0/go.mod h1:ppzxA5jBKcO1vIpCXQ9ZqgDh8iwODz6OXIGKU8r5m4Y=
76-
github.com/olekukonko/ll v0.1.4-0.20260115111900-9e59c2286df0 h1:jrYnow5+hy3WRDCBypUFvVKNSPPCdqgSXIE9eJDD8LM=
77-
github.com/olekukonko/ll v0.1.4-0.20260115111900-9e59c2286df0/go.mod h1:b52bVQRRPObe+yyBl0TxNfhesL0nedD4Cht0/zx55Ew=
76+
github.com/olekukonko/ll v0.1.4 h1:QcDaO9quz213xqHZr0gElOcYeOSnFeq7HTQ9Wu4O1wE=
77+
github.com/olekukonko/ll v0.1.4/go.mod h1:b52bVQRRPObe+yyBl0TxNfhesL0nedD4Cht0/zx55Ew=
7878
github.com/olekukonko/tablewriter v1.1.3 h1:VSHhghXxrP0JHl+0NnKid7WoEmd9/urKRJLysb70nnA=
7979
github.com/olekukonko/tablewriter v1.1.3/go.mod h1:9VU0knjhmMkXjnMKrZ3+L2JhhtsQ/L38BbL3CRNE8tM=
8080
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
@@ -117,8 +117,8 @@ golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ=
117117
golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
118118
golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
119119
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
120-
google.golang.org/genproto/googleapis/rpc v0.0.0-20260122232226-8e98ce8d340d h1:xXzuihhT3gL/ntduUZwHECzAn57E8dA6l8SOtYWdD8Q=
121-
google.golang.org/genproto/googleapis/rpc v0.0.0-20260122232226-8e98ce8d340d/go.mod h1:j9x/tPzZkyxcgEFkiKEEGxfvyumM01BEtsW8xzOahRQ=
120+
google.golang.org/genproto/googleapis/rpc v0.0.0-20260128011058-8636f8732409 h1:H86B94AW+VfJWDqFeEbBPhEtHzJwJfTbgE2lZa54ZAQ=
121+
google.golang.org/genproto/googleapis/rpc v0.0.0-20260128011058-8636f8732409/go.mod h1:j9x/tPzZkyxcgEFkiKEEGxfvyumM01BEtsW8xzOahRQ=
122122
google.golang.org/grpc v1.78.0 h1:K1XZG/yGDJnzMdd/uZHAkVqJE+xIDOcmdSFZkBUicNc=
123123
google.golang.org/grpc v1.78.0/go.mod h1:I47qjTo4OKbMkjA/aOOwxDIiPSBofUtQUI5EfpWvW7U=
124124
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=

0 commit comments

Comments
 (0)