Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
added rit config remove and bin remove
Browse files Browse the repository at this point in the history
  • Loading branch information
victor-schumacher committed Oct 8, 2021
1 parent 0628aeb commit dad8215
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
13 changes: 13 additions & 0 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
}
54 changes: 35 additions & 19 deletions pkg/cmd/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
}
}
Expand All @@ -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
}
7 changes: 7 additions & 0 deletions pkg/cmd/uninstall_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package cmd

import "testing"

func TestNewUninstallCmd(t *testing.T) {

}

0 comments on commit dad8215

Please sign in to comment.