Skip to content

Commit cb431b3

Browse files
committed
STAC-24987: use DI to suppress output with mock printer
1 parent 504d530 commit cb431b3

7 files changed

Lines changed: 121 additions & 111 deletions

File tree

‎cmd/stackpack/common.go‎

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func (w *OperationWaiter) WaitForCompletion(options WaitOptions) error {
121121

122122
// waitAndDisplayResult waits for a StackPack operation to complete, then displays the final status.
123123
// operationLabel is used in progress/success messages (e.g. "upgrade" or "downgrade").
124-
func waitAndDisplayResult(cli *di.Deps, api *stackstate_api.APIClient, stackPackName string, timeout time.Duration, operationLabel string, mute bool) common.CLIError {
124+
func waitAndDisplayResult(cli *di.Deps, api *stackstate_api.APIClient, stackPackName string, timeout time.Duration, operationLabel string) common.CLIError {
125125
if !cli.IsJson() {
126126
cli.Printer.PrintLn("Waiting for " + operationLabel + " to complete...")
127127
}
@@ -145,34 +145,32 @@ func waitAndDisplayResult(cli *di.Deps, api *stackstate_api.APIClient, stackPack
145145
return common.NewNotFoundError(err)
146146
}
147147

148-
if !mute {
149-
if cli.IsJson() {
150-
cli.Printer.PrintJson(map[string]interface{}{
151-
"stackpack": finalStackPack,
152-
"status": "completed",
153-
"current-version": finalStackPack.GetVersion(),
154-
})
155-
} else {
156-
cli.Printer.Success("StackPack " + operationLabel + " completed successfully")
157-
158-
data := make([][]interface{}, 0)
159-
for _, config := range finalStackPack.GetConfigurations() {
160-
lastUpdateTime := time.UnixMilli(config.GetLastUpdateTimestamp())
161-
data = append(data, []interface{}{
162-
config.GetId(),
163-
finalStackPack.GetName(),
164-
config.GetStatus(),
165-
config.GetStackPackVersion(),
166-
lastUpdateTime,
167-
})
168-
}
169-
170-
cli.Printer.Table(printer.TableData{
171-
Header: []string{"id", "name", "status", "version", "last updated"},
172-
Data: data,
173-
MissingTableDataMsg: printer.NotFoundMsg{Types: "configurations for " + stackPackName},
148+
if cli.IsJson() {
149+
cli.Printer.PrintJson(map[string]interface{}{
150+
"stackpack": finalStackPack,
151+
"status": "completed",
152+
"current-version": finalStackPack.GetVersion(),
153+
})
154+
} else {
155+
cli.Printer.Success("StackPack " + operationLabel + " completed successfully")
156+
157+
data := make([][]interface{}, 0)
158+
for _, config := range finalStackPack.GetConfigurations() {
159+
lastUpdateTime := time.UnixMilli(config.GetLastUpdateTimestamp())
160+
data = append(data, []interface{}{
161+
config.GetId(),
162+
finalStackPack.GetName(),
163+
config.GetStatus(),
164+
config.GetStackPackVersion(),
165+
lastUpdateTime,
174166
})
175167
}
168+
169+
cli.Printer.Table(printer.TableData{
170+
Header: []string{"id", "name", "status", "version", "last updated"},
171+
Data: data,
172+
MissingTableDataMsg: printer.NotFoundMsg{Types: "configurations for " + stackPackName},
173+
})
176174
}
177175

178176
return nil

‎cmd/stackpack/stackpack_downgrade.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ sts stackpack downgrade --name kubernetes --stackpack-version 1.2.3
3030
3131
# downgrade and wait for completion
3232
sts stackpack downgrade --name kubernetes --stackpack-version 1.2.3 --wait`,
33-
RunE: cli.CmdRunEWithApi(RunStackpackDowngradeCommand(args, false)),
33+
RunE: cli.CmdRunEWithApi(RunStackpackDowngradeCommand(args)),
3434
}
3535
common.AddRequiredNameFlagVar(cmd, &args.TypeName, "Name of the StackPack")
3636
cmd.Flags().StringVar(&args.Version, StackpackVersionFlag, "", "Version to downgrade to")
@@ -40,7 +40,7 @@ sts stackpack downgrade --name kubernetes --stackpack-version 1.2.3 --wait`,
4040
return cmd
4141
}
4242

43-
func RunStackpackDowngradeCommand(args *DowngradeArgs, mute bool) di.CmdWithApiFn {
43+
func RunStackpackDowngradeCommand(args *DowngradeArgs) di.CmdWithApiFn {
4444
return func(
4545
cmd *cobra.Command,
4646
cli *di.Deps,
@@ -55,7 +55,7 @@ func RunStackpackDowngradeCommand(args *DowngradeArgs, mute bool) di.CmdWithApiF
5555
}
5656

5757
if args.Wait {
58-
if cliErr := waitAndDisplayResult(cli, api, args.TypeName, args.Timeout, "downgrade", mute); cliErr != nil {
58+
if cliErr := waitAndDisplayResult(cli, api, args.TypeName, args.Timeout, "downgrade"); cliErr != nil {
5959
return cliErr
6060
}
6161
} else {

‎cmd/stackpack/stackpack_install.go‎

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ sts stackpack install --name kubernetes -p cluster_name=production --wait
3838
3939
# install a specific (older) version, e.g. to downgrade
4040
sts stackpack install --name example --stackpack-version 1.2.3 -p "full_name=First Last"`,
41-
RunE: cli.CmdRunEWithApi(RunStackpackInstallCommand(args, false)),
41+
RunE: cli.CmdRunEWithApi(RunStackpackInstallCommand(args)),
4242
}
4343
common.AddRequiredNameFlagVar(cmd, &args.Name, "Name of the StackPack")
4444
pflags.EnumVar(cmd.Flags(), &args.UnlockedStrategy,
@@ -55,7 +55,7 @@ sts stackpack install --name example --stackpack-version 1.2.3 -p "full_name=Fir
5555
return cmd
5656
}
5757

58-
func RunStackpackInstallCommand(args *InstallArgs, mute bool) di.CmdWithApiFn {
58+
func RunStackpackInstallCommand(args *InstallArgs) di.CmdWithApiFn {
5959
return func(
6060
cmd *cobra.Command,
6161
cli *di.Deps,
@@ -101,38 +101,36 @@ func RunStackpackInstallCommand(args *InstallArgs, mute bool) di.CmdWithApiFn {
101101
}
102102

103103
// Display final status
104-
if !mute {
105-
if cli.IsJson() {
106-
cli.Printer.PrintJson(map[string]interface{}{
107-
"stackpack": finalStackPack,
108-
"status": "completed",
104+
if cli.IsJson() {
105+
cli.Printer.PrintJson(map[string]interface{}{
106+
"stackpack": finalStackPack,
107+
"status": "completed",
108+
})
109+
} else {
110+
cli.Printer.Success("StackPack installation completed successfully")
111+
112+
// Show configurations status
113+
data := make([][]interface{}, 0)
114+
for _, config := range finalStackPack.GetConfigurations() {
115+
lastUpdateTime := time.UnixMilli(config.GetLastUpdateTimestamp())
116+
data = append(data, []interface{}{
117+
config.GetId(),
118+
finalStackPack.GetName(),
119+
config.GetStatus(),
120+
config.GetStackPackVersion(),
121+
lastUpdateTime,
109122
})
110-
} else {
111-
cli.Printer.Success("StackPack installation completed successfully")
112-
113-
// Show configurations status
114-
data := make([][]interface{}, 0)
115-
for _, config := range finalStackPack.GetConfigurations() {
116-
lastUpdateTime := time.UnixMilli(config.GetLastUpdateTimestamp())
117-
data = append(data, []interface{}{
118-
config.GetId(),
119-
finalStackPack.GetName(),
120-
config.GetStatus(),
121-
config.GetStackPackVersion(),
122-
lastUpdateTime,
123-
})
124-
}
125-
126-
cli.Printer.Table(
127-
printer.TableData{
128-
Header: []string{"id", "name", "status", "version", "last updated"},
129-
Data: data,
130-
MissingTableDataMsg: printer.NotFoundMsg{Types: "configurations for " + args.Name},
131-
},
132-
)
133123
}
124+
125+
cli.Printer.Table(
126+
printer.TableData{
127+
Header: []string{"id", "name", "status", "version", "last updated"},
128+
Data: data,
129+
MissingTableDataMsg: printer.NotFoundMsg{Types: "configurations for " + args.Name},
130+
},
131+
)
134132
}
135-
} else if !mute {
133+
} else {
136134
if cli.IsJson() {
137135
cli.Printer.PrintJson(map[string]interface{}{
138136
"instance": instance,

‎cmd/stackpack/stackpack_package.go‎

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ sts stackpack package -f my-custom-archive.sts
106106
107107
# Force overwrite existing .sts file
108108
sts stackpack package --force`,
109-
RunE: cli.CmdRunE(RunStackpackPackageCommand(args, false)),
109+
RunE: cli.CmdRunE(RunStackpackPackageCommand(args)),
110110
}
111111

112112
cmd.Flags().StringVarP(&args.StackpackDir, "directory", "d", "", "Path to stackpack directory (defaults to current directory)")
@@ -117,7 +117,7 @@ sts stackpack package --force`,
117117
}
118118

119119
// RunStackpackPackageCommand executes the package command
120-
func RunStackpackPackageCommand(args *PackageArgs, mute bool) func(cli *di.Deps, cmd *cobra.Command) common.CLIError {
120+
func RunStackpackPackageCommand(args *PackageArgs) func(cli *di.Deps, cmd *cobra.Command) common.CLIError {
121121
return func(cli *di.Deps, cmd *cobra.Command) common.CLIError {
122122
// Set default stackpack directory
123123
if args.StackpackDir == "" {
@@ -180,21 +180,19 @@ func RunStackpackPackageCommand(args *PackageArgs, mute bool) func(cli *di.Deps,
180180
return common.NewRuntimeError(fmt.Errorf("failed to create .sts file: %w", err))
181181
}
182182

183-
if !mute {
184-
if cli.IsJson() {
185-
cli.Printer.PrintJson(map[string]interface{}{
186-
"success": true,
187-
"stackpack_name": stackpackInfo.Name,
188-
"stackpack_version": stackpackInfo.Version,
189-
"zip_file": args.ArchiveFile,
190-
"source_dir": args.StackpackDir,
191-
})
192-
} else {
193-
cli.Printer.Successf("Stackpack packaged successfully!")
194-
cli.Printer.PrintLn("")
195-
cli.Printer.PrintLn(fmt.Sprintf("Stackpack: %s (v%s)", stackpackInfo.Name, stackpackInfo.Version))
196-
cli.Printer.PrintLn(fmt.Sprintf(".sts file: %s", args.ArchiveFile))
197-
}
183+
if cli.IsJson() {
184+
cli.Printer.PrintJson(map[string]interface{}{
185+
"success": true,
186+
"stackpack_name": stackpackInfo.Name,
187+
"stackpack_version": stackpackInfo.Version,
188+
"zip_file": args.ArchiveFile,
189+
"source_dir": args.StackpackDir,
190+
})
191+
} else {
192+
cli.Printer.Successf("Stackpack packaged successfully!")
193+
cli.Printer.PrintLn("")
194+
cli.Printer.PrintLn(fmt.Sprintf("Stackpack: %s (v%s)", stackpackInfo.Name, stackpackInfo.Version))
195+
cli.Printer.PrintLn(fmt.Sprintf(".sts file: %s", args.ArchiveFile))
198196
}
199197

200198
return nil

‎cmd/stackpack/stackpack_test_deploy.go‎

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package stackpack
33
import (
44
"bufio"
55
"fmt"
6+
"github.com/stackvista/stackstate-cli/internal/printer"
67
"io"
78
"os"
89
"path/filepath"
@@ -332,33 +333,50 @@ func confirmUpload(cli *di.Deps, zipFile string) bool {
332333
// runPackageStep executes the package command logic
333334
func runPackageStep(cli *di.Deps, args *PackageArgs) common.CLIError {
334335
// Reuse the existing package command logic
335-
packageCmd := &cobra.Command{}
336-
packageFn := RunStackpackPackageCommand(args, true)
337-
return packageFn(cli, packageCmd)
336+
return runCmd(cli, func(ctx *di.Deps) common.CLIError {
337+
packageCmd := &cobra.Command{}
338+
packageFn := RunStackpackPackageCommand(args)
339+
return packageFn(ctx, packageCmd)
340+
})
338341
}
339342

340343
// runUploadStep executes the upload command logic
341344
func runUploadStep(cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo, args *UploadArgs) common.CLIError {
342-
// Reuse the existing upload command logic
343-
uploadCmd := &cobra.Command{}
344-
uploadFn := RunStackpackUploadCommand(args, true)
345-
return uploadFn(uploadCmd, cli, api, serverInfo)
345+
return runCmd(cli, func(ctx *di.Deps) common.CLIError {
346+
// Reuse the existing upload command logic
347+
uploadCmd := &cobra.Command{}
348+
uploadFn := RunStackpackUploadCommand(args)
349+
return uploadFn(uploadCmd, ctx, api, serverInfo)
350+
})
346351
}
347352

348353
// runInstallStep executes the install command logic
349354
func runInstallStep(cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo, args *InstallArgs) common.CLIError {
350-
// Reuse the existing install command logic
351-
installCmd := &cobra.Command{}
352-
installFn := RunStackpackInstallCommand(args, true)
353-
return installFn(installCmd, cli, api, serverInfo)
355+
return runCmd(cli, func(ctx *di.Deps) common.CLIError {
356+
// Reuse the existing install command logic
357+
installCmd := &cobra.Command{}
358+
installFn := RunStackpackInstallCommand(args)
359+
return installFn(installCmd, ctx, api, serverInfo)
360+
})
354361
}
355362

356363
// runUpgradeStep executes the upgrade command logic
357364
func runUpgradeStep(cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo, args *UpgradeArgs) common.CLIError {
358-
// Reuse the existing upgrade command logic
359-
upgradeCmd := &cobra.Command{}
360-
upgradeFn := RunStackpackUpgradeCommand(args, true)
361-
return upgradeFn(upgradeCmd, cli, api, serverInfo)
365+
return runCmd(cli, func(ctx *di.Deps) common.CLIError {
366+
// Reuse the existing upgrade command logic
367+
upgradeCmd := &cobra.Command{}
368+
upgradeFn := RunStackpackUpgradeCommand(args)
369+
return upgradeFn(upgradeCmd, ctx, api, serverInfo)
370+
})
371+
}
372+
373+
func runCmd(cli *di.Deps, f func(ctx *di.Deps) common.CLIError) common.CLIError {
374+
oldPr := cli.Printer
375+
pr := printer.NewMockPrinter(nil)
376+
cli.Printer = &pr
377+
err := f(cli)
378+
cli.Printer = oldPr
379+
return err
362380
}
363381

364382
// getInstalledStackpackVersion checks if a stackpack is installed and returns its version

‎cmd/stackpack/stackpack_upgrade.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ sts stackpack upgrade --name kubernetes
3535
3636
# upgrade and wait for completion
3737
sts stackpack upgrade --name kubernetes --wait`,
38-
RunE: cli.CmdRunEWithApi(RunStackpackUpgradeCommand(args, false)),
38+
RunE: cli.CmdRunEWithApi(RunStackpackUpgradeCommand(args)),
3939
}
4040
common.AddRequiredNameFlagVar(cmd, &args.TypeName, "Name of the StackPack")
4141
pflags.EnumVar(cmd.Flags(), &args.UnlockedStrategy,
@@ -49,7 +49,7 @@ sts stackpack upgrade --name kubernetes --wait`,
4949
cmd.Flags().DurationVar(&args.Timeout, "timeout", DefaultTimeout, "Timeout for waiting")
5050
return cmd
5151
}
52-
func RunStackpackUpgradeCommand(args *UpgradeArgs, mute bool) di.CmdWithApiFn {
52+
func RunStackpackUpgradeCommand(args *UpgradeArgs) di.CmdWithApiFn {
5353
return func(
5454
cmd *cobra.Command,
5555
cli *di.Deps,
@@ -73,10 +73,10 @@ func RunStackpackUpgradeCommand(args *UpgradeArgs, mute bool) di.CmdWithApiFn {
7373
}
7474

7575
if args.Wait {
76-
if cliErr := waitAndDisplayResult(cli, api, args.TypeName, args.Timeout, "upgrade", mute); cliErr != nil {
76+
if cliErr := waitAndDisplayResult(cli, api, args.TypeName, args.Timeout, "upgrade"); cliErr != nil {
7777
return cliErr
7878
}
79-
} else if !mute {
79+
} else {
8080
if cli.IsJson() {
8181
cli.Printer.PrintJson(map[string]interface{}{
8282
"success": true,

‎cmd/stackpack/stackpack_upload.go‎

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ func StackpackUploadCommand(cli *di.Deps) *cobra.Command {
2323
Long: "Upload a StackPack file (.sts) to SUSE Observability. After upload, the StackPack can be installed using 'sts stackpack install'.",
2424
Example: `# upload a StackPack
2525
sts stackpack upload --file my-stackpack.sts`,
26-
RunE: cli.CmdRunEWithApi(RunStackpackUploadCommand(args, false)),
26+
RunE: cli.CmdRunEWithApi(RunStackpackUploadCommand(args)),
2727
}
2828
common.AddRequiredFileFlagVar(cmd, &args.FilePath, "Stackpack file to upload (.sts file)")
2929

3030
return cmd
3131
}
3232

33-
func RunStackpackUploadCommand(args *UploadArgs, mute bool) di.CmdWithApiFn {
33+
func RunStackpackUploadCommand(args *UploadArgs) di.CmdWithApiFn {
3434
return func(
3535
cmd *cobra.Command,
3636
cli *di.Deps,
@@ -48,18 +48,16 @@ func RunStackpackUploadCommand(args *UploadArgs, mute bool) di.CmdWithApiFn {
4848
return common.NewResponseError(err, resp)
4949
}
5050

51-
if !mute {
52-
if cli.IsJson() {
53-
cli.Printer.PrintJson(map[string]interface{}{
54-
"uploaded-stackpack": stackpack,
55-
})
56-
} else {
57-
cli.Printer.Success(fmt.Sprintf("uploaded StackPack: %s", args.FilePath))
58-
cli.Printer.Table(printer.TableData{
59-
Header: []string{"name", "display name", "version"},
60-
Data: [][]interface{}{{stackpack.Name, stackpack.DisplayName, stackpack.Version}},
61-
})
62-
}
51+
if cli.IsJson() {
52+
cli.Printer.PrintJson(map[string]interface{}{
53+
"uploaded-stackpack": stackpack,
54+
})
55+
} else {
56+
cli.Printer.Success(fmt.Sprintf("uploaded StackPack: %s", args.FilePath))
57+
cli.Printer.Table(printer.TableData{
58+
Header: []string{"name", "display name", "version"},
59+
Data: [][]interface{}{{stackpack.Name, stackpack.DisplayName, stackpack.Version}},
60+
})
6361
}
6462

6563
return nil

0 commit comments

Comments
 (0)