From 6bc9c84579519e9ff32fb9f1f6bb17631505e8d1 Mon Sep 17 00:00:00 2001 From: Felicitas Pojtinger Date: Wed, 15 Dec 2021 02:14:15 +0100 Subject: [PATCH] refactor: Move inventory and operation commands into subcommands --- cmd/stbak/cmd/{find.go => inventory_find.go} | 6 +++--- cmd/stbak/cmd/{list.go => inventory_list.go} | 8 ++++---- cmd/stbak/cmd/inventory_root.go | 17 +++++++++++++++++ .../cmd/{archive.go => operation_archive.go} | 18 +++++++++--------- .../cmd/{delete.go => operation_delete.go} | 14 +++++++------- cmd/stbak/cmd/{move.go => operation_move.go} | 16 ++++++++-------- .../cmd/{restore.go => operation_restore.go} | 18 +++++++++--------- cmd/stbak/cmd/operation_root.go | 17 +++++++++++++++++ .../cmd/{update.go => operation_update.go} | 18 +++++++++--------- 9 files changed, 83 insertions(+), 49 deletions(-) rename cmd/stbak/cmd/{find.go => inventory_find.go} (83%) rename cmd/stbak/cmd/{list.go => inventory_list.go} (77%) create mode 100644 cmd/stbak/cmd/inventory_root.go rename cmd/stbak/cmd/{archive.go => operation_archive.go} (75%) rename cmd/stbak/cmd/{delete.go => operation_delete.go} (80%) rename cmd/stbak/cmd/{move.go => operation_move.go} (77%) rename cmd/stbak/cmd/{restore.go => operation_restore.go} (74%) create mode 100644 cmd/stbak/cmd/operation_root.go rename cmd/stbak/cmd/{update.go => operation_update.go} (73%) diff --git a/cmd/stbak/cmd/find.go b/cmd/stbak/cmd/inventory_find.go similarity index 83% rename from cmd/stbak/cmd/find.go rename to cmd/stbak/cmd/inventory_find.go index d0dc833..8d15284 100644 --- a/cmd/stbak/cmd/find.go +++ b/cmd/stbak/cmd/inventory_find.go @@ -13,7 +13,7 @@ const ( expressionFlag = "expression" ) -var findCmd = &cobra.Command{ +var inventoryFindCmd = &cobra.Command{ Use: "find", Aliases: []string{"fin", "f"}, Short: "Find a file or directory on tape or tar file by matching against a regex", @@ -44,9 +44,9 @@ var findCmd = &cobra.Command{ } func init() { - findCmd.PersistentFlags().StringP(expressionFlag, "x", "", "Regex to match the file/directory name against") + inventoryFindCmd.PersistentFlags().StringP(expressionFlag, "x", "", "Regex to match the file/directory name against") viper.AutomaticEnv() - rootCmd.AddCommand(findCmd) + inventoryCmd.AddCommand(inventoryFindCmd) } diff --git a/cmd/stbak/cmd/list.go b/cmd/stbak/cmd/inventory_list.go similarity index 77% rename from cmd/stbak/cmd/list.go rename to cmd/stbak/cmd/inventory_list.go index 7059c71..58cfc2f 100644 --- a/cmd/stbak/cmd/list.go +++ b/cmd/stbak/cmd/inventory_list.go @@ -9,10 +9,10 @@ import ( "github.com/spf13/viper" ) -var listCmd = &cobra.Command{ +var inventoryListCmd = &cobra.Command{ Use: "list", Aliases: []string{"lis", "l", "t", "ls"}, - Short: "List the contents of a directory on tape or tar file ", + Short: "List the contents of a directory on tape or tar file", RunE: func(cmd *cobra.Command, args []string) error { if err := viper.BindPFlags(cmd.PersistentFlags()); err != nil { return err @@ -40,9 +40,9 @@ var listCmd = &cobra.Command{ } func init() { - listCmd.PersistentFlags().StringP(nameFlag, "n", "", "Directory to list the contents of") + inventoryListCmd.PersistentFlags().StringP(nameFlag, "n", "", "Directory to list the contents of") viper.AutomaticEnv() - rootCmd.AddCommand(listCmd) + inventoryCmd.AddCommand(inventoryListCmd) } diff --git a/cmd/stbak/cmd/inventory_root.go b/cmd/stbak/cmd/inventory_root.go new file mode 100644 index 0000000..a051e68 --- /dev/null +++ b/cmd/stbak/cmd/inventory_root.go @@ -0,0 +1,17 @@ +package cmd + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var inventoryCmd = &cobra.Command{ + Use: "inventory", + Short: "Get contents and metadata of tape or tar file from the index", +} + +func init() { + viper.AutomaticEnv() + + rootCmd.AddCommand(inventoryCmd) +} diff --git a/cmd/stbak/cmd/archive.go b/cmd/stbak/cmd/operation_archive.go similarity index 75% rename from cmd/stbak/cmd/archive.go rename to cmd/stbak/cmd/operation_archive.go index 603b42b..48850aa 100644 --- a/cmd/stbak/cmd/archive.go +++ b/cmd/stbak/cmd/operation_archive.go @@ -24,7 +24,7 @@ const ( passwordFlag = "password" ) -var archiveCmd = &cobra.Command{ +var operationArchiveCmd = &cobra.Command{ Use: "archive", Aliases: []string{"arc", "a", "c"}, Short: "Archive a file or directory to tape or tar file", @@ -118,15 +118,15 @@ var archiveCmd = &cobra.Command{ } func init() { - archiveCmd.PersistentFlags().IntP(recordSizeFlag, "z", 20, "Amount of 512-bit blocks per record") - archiveCmd.PersistentFlags().StringP(fromFlag, "f", ".", "File or directory to archive") - archiveCmd.PersistentFlags().BoolP(overwriteFlag, "o", false, "Start writing from the start instead of from the end of the tape or tar file") - archiveCmd.PersistentFlags().StringP(compressionLevelFlag, "l", config.CompressionLevelBalanced, fmt.Sprintf("Compression level to use (default %v, available are %v)", config.CompressionLevelBalanced, config.KnownCompressionLevels)) - archiveCmd.PersistentFlags().StringP(recipientFlag, "r", "", "Path to public key of recipient to encrypt for") - archiveCmd.PersistentFlags().StringP(identityFlag, "i", "", "Path to private key to sign with") - archiveCmd.PersistentFlags().StringP(passwordFlag, "p", "", "Password for the private key") + operationArchiveCmd.PersistentFlags().IntP(recordSizeFlag, "z", 20, "Amount of 512-bit blocks per record") + operationArchiveCmd.PersistentFlags().StringP(fromFlag, "f", ".", "File or directory to archive") + operationArchiveCmd.PersistentFlags().BoolP(overwriteFlag, "o", false, "Start writing from the start instead of from the end of the tape or tar file") + operationArchiveCmd.PersistentFlags().StringP(compressionLevelFlag, "l", config.CompressionLevelBalanced, fmt.Sprintf("Compression level to use (default %v, available are %v)", config.CompressionLevelBalanced, config.KnownCompressionLevels)) + operationArchiveCmd.PersistentFlags().StringP(recipientFlag, "r", "", "Path to public key of recipient to encrypt for") + operationArchiveCmd.PersistentFlags().StringP(identityFlag, "i", "", "Path to private key to sign with") + operationArchiveCmd.PersistentFlags().StringP(passwordFlag, "p", "", "Password for the private key") viper.AutomaticEnv() - rootCmd.AddCommand(archiveCmd) + operationCmd.AddCommand(operationArchiveCmd) } diff --git a/cmd/stbak/cmd/delete.go b/cmd/stbak/cmd/operation_delete.go similarity index 80% rename from cmd/stbak/cmd/delete.go rename to cmd/stbak/cmd/operation_delete.go index 8f91434..c82f94e 100644 --- a/cmd/stbak/cmd/delete.go +++ b/cmd/stbak/cmd/operation_delete.go @@ -15,7 +15,7 @@ const ( nameFlag = "name" ) -var deleteCmd = &cobra.Command{ +var operationDeleteCmd = &cobra.Command{ Use: "delete", Aliases: []string{"del", "d", "rm"}, Short: "Delete a file or directory from tape or tar file", @@ -97,13 +97,13 @@ var deleteCmd = &cobra.Command{ } func init() { - deleteCmd.PersistentFlags().IntP(recordSizeFlag, "z", 20, "Amount of 512-bit blocks per record") - deleteCmd.PersistentFlags().StringP(nameFlag, "n", "", "Name of the file to remove") - deleteCmd.PersistentFlags().StringP(recipientFlag, "r", "", "Path to public key of recipient to encrypt for") - deleteCmd.PersistentFlags().StringP(identityFlag, "i", "", "Path to private key to sign with") - deleteCmd.PersistentFlags().StringP(passwordFlag, "p", "", "Password for the private key") + operationDeleteCmd.PersistentFlags().IntP(recordSizeFlag, "z", 20, "Amount of 512-bit blocks per record") + operationDeleteCmd.PersistentFlags().StringP(nameFlag, "n", "", "Name of the file to remove") + operationDeleteCmd.PersistentFlags().StringP(recipientFlag, "r", "", "Path to public key of recipient to encrypt for") + operationDeleteCmd.PersistentFlags().StringP(identityFlag, "i", "", "Path to private key to sign with") + operationDeleteCmd.PersistentFlags().StringP(passwordFlag, "p", "", "Password for the private key") viper.AutomaticEnv() - rootCmd.AddCommand(deleteCmd) + operationCmd.AddCommand(operationDeleteCmd) } diff --git a/cmd/stbak/cmd/move.go b/cmd/stbak/cmd/operation_move.go similarity index 77% rename from cmd/stbak/cmd/move.go rename to cmd/stbak/cmd/operation_move.go index 1b7fcd3..f5e005a 100644 --- a/cmd/stbak/cmd/move.go +++ b/cmd/stbak/cmd/operation_move.go @@ -11,7 +11,7 @@ import ( "github.com/spf13/viper" ) -var moveCmd = &cobra.Command{ +var operationMoveCmd = &cobra.Command{ Use: "move", Aliases: []string{"mov", "m", "mv"}, Short: "Move a file or directory on tape or tar file", @@ -93,14 +93,14 @@ var moveCmd = &cobra.Command{ } func init() { - moveCmd.PersistentFlags().IntP(recordSizeFlag, "z", 20, "Amount of 512-bit blocks per record") - moveCmd.PersistentFlags().StringP(fromFlag, "f", "", "Current path of the file or directory to move") - moveCmd.PersistentFlags().StringP(toFlag, "t", "", "Path to move the file or directory to") - moveCmd.PersistentFlags().StringP(recipientFlag, "r", "", "Path to public key of recipient to encrypt for") - moveCmd.PersistentFlags().StringP(identityFlag, "i", "", "Path to private key to sign with") - moveCmd.PersistentFlags().StringP(passwordFlag, "p", "", "Password for the private key") + operationMoveCmd.PersistentFlags().IntP(recordSizeFlag, "z", 20, "Amount of 512-bit blocks per record") + operationMoveCmd.PersistentFlags().StringP(fromFlag, "f", "", "Current path of the file or directory to move") + operationMoveCmd.PersistentFlags().StringP(toFlag, "t", "", "Path to move the file or directory to") + operationMoveCmd.PersistentFlags().StringP(recipientFlag, "r", "", "Path to public key of recipient to encrypt for") + operationMoveCmd.PersistentFlags().StringP(identityFlag, "i", "", "Path to private key to sign with") + operationMoveCmd.PersistentFlags().StringP(passwordFlag, "p", "", "Password for the private key") viper.AutomaticEnv() - rootCmd.AddCommand(moveCmd) + operationCmd.AddCommand(operationMoveCmd) } diff --git a/cmd/stbak/cmd/restore.go b/cmd/stbak/cmd/operation_restore.go similarity index 74% rename from cmd/stbak/cmd/restore.go rename to cmd/stbak/cmd/operation_restore.go index cc808bb..dd3d9ec 100644 --- a/cmd/stbak/cmd/restore.go +++ b/cmd/stbak/cmd/operation_restore.go @@ -15,7 +15,7 @@ const ( flattenFlag = "flatten" ) -var restoreCmd = &cobra.Command{ +var operationRestoreCmd = &cobra.Command{ Use: "restore", Aliases: []string{"res", "r", "x"}, Short: "Restore a file or directory from tape or tar file", @@ -101,15 +101,15 @@ var restoreCmd = &cobra.Command{ } func init() { - restoreCmd.PersistentFlags().IntP(recordSizeFlag, "z", 20, "Amount of 512-bit blocks per record") - restoreCmd.PersistentFlags().StringP(fromFlag, "f", "", "File or directory to restore") - restoreCmd.PersistentFlags().StringP(toFlag, "t", "", "File or directory restore to (archived name by default)") - restoreCmd.PersistentFlags().BoolP(flattenFlag, "a", false, "Ignore the folder hierarchy on the tape or tar file") - restoreCmd.PersistentFlags().StringP(identityFlag, "i", "", "Path to private key of recipient that has been encrypted for") - restoreCmd.PersistentFlags().StringP(passwordFlag, "p", "", "Password for the private key") - restoreCmd.PersistentFlags().StringP(recipientFlag, "r", "", "Path to the public key to verify with") + operationRestoreCmd.PersistentFlags().IntP(recordSizeFlag, "z", 20, "Amount of 512-bit blocks per record") + operationRestoreCmd.PersistentFlags().StringP(fromFlag, "f", "", "File or directory to restore") + operationRestoreCmd.PersistentFlags().StringP(toFlag, "t", "", "File or directory restore to (archived name by default)") + operationRestoreCmd.PersistentFlags().BoolP(flattenFlag, "a", false, "Ignore the folder hierarchy on the tape or tar file") + operationRestoreCmd.PersistentFlags().StringP(identityFlag, "i", "", "Path to private key of recipient that has been encrypted for") + operationRestoreCmd.PersistentFlags().StringP(passwordFlag, "p", "", "Password for the private key") + operationRestoreCmd.PersistentFlags().StringP(recipientFlag, "r", "", "Path to the public key to verify with") viper.AutomaticEnv() - rootCmd.AddCommand(restoreCmd) + operationCmd.AddCommand(operationRestoreCmd) } diff --git a/cmd/stbak/cmd/operation_root.go b/cmd/stbak/cmd/operation_root.go new file mode 100644 index 0000000..83e8764 --- /dev/null +++ b/cmd/stbak/cmd/operation_root.go @@ -0,0 +1,17 @@ +package cmd + +import ( + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +var operationCmd = &cobra.Command{ + Use: "operation", + Short: "Perform operations on tape or tar file and the index", +} + +func init() { + viper.AutomaticEnv() + + rootCmd.AddCommand(operationCmd) +} diff --git a/cmd/stbak/cmd/update.go b/cmd/stbak/cmd/operation_update.go similarity index 73% rename from cmd/stbak/cmd/update.go rename to cmd/stbak/cmd/operation_update.go index 97e7b5f..8c1b8ea 100644 --- a/cmd/stbak/cmd/update.go +++ b/cmd/stbak/cmd/operation_update.go @@ -14,7 +14,7 @@ import ( "github.com/spf13/viper" ) -var updateCmd = &cobra.Command{ +var operationUpdateCmd = &cobra.Command{ Use: "update", Aliases: []string{"upd", "u"}, Short: "Update a file or directory's content and metadata on tape or tar file", @@ -108,15 +108,15 @@ var updateCmd = &cobra.Command{ } func init() { - updateCmd.PersistentFlags().IntP(recordSizeFlag, "z", 20, "Amount of 512-bit blocks per record") - updateCmd.PersistentFlags().StringP(fromFlag, "f", "", "Path of the file or directory to update") - updateCmd.PersistentFlags().BoolP(overwriteFlag, "o", false, "Replace the content on the tape or tar file") - updateCmd.PersistentFlags().StringP(compressionLevelFlag, "l", config.CompressionLevelBalanced, fmt.Sprintf("Compression level to use (default %v, available are %v)", config.CompressionLevelBalanced, config.KnownCompressionLevels)) - updateCmd.PersistentFlags().StringP(recipientFlag, "r", "", "Path to public key of recipient to encrypt for") - updateCmd.PersistentFlags().StringP(identityFlag, "i", "", "Path to private key to sign with") - updateCmd.PersistentFlags().StringP(passwordFlag, "p", "", "Password for the private key") + operationUpdateCmd.PersistentFlags().IntP(recordSizeFlag, "z", 20, "Amount of 512-bit blocks per record") + operationUpdateCmd.PersistentFlags().StringP(fromFlag, "f", "", "Path of the file or directory to update") + operationUpdateCmd.PersistentFlags().BoolP(overwriteFlag, "o", false, "Replace the content on the tape or tar file") + operationUpdateCmd.PersistentFlags().StringP(compressionLevelFlag, "l", config.CompressionLevelBalanced, fmt.Sprintf("Compression level to use (default %v, available are %v)", config.CompressionLevelBalanced, config.KnownCompressionLevels)) + operationUpdateCmd.PersistentFlags().StringP(recipientFlag, "r", "", "Path to public key of recipient to encrypt for") + operationUpdateCmd.PersistentFlags().StringP(identityFlag, "i", "", "Path to private key to sign with") + operationUpdateCmd.PersistentFlags().StringP(passwordFlag, "p", "", "Password for the private key") viper.AutomaticEnv() - rootCmd.AddCommand(updateCmd) + operationCmd.AddCommand(operationUpdateCmd) }