github.com/henvic/wedeploycli@v1.7.6-0.20200319005353-3630f582f284/command/uninstall/uninstall.go (about)

     1  package uninstall
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  	"strings"
    10  
    11  	"github.com/henvic/wedeploycli/color"
    12  	"github.com/henvic/wedeploycli/fancy"
    13  	"github.com/henvic/wedeploycli/userhome"
    14  	"github.com/henvic/wedeploycli/verbose"
    15  	"github.com/henvic/wedeploycli/waitlivemsg"
    16  	"github.com/spf13/cobra"
    17  )
    18  
    19  // UninstallCmd is used for uninstall this tool
    20  var UninstallCmd = &cobra.Command{
    21  	Use:   "uninstall",
    22  	Args:  cobra.NoArgs,
    23  	RunE:  uninstallRun,
    24  	Short: "Uninstall CLI",
    25  }
    26  
    27  var rmConfig bool
    28  
    29  func uninstall() error {
    30  	var exec, err = os.Executable()
    31  
    32  	if err != nil {
    33  		return err
    34  	}
    35  
    36  	return os.Remove(exec)
    37  }
    38  
    39  func uninstallChan(m *waitlivemsg.Message, ec chan error) {
    40  	var err = removeConfig()
    41  
    42  	if err != nil {
    43  		m.StopText(fancy.Error("Failed to remove user profile, configuration, and uninstall CLI [1/2]"))
    44  		ec <- err
    45  		return
    46  	}
    47  
    48  	err = uninstall()
    49  
    50  	if err != nil {
    51  		m.StopText(fancy.Error("Failed to uninstall the CLI [1/2]"))
    52  		ec <- err
    53  		return
    54  	}
    55  
    56  	m.StopText("Liferay Cloud Platform CLI uninstalled [2/2]\n" +
    57  		fancy.Info("Liferay Cloud Platform CLI is not working on your computer anymore.") + "\n" +
    58  		color.Format(color.FgHiYellow, "  For installing it again, type this command and press Enter:\n") +
    59  		color.Format(color.FgHiBlack, "  $ curl http://cdn.wedeploy.com/cli/latest/wedeploy.sh -sL | bash"))
    60  	ec <- err
    61  }
    62  
    63  func removeConfigOnlyChan(m *waitlivemsg.Message, ec chan error) {
    64  	err := removeConfig()
    65  
    66  	if err != nil {
    67  		m.StopText(fancy.Error("Failed to remove user profile and configuration [1/2]"))
    68  		ec <- err
    69  		return
    70  	}
    71  
    72  	m.StopText("User profile and configuration removed [2/2]")
    73  	ec <- nil
    74  }
    75  
    76  func removeConfig() error {
    77  	var homeDir = userhome.GetHomeDir()
    78  
    79  	var files = []string{
    80  		filepath.Join(homeDir, ".wedeploy"), // cache directory
    81  		filepath.Join(homeDir, ".lcp"),
    82  		filepath.Join(homeDir, ".lcp_autocomplete"),
    83  		filepath.Join(homeDir, ".lcp_metrics"),
    84  	}
    85  
    86  	var el []string
    87  
    88  	for _, f := range files {
    89  		verbose.Debug("Removing " + f)
    90  		err := os.RemoveAll(f)
    91  		if err != nil {
    92  			el = append(el, err.Error())
    93  		}
    94  	}
    95  
    96  	if len(el) == 0 {
    97  		return nil
    98  	}
    99  
   100  	return errors.New("can't remove all files: " + strings.Join(el, "\n"))
   101  }
   102  
   103  func uninstallRoutine(m *waitlivemsg.Message, ec chan error) {
   104  	if rmConfig {
   105  		removeConfigOnlyChan(m, ec)
   106  		return
   107  	}
   108  
   109  	uninstallChan(m, ec)
   110  }
   111  
   112  func uninstallRun(cmd *cobra.Command, args []string) error {
   113  	var m *waitlivemsg.Message
   114  	switch rmConfig {
   115  	case true:
   116  		m = waitlivemsg.NewMessage("Removing configuration files [1/2]")
   117  	default:
   118  		if runtime.GOOS == "windows" {
   119  			_, _ = fmt.Fprintln(os.Stderr, "Can't self-uninstall on Windows yet. Please remove the Liferay Cloud Platform CLI in the Control Panel.")
   120  		}
   121  
   122  		m = waitlivemsg.NewMessage("Uninstalling the Liferay Cloud Platform CLI [1/2]")
   123  	}
   124  
   125  	var wlm = waitlivemsg.New(nil)
   126  	go wlm.Wait()
   127  	wlm.AddMessage(m)
   128  	var ec = make(chan error, 1)
   129  	go uninstallRoutine(m, ec)
   130  	var err = <-ec
   131  	wlm.Stop()
   132  	return err
   133  }
   134  
   135  func init() {
   136  	UninstallCmd.Flags().BoolVar(&rmConfig, "rm-config", false, "Remove user profile and configuration only")
   137  }