Skip to content

Commit 0b564fc

Browse files
committed
refactor(cli): log success on create/update service-account
Match the majority of create commands (environment, flow, policy): instead of printing the response with --output table/json, create and update service-account now log a one-line success message. get and list keep --output (universal for those verbs), and delete already logs a message. The api-key commands keep --output where it makes sense (create api-key returns a secret to capture). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SWQ362qtJBoP3ncxRck9qu
1 parent 94102f5 commit 0b564fc

3 files changed

Lines changed: 18 additions & 34 deletions

File tree

cmd/kosli/createServiceAccount.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66
"net/url"
77

8-
"github.com/kosli-dev/cli/internal/output"
98
"github.com/kosli-dev/cli/internal/requests"
109
"github.com/spf13/cobra"
1110
)
@@ -27,7 +26,6 @@ kosli create service-account yourServiceAccountName \
2726
`
2827

2928
type createServiceAccountOptions struct {
30-
output string
3129
payload createServiceAccountPayload
3230
}
3331

@@ -53,13 +51,12 @@ func newCreateServiceAccountCmd(out io.Writer) *cobra.Command {
5351
return nil
5452
},
5553
RunE: func(cmd *cobra.Command, args []string) error {
56-
return o.run(out, args)
54+
return o.run(args)
5755
},
5856
}
5957

6058
cmd.Flags().StringVarP(&o.payload.Description, "description", "d", "", serviceAccountDescriptionFlag)
6159
cmd.Flags().StringVar(&o.payload.Privilege, "privilege", "", serviceAccountPrivilegeFlag)
62-
cmd.Flags().StringVarP(&o.output, "output", "o", "table", outputFlag)
6360
addDryRunFlag(cmd)
6461

6562
err := RequireFlags(cmd, []string{"privilege"})
@@ -70,7 +67,7 @@ func newCreateServiceAccountCmd(out io.Writer) *cobra.Command {
7067
return cmd
7168
}
7269

73-
func (o *createServiceAccountOptions) run(out io.Writer, args []string) error {
70+
func (o *createServiceAccountOptions) run(args []string) error {
7471
o.payload.Name = args[0]
7572
url, err := url.JoinPath(global.Host, "api/v2/service-accounts", global.Org)
7673
if err != nil {
@@ -84,14 +81,9 @@ func (o *createServiceAccountOptions) run(out io.Writer, args []string) error {
8481
DryRun: global.DryRun,
8582
Token: global.ApiToken,
8683
}
87-
response, err := kosliClient.Do(reqParams)
88-
if err != nil || global.DryRun {
89-
return err
84+
_, err = kosliClient.Do(reqParams)
85+
if err == nil && !global.DryRun {
86+
logger.Info("service account %s was created", o.payload.Name)
9087
}
91-
92-
return output.FormattedPrint(response.Body, o.output, out, 0,
93-
map[string]output.FormatOutputFunc{
94-
"table": printServiceAccountAsTable,
95-
"json": output.PrintJson,
96-
})
88+
return err
9789
}

cmd/kosli/serviceAccount_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ func (suite *ServiceAccountCommandTestSuite) TestServiceAccountSuccessOutput() {
214214
tests := []cmdTestCase{
215215
{
216216
wantError: false,
217-
name: "create prints the created service account",
218-
cmd: "create service-account ci-bot --privilege member --output json" + args,
219-
goldenRegex: `ci-bot`,
217+
name: "create reports the created service account",
218+
cmd: "create service-account ci-bot --privilege member" + args,
219+
goldenRegex: `service account ci-bot was created`,
220220
},
221221
{
222222
wantError: false,
@@ -232,9 +232,9 @@ func (suite *ServiceAccountCommandTestSuite) TestServiceAccountSuccessOutput() {
232232
},
233233
{
234234
wantError: false,
235-
name: "update prints the updated service account",
236-
cmd: "update service-account ci-bot --privilege admin --output json" + args,
237-
goldenRegex: `updated description`,
235+
name: "update reports the updated service account",
236+
cmd: "update service-account ci-bot --privilege admin" + args,
237+
goldenRegex: `service account ci-bot was updated`,
238238
},
239239
}
240240

cmd/kosli/updateServiceAccount.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"net/http"
66
"net/url"
77

8-
"github.com/kosli-dev/cli/internal/output"
98
"github.com/kosli-dev/cli/internal/requests"
109
"github.com/spf13/cobra"
1110
)
@@ -33,7 +32,6 @@ kosli update service-account yourServiceAccountName \
3332
type updateServiceAccountOptions struct {
3433
description string
3534
privilege string
36-
output string
3735
}
3836

3937
func newUpdateServiceAccountCmd(out io.Writer) *cobra.Command {
@@ -52,19 +50,18 @@ func newUpdateServiceAccountCmd(out io.Writer) *cobra.Command {
5250
return RequireAtLeastOneOfFlags(cmd, []string{"description", "privilege"})
5351
},
5452
RunE: func(cmd *cobra.Command, args []string) error {
55-
return o.run(out, cmd, args)
53+
return o.run(cmd, args)
5654
},
5755
}
5856

5957
cmd.Flags().StringVarP(&o.description, "description", "d", "", serviceAccountDescriptionFlag)
6058
cmd.Flags().StringVar(&o.privilege, "privilege", "", serviceAccountPrivilegeFlag)
61-
cmd.Flags().StringVarP(&o.output, "output", "o", "table", outputFlag)
6259
addDryRunFlag(cmd)
6360

6461
return cmd
6562
}
6663

67-
func (o *updateServiceAccountOptions) run(out io.Writer, cmd *cobra.Command, args []string) error {
64+
func (o *updateServiceAccountOptions) run(cmd *cobra.Command, args []string) error {
6865
url, err := url.JoinPath(global.Host, "api/v2/service-accounts", global.Org, args[0])
6966
if err != nil {
7067
return err
@@ -88,14 +85,9 @@ func (o *updateServiceAccountOptions) run(out io.Writer, cmd *cobra.Command, arg
8885
DryRun: global.DryRun,
8986
Token: global.ApiToken,
9087
}
91-
response, err := kosliClient.Do(reqParams)
92-
if err != nil || global.DryRun {
93-
return err
88+
_, err = kosliClient.Do(reqParams)
89+
if err == nil && !global.DryRun {
90+
logger.Info("service account %s was updated", args[0])
9491
}
95-
96-
return output.FormattedPrint(response.Body, o.output, out, 0,
97-
map[string]output.FormatOutputFunc{
98-
"table": printServiceAccountAsTable,
99-
"json": output.PrintJson,
100-
})
92+
return err
10193
}

0 commit comments

Comments
 (0)