@@ -15,6 +15,7 @@ import (
1515 "github.com/stackvista/stackstate-cli/generated/stackstate_api"
1616 "github.com/stackvista/stackstate-cli/internal/common"
1717 "github.com/stackvista/stackstate-cli/internal/di"
18+ "github.com/stackvista/stackstate-cli/internal/printer"
1819)
1920
2021const (
@@ -59,7 +60,7 @@ sts stackpack test-deploy -d ./my-stackpack --yes`,
5960 RunE : cli .CmdRunEWithApi (RunStackpackTestDeployCommand (args )),
6061 }
6162
62- cmd .Flags ().StringVarP (& args .StackpackDir , "stackpack- directory" , "d" , "" , "Path to stackpack directory (defaults to current directory)" )
63+ cmd .Flags ().StringVarP (& args .StackpackDir , "directory" , "d" , "" , "Path to stackpack directory (defaults to current directory)" )
6364 cmd .Flags ().StringToStringVarP (& args .Params , ParameterFlag , "p" , args .Params , "List of parameters of the form \" key=value\" " )
6465 cmd .Flags ().BoolVarP (& args .Yes , "yes" , "y" , false , "Skip confirmation prompt before upload" )
6566
@@ -76,11 +77,6 @@ func RunStackpackTestDeployCommand(args *TestDeployArgs) di.CmdWithApiFn {
7677 api * stackstate_api.APIClient ,
7778 serverInfo * stackstate_api.ServerInfo ,
7879 ) common.CLIError {
79- // Warn if JSON output is requested - not meaningful for test-deploy command
80- if cli .IsJson () {
81- cli .Printer .PrintLn ("Warning: JSON output format is not meaningful for the test-deploy command, proceeding with text output" )
82- }
83-
8480 // Set default stackpack directory
8581 if args .StackpackDir == "" {
8682 currentDir , err := os .Getwd ()
@@ -98,20 +94,20 @@ func RunStackpackTestDeployCommand(args *TestDeployArgs) di.CmdWithApiFn {
9894 return common .NewRuntimeError (fmt .Errorf ("failed to parse %s: %w" , stackpackConfigFile , err ))
9995 }
10096
101- cli . Printer . Success ( "Starting stackpack test-deploy sequence..." )
102- cli . Printer . PrintLn ( fmt .Sprintf (" Stackpack: %s (current version: %s)" , originalInfo .Name , originalInfo .Version ))
103- cli . Printer . PrintLn ( "" )
97+ printSuccess ( cli , "Starting stackpack test-deploy sequence..." )
98+ printMsg ( cli , fmt .Sprintf (" Stackpack: %s (current version: %s)" , originalInfo .Name , originalInfo .Version ))
99+ printMsg ( cli , "" )
104100
105101 // Step 1: Check installed version and determine base version for snapshot
106- cli . Printer . PrintLn ( "Step 1/5: Checking installed version..." )
102+ printMsg ( cli , "Step 1/5: Checking installed version..." )
107103 installedVersion , err := getInstalledStackpackVersion (cli , api , originalInfo .Name )
108104 if err != nil {
109105 return common .NewRuntimeError (fmt .Errorf ("failed to check installed stackpack version: %w" , err ))
110106 }
111107
112108 baseVersionForSnapshot := originalInfo .Version
113109 if installedVersion != "" {
114- cli . Printer . PrintLn ( fmt .Sprintf (" Found installed version: %s" , installedVersion ))
110+ printMsg ( cli , fmt .Sprintf (" Found installed version: %s" , installedVersion ))
115111
116112 // Compare base versions (strip cli-test suffix from installed version for comparison)
117113 installedBaseVersion := installedVersion
@@ -129,15 +125,15 @@ func RunStackpackTestDeployCommand(args *TestDeployArgs) di.CmdWithApiFn {
129125 baseComparison := compareVersions (installedBaseVersion , originalInfo .Version )
130126 if baseComparison > 0 || (baseComparison == 0 && strings .Contains (installedVersion , "-cli-test." )) {
131127 baseVersionForSnapshot = installedVersion
132- cli . Printer . PrintLn ( fmt .Sprintf (" Using installed version as base: %s" , baseVersionForSnapshot ))
128+ printMsg ( cli , fmt .Sprintf (" Using installed version as base: %s" , baseVersionForSnapshot ))
133129 } else if baseComparison < 0 {
134- cli . Printer . PrintLn ( fmt .Sprintf (" Using local version as base (higher than installed): %s" , baseVersionForSnapshot ))
130+ printMsg ( cli , fmt .Sprintf (" Using local version as base (higher than installed): %s" , baseVersionForSnapshot ))
135131 }
136132 }
137133
138134 // Step 2: Create temporary directory and copy stackpack
139- cli . Printer . PrintLn ( "" )
140- cli . Printer . PrintLn ( "Step 2/5: Creating temporary copy for testing..." )
135+ printMsg ( cli , "" )
136+ printMsg ( cli , "Step 2/5: Creating temporary copy for testing..." )
141137
142138 tempDir , err := os .MkdirTemp ("" , "stackpack-test-*" )
143139 if err != nil {
@@ -147,30 +143,30 @@ func RunStackpackTestDeployCommand(args *TestDeployArgs) di.CmdWithApiFn {
147143 // Ensure cleanup of temporary directory
148144 defer func () {
149145 if removeErr := os .RemoveAll (tempDir ); removeErr != nil {
150- cli . Printer . PrintLn ( fmt .Sprintf ("Warning: Failed to cleanup temporary directory: %v" , removeErr ))
146+ printMsg ( cli , fmt .Sprintf ("Warning: Failed to cleanup temporary directory: %v" , removeErr ))
151147 }
152148 }()
153149
154150 tempStackpackDir := filepath .Join (tempDir , "stackpack" )
155151 if err := copyDirectory (args .StackpackDir , tempStackpackDir ); err != nil {
156152 return common .NewRuntimeError (fmt .Errorf ("failed to copy stackpack to temporary directory: %w" , err ))
157153 }
158- cli . Printer . Success ( "Temporary copy created" )
154+ printSuccess ( cli , "Temporary copy created" )
159155
160156 // Step 3: Update version in temporary copy
161- cli . Printer . PrintLn ( "" )
162- cli . Printer . PrintLn ( "Step 3/5: Bumping version for testing..." )
157+ printMsg ( cli , "" )
158+ printMsg ( cli , "Step 3/5: Bumping version for testing..." )
163159
164160 tempConfigPath := filepath .Join (tempStackpackDir , stackpackConfigFile )
165161 newVersion , err := bumpSnapshotVersionWithBase (tempConfigPath , baseVersionForSnapshot )
166162 if err != nil {
167163 return common .NewRuntimeError (fmt .Errorf ("failed to bump version: %w" , err ))
168164 }
169- cli . Printer . Success ( fmt .Sprintf ("Version bumped to: %s" , newVersion ))
165+ printSuccess ( cli , fmt .Sprintf ("Version bumped to: %s" , newVersion ))
170166
171167 // Step 4: Package stackpack from temporary directory
172- cli . Printer . PrintLn ( "" )
173- cli . Printer . PrintLn ( "Step 4/5: Packaging stackpack..." )
168+ printMsg ( cli , "" )
169+ printMsg ( cli , "Step 4/5: Packaging stackpack..." )
174170 packageArgs := & PackageArgs {
175171 StackpackDir : tempStackpackDir , // Use temporary directory
176172 Force : true , // Always overwrite for testing
@@ -183,27 +179,27 @@ func RunStackpackTestDeployCommand(args *TestDeployArgs) di.CmdWithApiFn {
183179 if err := runPackageStep (cli , packageArgs ); err != nil {
184180 return err
185181 }
186- cli . Printer . Success ( "Stackpack packaged successfully" )
182+ printSuccess ( cli , "Stackpack packaged successfully" )
187183
188184 // Step 5: Confirm upload (if needed) and execute upload/install workflow
189- if ! args .Yes {
190- cli . Printer . PrintLn ( "" )
185+ if ! args .Yes && ! cli . IsJson () {
186+ printMsg ( cli , "" )
191187 if ! confirmUpload (cli , packageArgs .ArchiveFile ) {
192188 return common .NewRuntimeError (fmt .Errorf ("upload cancelled by user" ))
193189 }
194190 }
195191
196192 // Upload stackpack
197- cli . Printer . PrintLn ( "" )
198- cli . Printer . PrintLn ( "Step 5/5: Uploading and installing/upgrading stackpack..." )
193+ printMsg ( cli , "" )
194+ printMsg ( cli , "Step 5/5: Uploading and installing/upgrading stackpack..." )
199195 uploadArgs := & UploadArgs {
200196 FilePath : packageArgs .ArchiveFile ,
201197 }
202198
203199 if err := runUploadStep (cli , api , serverInfo , uploadArgs ); err != nil {
204200 return err
205201 }
206- cli . Printer . Success ( "Stackpack uploaded successfully" )
202+ printSuccess ( cli , "Stackpack uploaded successfully" )
207203
208204 // Install or upgrade stackpack based on installation status
209205 if installedVersion != "" {
@@ -217,7 +213,7 @@ func RunStackpackTestDeployCommand(args *TestDeployArgs) di.CmdWithApiFn {
217213 if err := runUpgradeStep (cli , api , serverInfo , upgradeArgs ); err != nil {
218214 return err
219215 }
220- cli . Printer . Success ( "Stackpack upgraded successfully" )
216+ printSuccess ( cli , "Stackpack upgraded successfully" )
221217 } else {
222218 installArgs := & InstallArgs {
223219 Name : originalInfo .Name ,
@@ -230,15 +226,20 @@ func RunStackpackTestDeployCommand(args *TestDeployArgs) di.CmdWithApiFn {
230226 if err := runInstallStep (cli , api , serverInfo , installArgs ); err != nil {
231227 return err
232228 }
233- cli . Printer . Success ( "Stackpack installed successfully" )
229+ printSuccess ( cli , "Stackpack installed successfully" )
234230 }
235231
236- cli . Printer . PrintLn ( "" )
237- cli . Printer . Success ( "🎉 Test-deploy sequence completed successfully!" )
232+ printMsg ( cli , "" )
233+ printSuccess ( cli , "🎉 Test-deploy sequence completed successfully!" )
238234
239235 // Clean up .sts file
240236 if err := os .Remove (packageArgs .ArchiveFile ); err != nil {
241- cli .Printer .PrintLn (fmt .Sprintf ("Note: Could not clean up .sts file %s: %v" , packageArgs .ArchiveFile , err ))
237+ printMsg (cli , fmt .Sprintf ("Note: Could not clean up .sts file %s: %v" , packageArgs .ArchiveFile , err ))
238+ } else if cli .IsJson () {
239+ cli .Printer .PrintJson (map [string ]interface {}{
240+ "stackpack" : originalInfo .Name ,
241+ "version" : newVersion ,
242+ })
242243 }
243244
244245 return nil
@@ -315,8 +316,8 @@ func confirmUpload(cli *di.Deps, zipFile string) bool {
315316 serverURL = cli .CurrentContext .URL
316317 }
317318
318- cli . Printer . PrintLn ( fmt .Sprintf ("⚠️ This will upload '%s' to SUSE Observability server:" , filepath .Base (zipFile )))
319- cli . Printer . PrintLn ( fmt .Sprintf (" Server: %s" , serverURL ))
319+ printMsg ( cli , fmt .Sprintf ("⚠️ This will upload '%s' to SUSE Observability server:" , filepath .Base (zipFile )))
320+ printMsg ( cli , fmt .Sprintf (" Server: %s" , serverURL ))
320321 fmt .Print (" Continue? (y/N): " )
321322
322323 reader := bufio .NewReader (os .Stdin )
@@ -332,33 +333,50 @@ func confirmUpload(cli *di.Deps, zipFile string) bool {
332333// runPackageStep executes the package command logic
333334func runPackageStep (cli * di.Deps , args * PackageArgs ) common.CLIError {
334335 // Reuse the existing package command logic
335- packageCmd := & cobra.Command {}
336- packageFn := RunStackpackPackageCommand (args )
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
341344func 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 )
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
349354func 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 )
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
357364func 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 )
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
@@ -489,3 +507,15 @@ func updateVersionInYaml(configPath, newVersion string) error {
489507
490508 return nil
491509}
510+
511+ func printMsg (cli * di.Deps , msg string ) {
512+ if ! cli .IsJson () {
513+ cli .Printer .PrintLn (msg )
514+ }
515+ }
516+
517+ func printSuccess (cli * di.Deps , msg string ) {
518+ if ! cli .IsJson () {
519+ cli .Printer .Success (msg )
520+ }
521+ }
0 commit comments