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

Create uninstall command #1052

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
95 changes: 95 additions & 0 deletions pkg/cmd/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cmd

import (
"errors"
"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"
)

// uninstallCmd type for uninstall command.
type uninstallCmd struct {
inBool prompt.InputBool
file stream.FileRemover
configManager config.Manager
}

// NewUninstallCmd creates a new cmd instance.
func NewUninstallCmd(
inBool prompt.InputBool,
file stream.FileRemover,
configDeleter config.Manager,
) *cobra.Command {
c := uninstallCmd{
inBool: inBool,
file: file,
configManager: configDeleter,
}

cmd := &cobra.Command{
Use: "uninstall",
Short: "Uninstall rit",
Example: "rit uninstall",
RunE: c.runPrompt(),
ValidArgs: []string{""},
Args: cobra.OnlyValidArgs,
}

cmd.LocalFlags()
return cmd
}

func (c uninstallCmd) runPrompt() CommandRunnerFunc {
return func(cmd *cobra.Command, args []string) error {
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(runtime.GOOS); err != nil {
fmt.Println(err)
}
}

return nil
}
}

func (c uninstallCmd) uninstall(os string) error {
switch os {
case "windows":
fmt.Println("later")
default:
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.configManager.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) {

}
2 changes: 2 additions & 0 deletions pkg/commands/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ func Build() *cobra.Command {
metricsCmd := cmd.NewMetricsCmd(fileManager, inputList)
tutorialCmd := cmd.NewTutorialCmd(inputList, tutorialFindSetter)
renameCmd := cmd.NewRenameCmd()
uninstallCmd := cmd.NewUninstallCmd(inputBool, fileManager, ritConfigManager)

// level 2
setCredentialCmd := cmd.NewSetCredentialCmd(
Expand Down Expand Up @@ -289,6 +290,7 @@ func Build() *cobra.Command {
createCmd,
deleteCmd,
initCmd,
uninstallCmd,
listCmd,
setCmd,
showCmd,
Expand Down