From 1f58a2af555ccedaf9e7440a97b7326df7ee4710 Mon Sep 17 00:00:00 2001 From: Leo Farias Date: Sun, 3 Mar 2019 16:42:40 -0500 Subject: [PATCH] Made api a bit cleaner --- README.md | 6 ++-- cmd/list.go | 78 -------------------------------------------------- cmd/release.go | 59 -------------------------------------- cmd/root.go | 78 ++++++++++++++++++++++++++++++++------------------ lib/fvm.go | 5 +++- 5 files changed, 57 insertions(+), 169 deletions(-) delete mode 100644 cmd/list.go delete mode 100644 cmd/release.go diff --git a/README.md b/README.md index df014aff..09a8b0a0 100644 --- a/README.md +++ b/README.md @@ -32,15 +32,15 @@ Use `fvm ` to install and activate a version of Flutter. $ fvm stable $ fvm beta - $ fvm release 1.2.1 - $ fvm release 0.11.13 + $ fvm 1.2.1 + $ fvm 0.11.13 If `` has already been installed, `fvm` will activate it from cache without having to download and set up again. Lists all currently installed versions - $ fvm list + $ fvm v1.2.1 > v0.11.13 master diff --git a/cmd/list.go b/cmd/list.go deleted file mode 100644 index e79a22bf..00000000 --- a/cmd/list.go +++ /dev/null @@ -1,78 +0,0 @@ -// Copyright © 2019 NAME HERE -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cmd - -import ( - "fmt" - "log" - "os" - - "github.com/leoafarias/fvm/lib" - "github.com/manifoldco/promptui" - "github.com/spf13/cobra" -) - -// listCmd represents the list command -var listCmd = &cobra.Command{ - Use: "list", - Short: "Lists currently installed channels", - 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/n") - os.Exit(1) - } - - 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) - - }, -} - -func init() { - rootCmd.AddCommand(listCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // listCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} diff --git a/cmd/release.go b/cmd/release.go deleted file mode 100644 index da30b3d5..00000000 --- a/cmd/release.go +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright © 2019 NAME HERE -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package cmd - -import ( - "errors" - "strings" - - "github.com/leoafarias/fvm/lib" - "github.com/spf13/cobra" -) - -// releaseCmd represents the release command -var releaseCmd = &cobra.Command{ - Use: "release", - 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?") - } - - return nil - }, - Run: func(cmd *cobra.Command, args []string) { - var version string - if strings.HasPrefix(args[0], "v") { - version = args[0] - } else { - version = "v" + args[0] - } - lib.LoadVersion(version) - }, -} - -func init() { - rootCmd.AddCommand(releaseCmd) - - // Here you will define your flags and configuration settings. - - // Cobra supports Persistent Flags which will work for this command - // and all subcommands, e.g.: - // releaseCmd.PersistentFlags().String("foo", "", "A help for foo") - - // Cobra supports local flags which will only run when this command - // is called directly, e.g.: - // releaseCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") -} diff --git a/cmd/root.go b/cmd/root.go index b145074a..0fa32429 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -15,9 +15,11 @@ package cmd import ( + "errors" "fmt" "log" "os" + "strings" "github.com/leoafarias/fvm/lib" "github.com/manifoldco/promptui" @@ -32,39 +34,59 @@ var cfgFile string var rootCmd = &cobra.Command{ Use: "fvm", Short: "Lists all the installed versions of Flutter", + Args: func(cmd *cobra.Command, args []string) error { + if len(args) > 1 { + return errors.New("Can only accept one argument") + } + + return nil + }, // Uncomment the following line if your bare application // has an action associated with it: 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 + if len(args) == 1 { + var version string + if strings.HasPrefix(args[0], "v") { + version = args[0] + } else { + version = "v" + args[0] + } + lib.LoadVersion(version) + + } else { + + // 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) } - lib.LoadVersion(vs[i].Name) }, } diff --git a/lib/fvm.go b/lib/fvm.go index 80f2bf46..105c0f3d 100644 --- a/lib/fvm.go +++ b/lib/fvm.go @@ -79,7 +79,10 @@ func setup(v Version) (Version, error) { // If directory doesnt exists get the channel if v.dir == false { - fluttertools.GetChannel(workspaceHome, v.Name) + if err := fluttertools.GetChannel(workspaceHome, v.Name); err != nil { + log.Fatal(err) + return Version{}, err + } v.dir = true }