Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Create structs for each tools command to better isolate commands - cont'd #3340

Merged
merged 2 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/cmd/common/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,8 @@ var (
vConfigError error
)

// InitViper initializes the viper singleton for the CLI
func InitViper() *viper.Viper {
// Already initialized by some other command
if v != nil {
return v
}

// initializes the viper singleton for the CLI
func initViper() *viper.Viper {
v = viper.New()

// Skip for vendor-only commands or the version command
Expand Down Expand Up @@ -159,6 +154,10 @@ func InitViper() *viper.Viper {

// GetViper returns the viper singleton
func GetViper() *viper.Viper {
if v == nil {
v = initViper()
}

return v
}

Expand Down
2 changes: 1 addition & 1 deletion src/cmd/initialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewInitCommand() *cobra.Command {
RunE: o.Run,
}

v := common.InitViper()
v := common.GetViper()

// Init package variable defaults that are non-zero values
// NOTE: these are not in common.setDefaults so that zarf tools update-creds does not erroneously update values back to the default
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewPackageCommand() *cobra.Command {
Short: lang.CmdPackageShort,
}

v := common.InitViper()
v := common.GetViper()

persistentFlags := cmd.PersistentFlags()
persistentFlags.IntVar(&config.CommonOptions.OCIConcurrency, "oci-concurrency", v.GetInt(common.VPkgOCIConcurrency), lang.CmdPackageFlagConcurrency)
Expand Down
12 changes: 8 additions & 4 deletions src/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ func NewZarfCommand() *cobra.Command {
Run: run,
}

// Add the tools commands
// IMPORTANT: we need to make sure the tools command are added first
// to ensure the config defaulting doesn't kick in, and inject values
// into zart tools update-creds command
// see https://github.com/zarf-dev/zarf/pull/3340#discussion_r1889221826
rootCmd.AddCommand(tools.NewToolsCommand())

// TODO(soltysh): consider adding command groups
rootCmd.AddCommand(NewConnectCommand())
rootCmd.AddCommand(NewDestroyCommand())
Expand Down Expand Up @@ -170,15 +177,12 @@ func Execute(ctx context.Context) {
}

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

// Skip for vendor-only commands
if common.CheckVendorOnlyFromArgs() {
return
}

v := common.InitViper()
v := common.GetViper()

// Logs
rootCmd.PersistentFlags().StringVarP(&LogLevelCLI, "log-level", "l", v.GetString(common.VLogLevel), lang.RootCmdFlagLogLevel)
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
Loading