From de4c941ccc5d950586f7571b998f4720f38ae9e7 Mon Sep 17 00:00:00 2001 From: Dimitrios Pliakos Date: Sun, 25 Dec 2022 20:23:06 +0200 Subject: [PATCH] feat: Create the rm command --- .gitignore | 3 ++ cmd/rm.go | 56 +++++++++++++++++++++++ internal/jorge/jorge-errors.go | 11 +++-- internal/jorge/jorge.go | 83 ++++++++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 4 deletions(-) create mode 100644 cmd/rm.go diff --git a/.gitignore b/.gitignore index e24ea04..3c3fd30 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ build/* docs/jorge.1.html docs/jorge.1 + +.idea +.vscode diff --git a/cmd/rm.go b/cmd/rm.go new file mode 100644 index 0000000..4f4d24b --- /dev/null +++ b/cmd/rm.go @@ -0,0 +1,56 @@ +package cmd + +import ( + "fmt" + "os" + + "github.com/dpliakos/jorge/internal/jorge" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +// rmCmd represents the ls command +var rmCmd = &cobra.Command{ + Use: "rm", + Short: "Remove an environment", + Long: `Removes a configuration environment. + Usage: + + jorge rm `, + Run: func(cmd *cobra.Command, args []string) { + debug, _ := cmd.Flags().GetBool("debug") + + if debug { + log.SetLevel(log.DebugLevel) + } + + var selectedEnv string + if len(args) > 0 { + selectedEnv = args[0] + } else { + fmt.Fprintf(os.Stderr, "%s\n", "No environment specified") + os.Exit(1) + } + + if err := jorge.RemoveEnv(selectedEnv); err != nil { + if debug && err.OriginalErr != nil { + fmt.Fprintf(os.Stderr, "%s\n", err.OriginalErr.Error()) + } + + fmt.Fprintf(os.Stderr, "%s\n", err.Message) + fmt.Fprintf(os.Stderr, "%s\n", err.Solution) + + if err.Code > 0 { + os.Exit(err.Code) + } else { + os.Exit(1) + } + } else { + fmt.Println("Removed environment", selectedEnv) + } + }, +} + +func init() { + rootCmd.AddCommand(rmCmd) +} diff --git a/internal/jorge/jorge-errors.go b/internal/jorge/jorge-errors.go index 64d59c0..67a6850 100644 --- a/internal/jorge/jorge-errors.go +++ b/internal/jorge/jorge-errors.go @@ -8,7 +8,7 @@ type ErrorCode string type SolutionMessage string const ( - // System level errors + // E000 System level errors E000 ErrorCode = "The active directory does not exist" E001 ErrorCode = "Directory already exist" // Already exists E002 ErrorCode = "Path is not a directory" @@ -18,8 +18,9 @@ const ( E006 ErrorCode = "Could not open target file" E007 ErrorCode = "Could not write to file" E008 ErrorCode = "Error constructing path" + E009 ErrorCode = "Could not delete the target directory" - // Application level errors. >= 100 + // E100 Application level errors. >= 100 E100 = "Active directory does not belong in a jorge project" E101 = "Path to .jorge is not a directory" E102 = "Envs directory already exist" @@ -32,22 +33,24 @@ const ( E109 = "Environment already exist" E110 = "Could not store configuration file" E111 = "Environment does not exist" + E112 = "Can not delete the active environment" ) const ( - // system level messages + // S000 system level messages S000 SolutionMessage = "Please make sure the active directory is a valid path" S001 SolutionMessage = "Make sure %s is a directory" S002 SolutionMessage = "Make sure user %s has write access to the active directory" S003 SolutionMessage = "Make sure %s file exist and is readable by the user" - // application level messages + // S100 application level messages S100 = "Please run `jorge init` to initialize a jorge project" S101 = "Do not try to initialize a directory which is already a jorge project" S102 = "Backup configuration files manually by copying files under ./.jorge/envs and initialize the jorge project again using `jorge init`" S103 = "Make sure user %s has write privileges to .jorge directory" S104 = "Please use an environment name that is not already in use. You can use `jorge ls` to see the list of current environments" S105 = "You can create environment by running `jorge use -n %s`" + S106 = "Please select another environment or create a new one" ) func (e ErrorCode) Str() string { diff --git a/internal/jorge/jorge.go b/internal/jorge/jorge.go index 64f542c..03b23fc 100644 --- a/internal/jorge/jorge.go +++ b/internal/jorge/jorge.go @@ -564,6 +564,42 @@ func initializeJorgeProject(configFilePathFlag string) *EncapsulatedError { return nil } +func deleteJorgeEnv(env string) *EncapsulatedError { + jorgeDir, err := getJorgeDir() + if err != nil { + fmt.Println("Error") + return nil + } + + target := filepath.Join(jorgeDir, "envs", env) + + if _, err := os.Stat(target); err != nil { + encError := EncapsulatedError{ + OriginalErr: err, + Message: ErrorCode.Str(E002), + Solution: SolutionMessage.Str(S001, target), + Code: 2, + } + + return &encError + } + + removeErr := os.RemoveAll(target) + if removeErr != nil { + fmt.Println("could not remove ", target) + encError := EncapsulatedError{ + OriginalErr: removeErr, + Message: ErrorCode.Str(E009), + Solution: SolutionMessage.Str(S001, target), + Code: 9, + } + + return &encError + } + + return nil +} + // StoreConfigFile // It stores the current active user config file under an jorge environment name func StoreConfigFile(path string, envName string) (int64, *EncapsulatedError) { @@ -829,3 +865,50 @@ func RestoreEnv() *EncapsulatedError { return nil } } + +func RemoveEnv(envName string) *EncapsulatedError { + envs, err := getEnvs() + + if err != nil { + return err + } + + config, err := getInternalConfig() + if err != nil { + return err + } + + if envName == config.CurrentEnv { + encErr := EncapsulatedError{ + OriginalErr: ErrorCode.Err(E112), + Message: ErrorCode.Str(E112), + Solution: SolutionMessage.Str(S106), + Code: 112, + } + + return &encErr + } + + targetEnvFound := false + for _, fileName := range envs { + if fileName == envName { + targetEnvFound = true + } + } + + if !targetEnvFound { + encErr := EncapsulatedError{ + OriginalErr: ErrorCode.Err(E111), + Message: ErrorCode.Str(E111), + Code: 111, + } + + return &encErr + } + + if err = deleteJorgeEnv(envName); err != nil { + return err + } + + return nil +}