Skip to content

Commit e081d07

Browse files
authored
Add debug logs to the CLI (#258)
* Initial debug implementation * improve implementation * remove unused code * add documentation * address PR comments * fix linting * sort map keys before building debug string * Add testing to debug string builders * address PR comments * improve debug string construction * Debug the input model on all commands * add input model debugging to contribution guidelines
1 parent de64cdf commit e081d07

File tree

332 files changed

+3327
-646
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

332 files changed

+3327
-646
lines changed

CONTRIBUTION.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,23 @@ func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
138138
return nil, &errors.ProjectIdError{}
139139
}
140140

141-
return &inputModel{
141+
model := inputModel{
142142
GlobalFlagModel: globalFlags,
143143
MyArg myArg,
144144
MyFlag: flags.FlagToStringPointer(cmd, myFlag),
145145
}, nil
146+
147+
// Write the input model to the debug logs
148+
if p.IsVerbosityDebug() {
149+
modelStr, err := print.BuildDebugStrFromInputModel(model)
150+
if err != nil {
151+
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
152+
} else {
153+
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
154+
}
155+
}
156+
157+
return &model, nil
146158
}
147159

148160
// Build request to the API

internal/cmd/argus/grafana/describe/describe.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ func NewCmd(p *print.Printer) *cobra.Command {
5454
RunE: func(cmd *cobra.Command, args []string) error {
5555
ctx := context.Background()
5656
model, err := parseInput(p, cmd, args)
57-
5857
if err != nil {
5958
return err
6059
}
60+
6161
// Configure API client
6262
apiClient, err := client.ConfigureClient(p)
6363
if err != nil {
@@ -95,11 +95,22 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
9595
return nil, &errors.ProjectIdError{}
9696
}
9797

98-
return &inputModel{
98+
model := inputModel{
9999
GlobalFlagModel: globalFlags,
100100
InstanceId: instanceId,
101101
HidePassword: flags.FlagToBoolValue(p, cmd, hidePasswordFlag),
102-
}, nil
102+
}
103+
104+
if p.IsVerbosityDebug() {
105+
modelStr, err := print.BuildDebugStrFromInputModel(model)
106+
if err != nil {
107+
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
108+
} else {
109+
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
110+
}
111+
}
112+
113+
return &model, nil
103114
}
104115

105116
func buildGetGrafanaConfigRequest(ctx context.Context, model *inputModel, apiClient *argus.APIClient) argus.ApiGetGrafanaConfigsRequest {

internal/cmd/argus/grafana/describe/describe_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66

77
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
89

910
"github.com/google/go-cmp/cmp"
1011
"github.com/google/go-cmp/cmp/cmpopts"
@@ -167,7 +168,8 @@ func TestParseInput(t *testing.T) {
167168

168169
for _, tt := range tests {
169170
t.Run(tt.description, func(t *testing.T) {
170-
cmd := NewCmd(nil)
171+
p := print.NewPrinter()
172+
cmd := NewCmd(p)
171173
err := globalflags.Configure(cmd.Flags())
172174
if err != nil {
173175
t.Fatalf("configure global flags: %v", err)
@@ -199,7 +201,7 @@ func TestParseInput(t *testing.T) {
199201
t.Fatalf("error validating flags: %v", err)
200202
}
201203

202-
model, err := parseInput(nil, cmd, tt.argValues)
204+
model, err := parseInput(p, cmd, tt.argValues)
203205

204206
if err != nil {
205207
if !tt.isValid {

internal/cmd/argus/grafana/public-read-access/disable/disable.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,21 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
9191
return nil, &errors.ProjectIdError{}
9292
}
9393

94-
return &inputModel{
94+
model := inputModel{
9595
GlobalFlagModel: globalFlags,
9696
InstanceId: instanceId,
97-
}, nil
97+
}
98+
99+
if p.IsVerbosityDebug() {
100+
modelStr, err := print.BuildDebugStrFromInputModel(model)
101+
if err != nil {
102+
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
103+
} else {
104+
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
105+
}
106+
}
107+
108+
return &model, nil
98109
}
99110

100111
func buildRequest(ctx context.Context, model *inputModel, apiClient argusUtils.ArgusClient) (argus.ApiUpdateGrafanaConfigsRequest, error) {

internal/cmd/argus/grafana/public-read-access/disable/disable_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
910
argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1112

@@ -184,7 +185,8 @@ func TestParseInput(t *testing.T) {
184185

185186
for _, tt := range tests {
186187
t.Run(tt.description, func(t *testing.T) {
187-
cmd := NewCmd(nil)
188+
p := print.NewPrinter()
189+
cmd := NewCmd(p)
188190
err := globalflags.Configure(cmd.Flags())
189191
if err != nil {
190192
t.Fatalf("configure global flags: %v", err)
@@ -216,7 +218,7 @@ func TestParseInput(t *testing.T) {
216218
t.Fatalf("error validating flags: %v", err)
217219
}
218220

219-
model, err := parseInput(nil, cmd, tt.argValues)
221+
model, err := parseInput(p, cmd, tt.argValues)
220222
if err != nil {
221223
if !tt.isValid {
222224
return

internal/cmd/argus/grafana/public-read-access/enable/enable.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,21 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
9191
return nil, &errors.ProjectIdError{}
9292
}
9393

94-
return &inputModel{
94+
model := inputModel{
9595
GlobalFlagModel: globalFlags,
9696
InstanceId: instanceId,
97-
}, nil
97+
}
98+
99+
if p.IsVerbosityDebug() {
100+
modelStr, err := print.BuildDebugStrFromInputModel(model)
101+
if err != nil {
102+
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
103+
} else {
104+
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
105+
}
106+
}
107+
108+
return &model, nil
98109
}
99110

100111
func buildRequest(ctx context.Context, model *inputModel, apiClient argusUtils.ArgusClient) (argus.ApiUpdateGrafanaConfigsRequest, error) {

internal/cmd/argus/grafana/public-read-access/enable/enable_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
910
argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1112

@@ -184,7 +185,8 @@ func TestParseInput(t *testing.T) {
184185

185186
for _, tt := range tests {
186187
t.Run(tt.description, func(t *testing.T) {
187-
cmd := NewCmd(nil)
188+
p := print.NewPrinter()
189+
cmd := NewCmd(p)
188190
err := globalflags.Configure(cmd.Flags())
189191
if err != nil {
190192
t.Fatalf("configure global flags: %v", err)
@@ -216,7 +218,7 @@ func TestParseInput(t *testing.T) {
216218
t.Fatalf("error validating flags: %v", err)
217219
}
218220

219-
model, err := parseInput(nil, cmd, tt.argValues)
221+
model, err := parseInput(p, cmd, tt.argValues)
220222
if err != nil {
221223
if !tt.isValid {
222224
return

internal/cmd/argus/grafana/single-sign-on/disable/disable.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,21 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
9191
return nil, &errors.ProjectIdError{}
9292
}
9393

94-
return &inputModel{
94+
model := inputModel{
9595
GlobalFlagModel: globalFlags,
9696
InstanceId: instanceId,
97-
}, nil
97+
}
98+
99+
if p.IsVerbosityDebug() {
100+
modelStr, err := print.BuildDebugStrFromInputModel(model)
101+
if err != nil {
102+
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
103+
} else {
104+
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
105+
}
106+
}
107+
108+
return &model, nil
98109
}
99110

100111
func buildRequest(ctx context.Context, model *inputModel, apiClient argusUtils.ArgusClient) (argus.ApiUpdateGrafanaConfigsRequest, error) {

internal/cmd/argus/grafana/single-sign-on/disable/disable_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
910
argusUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/argus/utils"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1112

@@ -184,7 +185,8 @@ func TestParseInput(t *testing.T) {
184185

185186
for _, tt := range tests {
186187
t.Run(tt.description, func(t *testing.T) {
187-
cmd := NewCmd(nil)
188+
p := print.NewPrinter()
189+
cmd := NewCmd(p)
188190
err := globalflags.Configure(cmd.Flags())
189191
if err != nil {
190192
t.Fatalf("configure global flags: %v", err)
@@ -216,7 +218,7 @@ func TestParseInput(t *testing.T) {
216218
t.Fatalf("error validating flags: %v", err)
217219
}
218220

219-
model, err := parseInput(nil, cmd, tt.argValues)
221+
model, err := parseInput(p, cmd, tt.argValues)
220222
if err != nil {
221223
if !tt.isValid {
222224
return

internal/cmd/argus/grafana/single-sign-on/enable/enable.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,21 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
9191
return nil, &errors.ProjectIdError{}
9292
}
9393

94-
return &inputModel{
94+
model := inputModel{
9595
GlobalFlagModel: globalFlags,
9696
InstanceId: instanceId,
97-
}, nil
97+
}
98+
99+
if p.IsVerbosityDebug() {
100+
modelStr, err := print.BuildDebugStrFromInputModel(model)
101+
if err != nil {
102+
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
103+
} else {
104+
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
105+
}
106+
}
107+
108+
return &model, nil
98109
}
99110

100111
func buildRequest(ctx context.Context, model *inputModel, apiClient argusUtils.ArgusClient) (argus.ApiUpdateGrafanaConfigsRequest, error) {

0 commit comments

Comments
 (0)