-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add "dependencies-tool contains" sub-command
Add the sub-command: dependencies-tool contains ROOT-DIR|DEP-TREE-FILE DISTRIBUTION APP The command checks if a given distribution contains an app with the given name. If it does it exits with code 0, otherwise with code 2. Additionally a result message is printed to stdout.
- Loading branch information
Showing
7 changed files
with
176 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/simplesurance/dependencies-tool/v3/internal/cmd/fs" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var containsLongHelp = fmt.Sprintf(` | ||
Check if an app is part of a distribution. | ||
Exit Codes: | ||
%d - Success, app is part of the distribution | ||
%d - Error | ||
%d - App is not part of the distribution | ||
`, ExitCodeSuccess, ExitCodeError, ExitCodeNotFound) | ||
|
||
type containsCmd struct { | ||
root *rootCmd | ||
*cobra.Command | ||
|
||
src string | ||
distribution string | ||
app string | ||
srcType fs.PathType | ||
} | ||
|
||
func newContainsCmd(root *rootCmd) *containsCmd { | ||
cmd := containsCmd{ | ||
Command: &cobra.Command{ | ||
Use: "contains ROOT-DIR|DEP-TREE-FILE DISTRIBUTION APP", | ||
Short: "Check if an app is part of a distribution", | ||
Args: cobra.ExactArgs(3), | ||
Long: strings.TrimSpace(containsLongHelp), | ||
}, | ||
root: root, | ||
} | ||
|
||
cmd.PreRunE = func(_ *cobra.Command, args []string) error { | ||
cmd.src = args[0] | ||
cmd.distribution = args[1] | ||
cmd.app = args[2] | ||
|
||
pType, err := fs.FileOrDir(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cmd.srcType = pType | ||
|
||
return nil | ||
} | ||
cmd.RunE = cmd.run | ||
|
||
return &cmd | ||
} | ||
|
||
func (c *containsCmd) run(*cobra.Command, []string) error { | ||
composition, err := c.root.loadComposition(c.srcType, c.src) | ||
if err != nil { | ||
return err | ||
} | ||
exists, err := composition.Contains(c.distribution, c.app) | ||
if err != nil { | ||
return fmt.Errorf("checking if %q is part of %q failed: %w", c.app, c.distribution, err) | ||
} | ||
|
||
if exists { | ||
fmt.Printf("%q is part of the distribution %q\n", c.app, c.distribution) | ||
return nil | ||
} | ||
|
||
fmt.Printf("%q is not part of the distribution %q\n", c.app, c.distribution) | ||
|
||
// do not print the error, result message has already been printed to | ||
// stdout | ||
c.SilenceErrors = true | ||
return NewErrWithExitCode(nil, ExitCodeNotFound) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package cmd | ||
|
||
import "fmt" | ||
|
||
type ErrWithExitCode struct { | ||
exitCode int | ||
err error | ||
} | ||
|
||
func NewErrWithExitCode(originalError error, exitCode int) *ErrWithExitCode { | ||
return &ErrWithExitCode{ | ||
exitCode: exitCode, | ||
err: originalError, | ||
} | ||
} | ||
|
||
func (e *ErrWithExitCode) Unwrap() error { | ||
return e.err | ||
} | ||
|
||
func (e *ErrWithExitCode) Error() string { | ||
if e.err == nil { | ||
return fmt.Sprintf("ErrWithExitCode: %d", e.exitCode) | ||
} | ||
|
||
return e.err.Error() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package cmd | ||
|
||
const ( | ||
ExitCodeSuccess = 0 | ||
ExitCodeError = 1 | ||
ExitCodeNotFound = 2 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters