Skip to content

Commit

Permalink
Merge pull request #13 from dpliakos/issue/10/add-the-rm-command
Browse files Browse the repository at this point in the history
Create the rm command
  • Loading branch information
dpliakos authored Jan 4, 2023
2 parents e1dba14 + de4c941 commit 6286b7f
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 4 deletions.
56 changes: 56 additions & 0 deletions cmd/rm.go
Original file line number Diff line number Diff line change
@@ -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 <env_name>`,
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)
}
11 changes: 7 additions & 4 deletions internal/jorge/jorge-errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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 {
Expand Down
83 changes: 83 additions & 0 deletions internal/jorge/jorge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -838,3 +874,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
}

0 comments on commit 6286b7f

Please sign in to comment.