Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions internal/cmd/service-account/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("create service account: %w", err)
}

return outputResult(p, model, projectLabel, resp)
return outputResult(p, model.OutputFormat, projectLabel, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -119,8 +119,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco
return req
}

func outputResult(p *print.Printer, model *inputModel, projectLabel string, serviceAccount *serviceaccount.ServiceAccount) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat, projectLabel string, serviceAccount *serviceaccount.ServiceAccount) error {
if serviceAccount == nil {
return fmt.Errorf("service account is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(serviceAccount, "", " ")
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions internal/cmd/service-account/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,38 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
projectLabel string
serviceAccount *serviceaccount.ServiceAccount
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "empty service account",
args: args{
serviceAccount: &serviceaccount.ServiceAccount{},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.serviceAccount); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
40 changes: 40 additions & 0 deletions internal/cmd/service-account/get-jwks/get_jwks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,43 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
serviceAccounts []serviceaccount.JWK
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "empty service accounts slice",
args: args{
serviceAccounts: []serviceaccount.JWK{},
},
wantErr: false,
},
{
name: "empty service account in service accounts slice",
args: args{
serviceAccounts: []serviceaccount.JWK{{}},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.serviceAccounts); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
4 changes: 4 additions & 0 deletions internal/cmd/service-account/key/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco
}

func outputResult(p *print.Printer, key *serviceaccount.GetServiceAccountKeyResponse) error {
if key == nil {
return fmt.Errorf("key is nil")
}

marshaledKey, err := json.MarshalIndent(key, "", " ")
if err != nil {
return fmt.Errorf("marshal service account key: %w", err)
Expand Down
33 changes: 33 additions & 0 deletions internal/cmd/service-account/key/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,36 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
key *serviceaccount.GetServiceAccountKeyResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "empty key",
args: args{
key: &serviceaccount.GetServiceAccountKeyResponse{},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.key); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
41 changes: 41 additions & 0 deletions internal/cmd/service-account/key/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,44 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
keys []serviceaccount.ServiceAccountKeyListResponse
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "empty keys slice",
args: args{
keys: []serviceaccount.ServiceAccountKeyListResponse{},
},
wantErr: false,
},
{
name: "empty key in keys slice",
args: args{
keys: []serviceaccount.ServiceAccountKeyListResponse{{}},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.keys); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
41 changes: 41 additions & 0 deletions internal/cmd/service-account/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,44 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
serviceAccounts []serviceaccount.ServiceAccount
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "empty service accounts slice",
args: args{
serviceAccounts: []serviceaccount.ServiceAccount{},
},
wantErr: false,
},
{
name: "empty service account in service accounts slice",
args: args{
serviceAccounts: []serviceaccount.ServiceAccount{{}},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.serviceAccounts); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
12 changes: 8 additions & 4 deletions internal/cmd/service-account/token/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func NewCmd(p *print.Printer) *cobra.Command {
return fmt.Errorf("create access token: %w", err)
}

return outputResult(p, model, token)
return outputResult(p, model.OutputFormat, model.ServiceAccountEmail, token)
},
}

Expand Down Expand Up @@ -142,8 +142,12 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *serviceacco
return req
}

func outputResult(p *print.Printer, model *inputModel, token *serviceaccount.AccessToken) error {
switch model.OutputFormat {
func outputResult(p *print.Printer, outputFormat, serviceAccountEmail string, token *serviceaccount.AccessToken) error {
if token == nil {
return fmt.Errorf("token is nil")
}

switch outputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(token, "", " ")
if err != nil {
Expand All @@ -161,7 +165,7 @@ func outputResult(p *print.Printer, model *inputModel, token *serviceaccount.Acc

return nil
default:
p.Outputf("Created access token for service account %s. Token ID: %s\n\n", model.ServiceAccountEmail, utils.PtrString(token.Id))
p.Outputf("Created access token for service account %s. Token ID: %s\n\n", serviceAccountEmail, utils.PtrString(token.Id))
p.Outputf("Valid until: %s\n", utils.PtrString(token.ValidUntil))
p.Outputf("Token: %s\n", utils.PtrString(token.Token))
return nil
Expand Down
35 changes: 35 additions & 0 deletions internal/cmd/service-account/token/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,38 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
serviceAccountEmail string
token *serviceaccount.AccessToken
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: true,
},
{
name: "empty token",
args: args{
token: &serviceaccount.AccessToken{},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.serviceAccountEmail, tt.args.token); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
41 changes: 41 additions & 0 deletions internal/cmd/service-account/token/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,44 @@ func TestBuildRequest(t *testing.T) {
})
}
}

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
tokensMetadata []serviceaccount.AccessTokenMetadata
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "empty",
args: args{},
wantErr: false,
},
{
name: "empty tokens metadata slice",
args: args{
tokensMetadata: []serviceaccount.AccessTokenMetadata{},
},
wantErr: false,
},
{
name: "empty token metadata in tokens metadata slice",
args: args{
tokensMetadata: []serviceaccount.AccessTokenMetadata{{}},
},
wantErr: false,
},
}
p := print.NewPrinter()
p.Cmd = NewCmd(p)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.tokensMetadata); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
Loading