Skip to content

Commit

Permalink
cmd: use Print* functions from *cobra.Command
Browse files Browse the repository at this point in the history
Use the the Print function from the *cobra.Command object, instead of
from the fmt package.
This allows to easily redirect the output in testscases.
  • Loading branch information
fho committed Nov 30, 2023
1 parent bf27846 commit 31c6de5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
17 changes: 9 additions & 8 deletions internal/cmd/deployorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type deployOrder struct {
apps []string

src string
eEnv string
Env string
region string

srcType fs.PathType
Expand Down Expand Up @@ -93,6 +93,9 @@ func newDeployOrder() *deployOrder {
return fmt.Errorf("expecting 3 arguments, got: %d", len(args))
}

cmd.Env = args[1]
cmd.region = args[2]

case fs.PathTypeFile:
if len(args) != 1 {
return fmt.Errorf("expecting 1 arguments, got: %d", len(args))
Expand All @@ -103,8 +106,6 @@ func newDeployOrder() *deployOrder {
}

cmd.src = args[0]
cmd.eEnv = args[1]
cmd.region = args[2]
cmd.srcType = pType

return validateAppsParam(cmd.apps)
Expand All @@ -114,7 +115,7 @@ func newDeployOrder() *deployOrder {
return &cmd
}

func (c *deployOrder) run(*cobra.Command, []string) error {
func (c *deployOrder) run(cc *cobra.Command, _ []string) error {
var depsfrom deps.Composition

composition, err := c.loadComposition()
Expand All @@ -140,16 +141,16 @@ func (c *deployOrder) run(*cobra.Command, []string) error {
}

for _, i := range secondsorted {
fmt.Println(i)
cc.Println(i)
}
case "dot":
fmt.Printf("###########\n# dot of %s\n##########\n", strings.Join(c.apps, ", "))
cc.Printf("###########\n# dot of %s\n##########\n", strings.Join(c.apps, ", "))
depsgraph, err := deps.OutputDotGraph(depsfrom)
if err != nil {
return err
}

fmt.Println(depsgraph)
cc.Println(depsgraph)
}

return nil
Expand All @@ -167,7 +168,7 @@ func validateAppsParam(apps []string) error {
func (c *deployOrder) loadComposition() (*deps.Composition, error) {
switch c.srcType {
case fs.PathTypeDir:
return deps.CompositionFromSisuDir(c.src, c.eEnv, c.region)
return deps.CompositionFromSisuDir(c.src, c.Env, c.region)

case fs.PathTypeFile:
return deps.CompositionFromJSON(c.src)
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/export.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"path/filepath"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -40,7 +39,7 @@ func newExportCmd() *exportCmd {
return &cmd
}

func (c *exportCmd) run(*cobra.Command, []string) error {
func (c *exportCmd) run(cc *cobra.Command, _ []string) error {
absPath, err := filepath.Abs(c.destFile)
if err != nil {
return err
Expand All @@ -56,7 +55,7 @@ func (c *exportCmd) run(*cobra.Command, []string) error {
return err
}

fmt.Printf("written dependency tree to %s\n", absPath)
cc.Printf("written dependency tree to %s\n", absPath)

return nil
}
3 changes: 3 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
)

Expand All @@ -22,5 +24,6 @@ func newRoot() *cobra.Command {

func Execute() {
root := newRoot()
root.SetOut(os.Stdout)
_ = root.Execute()
}
6 changes: 2 additions & 4 deletions internal/cmd/verify.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"

"github.com/simplesurance/dependencies-tool/internal/deps"
Expand Down Expand Up @@ -35,7 +33,7 @@ func newVerify() *verify {
return &cmd
}

func (c *verify) run(*cobra.Command, []string) error {
func (c *verify) run(cc *cobra.Command, _ []string) error {
composition, err := deps.CompositionFromSisuDir(c.path, c.env, c.region)
if err != nil {
return err
Expand All @@ -45,7 +43,7 @@ func (c *verify) run(*cobra.Command, []string) error {
return err
}

fmt.Println("verification successful")
cc.Println("verification successful")

return nil
}

0 comments on commit 31c6de5

Please # to comment.