refactor: Move inventory and operation commands into subcommands

This commit is contained in:
Felicitas Pojtinger
2021-12-15 02:14:15 +01:00
parent ae7e418891
commit 6bc9c84579
9 changed files with 83 additions and 49 deletions

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)
}