Skip to content

Commit

Permalink
refactor: Create structs for each tools command to better isolate com…
Browse files Browse the repository at this point in the history
…mands

Signed-off-by: Maciej Szulik <[email protected]>
  • Loading branch information
soltysh committed Dec 17, 2024
1 parent 291eaa8 commit be04e94
Show file tree
Hide file tree
Showing 11 changed files with 758 additions and 586 deletions.
5 changes: 2 additions & 3 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ func NewZarfCommand() *cobra.Command {
rootCmd.AddCommand(NewInitCommand())
rootCmd.AddCommand(NewInternalCommand(rootCmd))
rootCmd.AddCommand(NewPackageCommand())
// Add the tools commands
rootCmd.AddCommand(tools.NewToolsCommand())

return rootCmd
}
Expand Down Expand Up @@ -170,9 +172,6 @@ func Execute(ctx context.Context) {
}

func init() {
// Add the tools commands
tools.Include(rootCmd)

// Skip for vendor-only commands
if common.CheckVendorOnlyFromArgs() {
return
Expand Down
156 changes: 92 additions & 64 deletions src/cmd/tools/archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,80 +19,108 @@ import (
// ldflags github.com/zarf-dev/zarf/src/cmd/tools.archiverVersion=x.x.x
var archiverVersion string

var archiverCmd = &cobra.Command{
Use: "archiver",
Aliases: []string{"a"},
Short: lang.CmdToolsArchiverShort,
Version: archiverVersion,
// NewArchiverCommand creates the `tools archiver` sub-command and its nested children.
func NewArchiverCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "archiver",
Aliases: []string{"a"},
Short: lang.CmdToolsArchiverShort,
Version: archiverVersion,
}

cmd.AddCommand(NewArchiverCompressCommand())
cmd.AddCommand(NewArchiverDecompressCommand())
cmd.AddCommand(newVersionCmd("mholt/archiver", archiverVersion))

return cmd
}

var archiverCompressCmd = &cobra.Command{
Use: "compress SOURCES ARCHIVE",
Aliases: []string{"c"},
Short: lang.CmdToolsArchiverCompressShort,
Args: cobra.MinimumNArgs(2),
RunE: func(_ *cobra.Command, args []string) error {
sourceFiles, destinationArchive := args[:len(args)-1], args[len(args)-1]
err := archiver.Archive(sourceFiles, destinationArchive)
if err != nil {
return fmt.Errorf("unable to perform compression: %w", err)
}
return err
},
// ArchiverCompressOptions holds the command-line options for 'tools archiver compress' sub-command.
type ArchiverCompressOptions struct{}

// NewArchiverCompressCommand creates the `tools archiver compress` sub-command.
func NewArchiverCompressCommand() *cobra.Command {
o := ArchiverCompressOptions{}

cmd := &cobra.Command{
Use: "compress SOURCES ARCHIVE",
Aliases: []string{"c"},
Short: lang.CmdToolsArchiverCompressShort,
Args: cobra.MinimumNArgs(2),
RunE: o.Run,
}

return cmd
}

// Run performs the execution of 'tools archiver compress' sub-command.
func (o *ArchiverCompressOptions) Run(_ *cobra.Command, args []string) error {
sourceFiles, destinationArchive := args[:len(args)-1], args[len(args)-1]
err := archiver.Archive(sourceFiles, destinationArchive)
if err != nil {
return fmt.Errorf("unable to perform compression: %w", err)
}
return err
}

var unarchiveAll bool
// ArchiverDecompressOptions holds the command-line options for 'tools archiver decompress' sub-command.
type ArchiverDecompressOptions struct {
unarchiveAll bool
}

// NewArchiverDecompressCommand creates the `tools archiver decompress` sub-command.
func NewArchiverDecompressCommand() *cobra.Command {
o := ArchiverDecompressOptions{}

cmd := &cobra.Command{
Use: "decompress ARCHIVE DESTINATION",
Aliases: []string{"d"},
Short: lang.CmdToolsArchiverDecompressShort,
Args: cobra.ExactArgs(2),
RunE: o.Run,
}

var archiverDecompressCmd = &cobra.Command{
Use: "decompress ARCHIVE DESTINATION",
Aliases: []string{"d"},
Short: lang.CmdToolsArchiverDecompressShort,
Args: cobra.ExactArgs(2),
RunE: func(_ *cobra.Command, args []string) error {
sourceArchive, destinationPath := args[0], args[1]
err := archiver.Unarchive(sourceArchive, destinationPath)
cmd.Flags().BoolVar(&o.unarchiveAll, "decompress-all", false, "Decompress all tarballs in the archive")
cmd.Flags().BoolVar(&o.unarchiveAll, "unarchive-all", false, "Unarchive all tarballs in the archive")
cmd.MarkFlagsMutuallyExclusive("decompress-all", "unarchive-all")
cmd.Flags().MarkHidden("decompress-all")

return cmd
}

// Run performs the execution of 'tools archiver decompress' sub-command.
func (o *ArchiverDecompressOptions) Run(_ *cobra.Command, args []string) error {
sourceArchive, destinationPath := args[0], args[1]
err := archiver.Unarchive(sourceArchive, destinationPath)
if err != nil {
return fmt.Errorf("unable to perform decompression: %w", err)
}
if !o.unarchiveAll {
return nil
}
err = filepath.Walk(destinationPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return fmt.Errorf("unable to perform decompression: %w", err)
}
if !unarchiveAll {
return nil
return err
}
err = filepath.Walk(destinationPath, func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".tar") {
dst := filepath.Join(strings.TrimSuffix(path, ".tar"), "..")
// Unpack sboms.tar differently since it has a different folder structure than components
if info.Name() == layout.SBOMTar {
dst = strings.TrimSuffix(path, ".tar")
}
err := archiver.Unarchive(path, dst)
if err != nil {
return err
return fmt.Errorf(lang.ErrUnarchive, path, err.Error())
}
if strings.HasSuffix(path, ".tar") {
dst := filepath.Join(strings.TrimSuffix(path, ".tar"), "..")
// Unpack sboms.tar differently since it has a different folder structure than components
if info.Name() == layout.SBOMTar {
dst = strings.TrimSuffix(path, ".tar")
}
err := archiver.Unarchive(path, dst)
if err != nil {
return fmt.Errorf(lang.ErrUnarchive, path, err.Error())
}
err = os.Remove(path)
if err != nil {
return fmt.Errorf(lang.ErrRemoveFile, path, err.Error())
}
err = os.Remove(path)
if err != nil {
return fmt.Errorf(lang.ErrRemoveFile, path, err.Error())
}
return nil
})
if err != nil {
return fmt.Errorf("unable to unarchive all nested tarballs: %w", err)
}
return nil
},
}

func init() {
toolsCmd.AddCommand(archiverCmd)

archiverCmd.AddCommand(archiverCompressCmd)
archiverCmd.AddCommand(archiverDecompressCmd)
archiverCmd.AddCommand(newVersionCmd("mholt/archiver", archiverVersion))
archiverDecompressCmd.Flags().BoolVar(&unarchiveAll, "decompress-all", false, "Decompress all tarballs in the archive")
archiverDecompressCmd.Flags().BoolVar(&unarchiveAll, "unarchive-all", false, "Unarchive all tarballs in the archive")
archiverDecompressCmd.MarkFlagsMutuallyExclusive("decompress-all", "unarchive-all")
archiverDecompressCmd.Flags().MarkHidden("decompress-all")
})
if err != nil {
return fmt.Errorf("unable to unarchive all nested tarballs: %w", err)
}
return nil
}
35 changes: 26 additions & 9 deletions src/cmd/tools/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,36 @@ import (
"fmt"

"github.com/spf13/cobra"

"github.com/zarf-dev/zarf/src/cmd/common"
"github.com/zarf-dev/zarf/src/config/lang"
)

var toolsCmd = &cobra.Command{
Use: "tools",
Aliases: []string{"t"},
Short: lang.CmdToolsShort,
}
// NewToolsCommand creates the `tools` sub-command and its nested children.
func NewToolsCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "tools",
Aliases: []string{"t"},
Short: lang.CmdToolsShort,
}

v := common.GetViper()

cmd.AddCommand(NewArchiverCommand())
cmd.AddCommand(NewRegistryCommand())
cmd.AddCommand(NewHelmCommand())
cmd.AddCommand(NewK9sCommand())
cmd.AddCommand(NewKubectlCommand())
cmd.AddCommand(NewSbomCommand())
cmd.AddCommand(NewWaitForCommand())
cmd.AddCommand(NewYQCommand())
cmd.AddCommand(NewGetCredsCommand())
cmd.AddCommand(NewUpdateCredsCommand(v))
cmd.AddCommand(NewClearCacheCommand())
cmd.AddCommand(NewDownloadInitCommand())
cmd.AddCommand(NewGenPKICommand())
cmd.AddCommand(NewGenKeyCommand())

// Include adds the tools command to the root command.
func Include(rootCmd *cobra.Command) {
rootCmd.AddCommand(toolsCmd)
return cmd
}

// newVersionCmd is a generic version command for tools
Expand Down
Loading

0 comments on commit be04e94

Please sign in to comment.