Skip to content

Commit 9399e8b

Browse files
authored
Stackit git: add vertical table on describe and limit list command
1 parent d368ead commit 9399e8b

File tree

9 files changed

+93
-22
lines changed

9 files changed

+93
-22
lines changed

docs/stackit_git_create.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Creates STACKIT Git instance
44

55
### Synopsis
66

7-
Create an STACKIT Git instance by name.
7+
Create a STACKIT Git instance by name.
88

99
```
1010
stackit git create [flags]
@@ -13,7 +13,7 @@ stackit git create [flags]
1313
### Examples
1414

1515
```
16-
Create an instance with name 'my-new-instance'
16+
Create a instance with name 'my-new-instance'
1717
$ stackit git create --name my-new-instance
1818
```
1919

docs/stackit_git_delete.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Deletes STACKIT Git instance
44

55
### Synopsis
66

7-
Deletes an STACKIT Git instance by its internal ID.
7+
Deletes a STACKIT Git instance by its internal ID.
88

99
```
1010
stackit git delete INSTANCE_ID [flags]
@@ -13,7 +13,7 @@ stackit git delete INSTANCE_ID [flags]
1313
### Examples
1414

1515
```
16-
Delete an instance with ID "xxx"
16+
Delete a instance with ID "xxx"
1717
$ stackit git delete xxx
1818
```
1919

docs/stackit_git_describe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Describes STACKIT Git instance
44

55
### Synopsis
66

7-
Describes an STACKIT Git instance by its internal ID.
7+
Describes a STACKIT Git instance by its internal ID.
88

