diff --git a/README.md b/README.md index 30b348fc..9c289251 100644 --- a/README.md +++ b/README.md @@ -45,8 +45,8 @@ Removes a specific version $ fvm remove -Removes all versions except the current activated one +Removes all Flutter versions, and channels - $ fvm shake + $ fvm clean diff --git a/cmd/shake.go b/cmd/clean.go similarity index 90% rename from cmd/shake.go rename to cmd/clean.go index 1b6d4659..bce0ed72 100644 --- a/cmd/shake.go +++ b/cmd/clean.go @@ -20,9 +20,9 @@ import ( ) // pruneCmd represents the prune command -var shakeCmd = &cobra.Command{ - Use: "shake", - Short: "Remove all versions except the current version", +var cleanCmd = &cobra.Command{ + Use: "clean", + Short: "Remove all flutter versions", Run: func(cmd *cobra.Command, args []string) { lib.RemoveVersions() @@ -30,7 +30,7 @@ var shakeCmd = &cobra.Command{ } func init() { - rootCmd.AddCommand(shakeCmd) + rootCmd.AddCommand(cleanCmd) // Here you will define your flags and configuration settings. diff --git a/cmd/list.go b/cmd/list.go index 009250f3..e79a22bf 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -34,9 +34,9 @@ var listCmd = &cobra.Command{ if err != nil { log.Fatal(err) } - fmt.Println(len(vs)) + if len(vs) == 0 { - fmt.Println("No Flutter versions installed") + fmt.Println("No Flutter versions installed/n") os.Exit(1) } @@ -59,6 +59,7 @@ var listCmd = &cobra.Command{ } lib.LoadVersion(vs[i].Name) + }, } diff --git a/cmd/release.go b/cmd/release.go index 89638c79..da30b3d5 100644 --- a/cmd/release.go +++ b/cmd/release.go @@ -25,7 +25,7 @@ import ( // releaseCmd represents the release command var releaseCmd = &cobra.Command{ Use: "release", - Short: "Gets release by version", + Short: "Gets release by version number", Args: func(cmd *cobra.Command, args []string) error { if len(args) != 1 { return errors.New("What version would like to install?") diff --git a/cmd/root.go b/cmd/root.go index c143adfa..b145074a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,8 +16,11 @@ package cmd import ( "fmt" + "log" "os" + "github.com/leoafarias/fvm/lib" + "github.com/manifoldco/promptui" homedir "github.com/mitchellh/go-homedir" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -28,12 +31,41 @@ var cfgFile string // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ Use: "fvm", - Short: "A version management tool for Flutter", + Short: "Lists all the installed versions of Flutter", // Uncomment the following line if your bare application // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { - // fmt.Println(args) - // }, + Run: func(cmd *cobra.Command, args []string) { + // var options []string + vs, err := lib.ListVersions() + if err != nil { + log.Fatal(err) + } + + if len(vs) == 0 { + fmt.Println("No Flutter versions installed") + os.Exit(0) + } + + templates := promptui.SelectTemplates{ + Active: `👉 {{ .Name | cyan | bold }}`, + Inactive: ` {{ .Name | cyan }}`, + Selected: `{{ "✔" | green | bold }} {{ "Channel" | bold }}: {{ .Name | cyan }}`, + } + + list := promptui.Select{ + Label: "Choose Installed Versions", + Items: vs, + Templates: &templates, + } + + i, _, err := list.Run() + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + lib.LoadVersion(vs[i].Name) + }, } // Execute adds all child commands to the root command and sets flags appropriately. diff --git a/lib/fluttertools/fluttertools.go b/lib/fluttertools/fluttertools.go index cb5c4fca..52fb0a7a 100644 --- a/lib/fluttertools/fluttertools.go +++ b/lib/fluttertools/fluttertools.go @@ -11,6 +11,7 @@ import ( "time" "github.com/briandowns/spinner" + homedir "github.com/mitchellh/go-homedir" "github.com/ttacon/chalk" ) @@ -87,6 +88,29 @@ func runGit(execPath string, args ...string) (string, error) { } +// GetFlutterHome - Returns the home path to the flutter directory +func GetFlutterHome() string { + + var flutterHome string + envVar := os.Getenv("PATH") + v := strings.Split(envVar, ":") + for _, v := range v { + if strings.Contains(v, "flutter") { + flutterHome = strings.TrimSuffix(v, "/bin") + } + } + + if len(flutterHome) == 0 { + homeDir, _ := homedir.Dir() + flutterHome = path.Join(homeDir, "flutter") + } + + os.MkdirAll(flutterHome, os.ModePerm) + + return flutterHome + +} + // RunDoctor - runs 'flutter doctor' command func RunDoctor() { s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) @@ -102,48 +126,3 @@ func RunDoctor() { s.Stop() fmt.Println(chalk.Cyan.Color("[✓]"), "Flutter is setup") } - -// RunDoctor - runs 'flutter doctor' command -// func RunDoctor() { -// s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) -// s.Color("cyan", "bold") - -// s.Start() - -// cmd := exec.Command("flutter", "doctor") - -// c := make(chan struct{}) -// wg.Add(1) - -// go func(cmd *exec.Cmd, c chan struct{}) { -// s := spinner.New(spinner.CharSets[11], 100*time.Millisecond) -// s.Prefix = "Setting up Flutter" -// s.Color("cyan", "bold") -// s.Start() - -// defer wg.Done() -// stdout, err := cmd.StdoutPipe() -// if err != nil { -// panic(err) -// } -// <-c -// scanner := bufio.NewScanner(stdout) -// for scanner.Scan() { -// m := scanner.Text() -// // fmt.Println(m) -// if !strings.HasPrefix(m, " ") { -// s.Suffix = m -// } -// } - -// fmt.Println("[✓] " + "Setup Complete") - -// }(cmd, c) - -// c <- struct{}{} -// cmd.Start() - -// cmd.Wait() -// s.Stop() -// fmt.Println(chalk.Cyan.Color("[✓]"), "Flutter is setup") -// } diff --git a/lib/fvm.go b/lib/fvm.go index f42127ff..80f2bf46 100644 --- a/lib/fvm.go +++ b/lib/fvm.go @@ -16,10 +16,8 @@ import ( ) var ( - homeDir, _ = homedir.Dir() - workspaceHome = path.Join(homeDir, "fvm") - flutterHome = path.Join(homeDir, "flutter") - versionsDir = path.Join(workspaceHome, "versions") + flutterHome = fluttertools.GetFlutterHome() + workspaceHome = getWorkspaceHome() ) // Versions - slice of versions @@ -81,7 +79,7 @@ func setup(v Version) (Version, error) { // If directory doesnt exists get the channel if v.dir == false { - fluttertools.GetChannel(versionsDir, v.Name) + fluttertools.GetChannel(workspaceHome, v.Name) v.dir = true } @@ -113,7 +111,7 @@ func setup(v Version) (Version, error) { // ListVersions - lists all the current versions func ListVersions() (Versions, error) { var vs Versions - files, err := ioutil.ReadDir(versionsDir) + files, err := ioutil.ReadDir(workspaceHome) if err != nil { return Versions{}, err } @@ -125,7 +123,7 @@ func ListVersions() (Versions, error) { } dir := f.Name() - versionNumber, _ := fluttertools.GetVersionNumber(path.Join(versionsDir, dir)) + versionNumber, _ := fluttertools.GetVersionNumber(path.Join(workspaceHome, dir)) vs = append(vs, Version{ Name: dir, @@ -156,7 +154,7 @@ func ListVersions() (Versions, error) { // RemoveVersions - Remove all the files in the versionPath provided func RemoveVersions() error { - folders, err := filepath.Glob(filepath.Join(versionsDir, "*")) + folders, err := filepath.Glob(filepath.Join(workspaceHome, "*")) if err != nil { return err } @@ -181,7 +179,7 @@ func RemoveVersion(version string) error { if v.Name == version && v.Active { dirPath = flutterHome } else { - dirPath = path.Join(versionsDir, version) + dirPath = path.Join(workspaceHome, version) } } @@ -203,8 +201,7 @@ func toggleActive(fromActive bool, branch string) error { s.Start() if fromActive { - // toPath = path.Join(versionsDir, branch) - // fromPath = flutterHome + s.Suffix = "Deactivating version [" + branch + "]" if err := os.Remove(flutterHome); err != nil { log.Fatal(err) @@ -213,7 +210,7 @@ func toggleActive(fromActive bool, branch string) error { s.Suffix = "Deactivating version [" + branch + "]" os.Remove(flutterHome) - toPath = path.Join(versionsDir, branch) + toPath = path.Join(workspaceHome, branch) s.Suffix = "Activating version [" + branch + "]" err := os.Symlink(toPath, flutterHome) if err != nil { @@ -226,3 +223,10 @@ func toggleActive(fromActive bool, branch string) error { fmt.Println(chalk.Cyan.Color("[✓]"), s.Suffix) return nil } + +func getWorkspaceHome() string { + homeDir, _ := homedir.Dir() + workspaceHome := path.Join(homeDir, "fvm") + os.MkdirAll(workspaceHome, os.ModePerm) + return workspaceHome +}