diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 0912a3895..76b998e43 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -45,6 +45,11 @@ type Writer interface { Write(configs Configs) error } +type Deleter interface { + Delete() error +} + + type Configs struct { Language string `toml:"language"` Tutorial string `toml:"tutorial"` @@ -105,3 +110,11 @@ func (m Manager) Read() (Configs, error) { return c, nil } + +// Delete +func (m Manager) Delete() error { + if err := os.Remove(m.ConfigsPath); err != nil { + return err + } + return nil +} \ No newline at end of file diff --git a/pkg/cmd/uninstall.go b/pkg/cmd/uninstall.go index 778140bbf..c6f7b5bb0 100644 --- a/pkg/cmd/uninstall.go +++ b/pkg/cmd/uninstall.go @@ -5,29 +5,29 @@ import ( "fmt" "runtime" + "github.com/ZupIT/ritchie-cli/internal/pkg/config" "github.com/ZupIT/ritchie-cli/pkg/prompt" "github.com/ZupIT/ritchie-cli/pkg/stream" "github.com/spf13/cobra" ) -var ( - ErrCannotUninstall = errors.New("cannot remove rit") -) - // uninstallCmd type for uninstall command. type uninstallCmd struct { - inBool prompt.InputBool - file stream.FileRemover + inBool prompt.InputBool + file stream.FileRemover + configDeleter config.Deleter } // NewUninstallCmd creates a new cmd instance. func NewUninstallCmd( inBool prompt.InputBool, file stream.FileRemover, + configDeleter config.Deleter, ) *cobra.Command { c := uninstallCmd{ - inBool: inBool, - file: file, + inBool: inBool, + file: file, + configDeleter: configDeleter, } cmd := &cobra.Command{ @@ -40,19 +40,18 @@ func NewUninstallCmd( } cmd.LocalFlags() - return cmd } func (c uninstallCmd) runPrompt() CommandRunnerFunc { return func(cmd *cobra.Command, args []string) error { - sure, err := c.inBool.Bool("Are you sure", []string{"yes", "no"}) + sure, err := c.inBool.Bool("This will remove rit from your computer, are you sure about that?", []string{"yes", "no"}) if err != nil { fmt.Println(err) } if sure { - if err := c.uninstall(); err != nil { + if err := c.uninstall(runtime.GOOS); err != nil { fmt.Println(err) } } @@ -61,19 +60,36 @@ func (c uninstallCmd) runPrompt() CommandRunnerFunc { } } -func (c uninstallCmd) uninstall() error { - switch runtime.GOOS { +func (c uninstallCmd) uninstall(os string) error { + switch os { case "windows": fmt.Println("later") default: - if err := c.file.Remove("/usr/local/bin/rit"); err != nil { - return errors.New( - "Fail to uninstall\n" + - "Please try running this command again as root/Administrator\n" + - "Example: sudo rit uninstall", - ) + if err := c.removeBin(); err != nil { + return err } + if err := c.removeRitConfig(); err != nil { + return err + } + } + + return nil +} + +func (c uninstallCmd) removeBin() error { + if err := c.file.Remove("/usr/local/bin/rit"); err != nil { + return errors.New( + "Fail to uninstall\n" + + "Please try running this command again as root/Administrator\n" + + "Example: sudo rit uninstall", + ) } + return nil +} +func (c uninstallCmd) removeRitConfig() error { + if err := c.configDeleter.Delete(); err != nil { + return err + } return nil } diff --git a/pkg/cmd/uninstall_test.go b/pkg/cmd/uninstall_test.go new file mode 100644 index 000000000..318b6a732 --- /dev/null +++ b/pkg/cmd/uninstall_test.go @@ -0,0 +1,7 @@ +package cmd + +import "testing" + +func TestNewUninstallCmd(t *testing.T) { + +} \ No newline at end of file