99
```
1010
stackit git describe INSTANCE_ID [flags]

docs/stackit_git_list.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,16 @@ stackit git list [flags]
1515
```
1616
List all STACKIT Git instances
1717
$ stackit git instance list
18+
19+
Lists up to 10 STACKIT Git instances
20+
$ stackit git instance list --limit=10
1821
```
1922

2023
### Options
2124

2225
```
23-
-h, --help Help for "stackit git list"
26+
-h, --help Help for "stackit git list"
27+
--limit int Limit the output to the first n elements
2428
```
2529

2630
### Options inherited from parent commands

internal/cmd/git/create/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ func NewCmd(p *print.Printer) *cobra.Command {
3434
cmd := &cobra.Command{
3535
Use: "create",
3636
Short: "Creates STACKIT Git instance",
37-
Long: "Create an STACKIT Git instance by name.",
37+
Long: "Create a STACKIT Git instance by name.",
3838
Args: args.NoArgs,
3939
Example: examples.Build(
4040
examples.NewExample(
41-
`Create an instance with name 'my-new-instance'`,
41+
`Create a instance with name 'my-new-instance'`,
4242
`$ stackit git create --name my-new-instance`,
4343
),
4444
),

internal/cmd/git/delete/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ func NewCmd(p *print.Printer) *cobra.Command {
3030
cmd := &cobra.Command{
3131
Use: fmt.Sprintf("delete %s", instanceIdArg),
3232
Short: "Deletes STACKIT Git instance",
33-
Long: "Deletes an STACKIT Git instance by its internal ID.",
33+
Long: "Deletes a STACKIT Git instance by its internal ID.",
3434
Args: args.SingleArg(instanceIdArg, utils.ValidateUUID),
3535
Example: examples.Build(
36-
examples.NewExample(`Delete an instance with ID "xxx"`, `$ stackit git delete xxx`),
36+
examples.NewExample(`Delete a instance with ID "xxx"`, `$ stackit git delete xxx`),
3737
),
3838
RunE: func(cmd *cobra.Command, args []string) error {
3939
ctx := context.Background()

internal/cmd/git/describe/describe.go

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
2929
cmd := &cobra.Command{
3030
Use: fmt.Sprintf("describe %s", instanceIdArg),
3131
Short: "Describes STACKIT Git instance",
32-
Long: "Describes an STACKIT Git instance by its internal ID.",
32+
Long: "Describes a STACKIT Git instance by its internal ID.",
3333
Args: args.SingleArg(instanceIdArg, utils.ValidateUUID),
3434
Example: examples.Build(
3535
examples.NewExample(`Describe instance "xxx"`, `$ stackit git describe xxx`),
@@ -116,15 +116,30 @@ func outputResult(p *print.Printer, outputFormat string, resp *git.Instance) err
116116
return nil
117117
default:
118118
table := tables.NewTable()
119-
table.SetHeader("ID", "NAME", "URL", "VERSION", "STATE", "CREATED")
120-
table.AddRow(
121-
utils.PtrString(resp.Id),
122-
utils.PtrString(resp.Name),
123-
utils.PtrString(resp.Url),
124-
utils.PtrString(resp.Version),
125-
utils.PtrString(resp.State),
126-
utils.PtrString(resp.Created),
127-
)
119+
if id := resp.Id; id != nil {
120+
table.AddRow("ID", *id)
121+
table.AddSeparator()
122+
}
123+
if name := resp.Name; name != nil {
124+
table.AddRow("NAME", *name)
125+
table.AddSeparator()
126+
}
127+
if url := resp.Url; url != nil {
128+
table.AddRow("URL", *url)
129+
table.AddSeparator()
130+
}
131+
if version := resp.Version; version != nil {
132+
table.AddRow("VERSION", *version)
133+
table.AddSeparator()
134+
}
135+
if state := resp.State; state != nil {
136+
table.AddRow("STATE", *state)
137+
table.AddSeparator()
138+
}
139+
if created := resp.Created; created != nil {
140+
table.AddRow("CREATED", *created)
141+
table.AddSeparator()
142+
}
128143

129144
if err := table.Display(p); err != nil {
130145
return fmt.Errorf("render table: %w", err)

internal/cmd/git/list/list.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1111
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1212
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
1314
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1415
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1516
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
@@ -21,8 +22,11 @@ import (
2122

2223
type inputModel struct {
2324
*globalflags.GlobalFlagModel
25+
Limit *int64
2426
}
2527

28+
const limitFlag = "limit"
29+
2630
func NewCmd(p *print.Printer) *cobra.Command {
2731
cmd := &cobra.Command{
2832
Use: "list",
@@ -33,6 +37,10 @@ func NewCmd(p *print.Printer) *cobra.Command {
3337
examples.NewExample(
3438
`List all STACKIT Git instances`,
3539
"$ stackit git instance list"),
40+
examples.NewExample(
41+
"Lists up to 10 STACKIT Git instances",
42+
"$ stackit git instance list --limit=10",
43+
),
3644
),
3745
RunE: func(cmd *cobra.Command, _ []string) error {
3846
ctx := context.Background()
@@ -62,23 +70,37 @@ func NewCmd(p *print.Printer) *cobra.Command {
6270
}
6371
p.Info("No instances found for project %q\n", projectLabel)
6472
return nil
73+
} else if model.Limit != nil && len(instances) > int(*model.Limit) {
74+
instances = (instances)[:*model.Limit]
6575
}
66-
6776
return outputResult(p, model.OutputFormat, instances)
6877
},
6978
}
70-
79+
configureFlags(cmd)
7180
return cmd
7281
}
7382

83+
func configureFlags(cmd *cobra.Command) {
84+
cmd.Flags().Int64(limitFlag, 0, "Limit the output to the first n elements")
85+
}
86+
7487
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
7588
globalFlags := globalflags.Parse(p, cmd)
7689
if globalFlags.ProjectId == "" {
7790
return nil, &errors.ProjectIdError{}
7891
}
7992

93+
limit := flags.FlagToInt64Pointer(p, cmd, limitFlag)
94+
if limit != nil && *limit < 1 {
95+
return nil, &errors.FlagValidationError{
96+
Flag: limitFlag,
97+
Details: "must be greater than 0",
98+
}
99+
}
100+
80101
model := inputModel{
81102
GlobalFlagModel: globalFlags,
103+
Limit: limit,
82104
}
83105

84106
if p.IsVerbosityDebug() {

internal/cmd/git/list/list_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package list
22

33
import (
44
"context"
5+
"strconv"
56
"testing"
67

78
"github.com/google/go-cmp/cmp"
89
"github.com/google/go-cmp/cmp/cmpopts"
910
"github.com/google/uuid"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1214
"github.com/stackitcloud/stackit-sdk-go/services/git"
1315
)
1416

@@ -18,6 +20,10 @@ var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
1820
var testClient = &git.APIClient{}
1921
var testProjectId = uuid.NewString()
2022

23+
const (
24+
testLimit = 10
25+
)
26+
2127
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
2228
flagValues := map[string]string{
2329
globalflags.ProjectIdFlag: testProjectId,
@@ -88,6 +94,30 @@ func TestParseInput(t *testing.T) {
8894
}),
8995
isValid: false,
9096
},
97+
{
98+
description: "with limit flag",
99+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
100+
flagValues["limit"] = strconv.Itoa(testLimit)
101+
}),
102+
isValid: true,
103+
expectedModel: fixtureInputModel(func(model *inputModel) {
104+
model.Limit = utils.Ptr(int64(testLimit))
105+
}),
106+
},
107+
{
108+
description: "with limit flag == 0",
109+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
110+
flagValues["limit"] = strconv.Itoa(0)
111+
}),
112+
isValid: false,
113+
},
114+
{
115+
description: "with limit flag < 0",
116+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
117+
flagValues["limit"] = strconv.Itoa(-1)
118+
}),
119+
isValid: false,
120+
},
91121
}
92122

93123
for _, tt := range tests {

0 commit comments

Comments
 (0)