diff --git a/README.md b/README.md index 5e19de7..0ed1f06 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,11 @@ FVM gives you the ability to install many Flutter **releases** or **channels**. Use `master` to install the Master channel and `v1.8.0` to install the release. +Use `--repo` to install flutter from unoffical repo +```bash +> fvm install master --repo https://github.com/flutter/flutter.git +``` + ### Import Flutter from previous installation FVM has subcommand `import` which can import your previous installed flutter into fvm. diff --git a/cmd/import.go b/cmd/import.go index 58c1302..06c52dc 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -44,7 +44,7 @@ var importCommand = &cobra.Command{ "Or you can use flags --path to special the path of flutter", Args: func(cmd *cobra.Command, args []string) error { if len(args) == 0 { - return errors.New("need to provide a channel or a version or other name as import name, you can use `master` `beta` or `alibaba` `baidu` etc.") + return errors.New("need to provide a channel or a version or other name as import name, you can use `master` `beta` or `alibaba` `baidu` etc") } if len(args) > 1 { return errors.New("allows only one argument, the name to be imported as") diff --git a/cmd/install.go b/cmd/install.go index 67951ab..db9d7cf 100644 --- a/cmd/install.go +++ b/cmd/install.go @@ -24,7 +24,10 @@ import ( "github.com/spf13/cobra" ) +var repo string + func init() { + installCommand.Flags().StringVar(&repo, "repo", "", "install flutter from unoffical git repo") rootCmd.AddCommand(installCommand) } @@ -72,7 +75,10 @@ var installCommand = &cobra.Command{ err := fvmgo.CheckIfGitExists() if err == nil { version := args[0] - if fvmgo.IsValidFlutterChannel(version) { + if len(repo) > 0 { + fvmgo.Infof("Install flutter <%s> from repo %s", version, repo) + err = fvmgo.FlutterRepoClone(version, repo) + } else if fvmgo.IsValidFlutterChannel(version) { err = fvmgo.FlutterChannelClone(version) } else if !maybeVersion(version) { fvmgo.Errorf("It seems that you want install a Flutter channel but have a invalid channel") diff --git a/fvmgo/tool.go b/fvmgo/tool.go index b7b1600..38a00e7 100644 --- a/fvmgo/tool.go +++ b/fvmgo/tool.go @@ -55,7 +55,7 @@ func ProcessRunner(cmd string, dir string, arg ...string) error { if len(dir) == 0 { cwd, err := os.Getwd() if err != nil { - return errors.New(fmt.Sprintf("Cannot get work directory: %v", err)) + return fmt.Errorf("Cannot get work directory: %v", err) } runner.Dir = cwd } else { @@ -67,7 +67,7 @@ func ProcessRunner(cmd string, dir string, arg ...string) error { err := runner.Run() if err != nil { - return errors.New(fmt.Sprintf("Command '%s' exited with error: %v", cmd, err)) + return fmt.Errorf("Command '%s' exited with error: %v", cmd, err) } return nil } @@ -111,7 +111,7 @@ func CurrentVersion() (string, error) { } dst, err := os.Readlink(link) if err != nil { - return "", errors.New(fmt.Sprintf("Cannot read link target: %v", err)) + return "", fmt.Errorf("Cannot read link target: %v", err) } return filepath.Base(dst), nil } @@ -148,9 +148,30 @@ func checkInstalledCorrectly(version string) bool { return true } +func FlutterRepoClone(version string, repo string) error { + if checkInstalledCorrectly(version) { + Warnf("Flutter version %s is already installed", version) + return nil + } + + versionDir := filepath.Join(VersionsDir(), version) + Verbosef("Installing Flutter sdk %s to cache directory %s", version, versionDir) + + err := os.MkdirAll(versionDir, 0755) + if err != nil { + return fmt.Errorf("Cannot creat directory for version %s: %v", version, err) + } + err = ProcessRunner("git", versionDir, "clone", "-b", version, repo, versionDir) + if err != nil { + return err + } + Infof("Successfully installed flutter %s from %s", version, repo) + return nil +} + func FlutterChannelClone(channel string) error { if !IsValidFlutterChannel(channel) { - return errors.New(fmt.Sprintf("%s is not a valid flutter channel", channel)) + return fmt.Errorf("%s is not a valid flutter channel", channel) } Verbosef("%s is a valid flutter channel", channel) @@ -162,9 +183,9 @@ func FlutterChannelClone(channel string) error { Verbosef("Installing Flutter sdk %s to cache directory %s", channel, channelDir) err := os.MkdirAll(channelDir, 0755) if err != nil { - return errors.New(fmt.Sprintf("Cannot create directory for channel %s: %v", channel, err)) + return fmt.Errorf("Cannot create directory for channel %s: %v", channel, err) } - err = ProcessRunner("git", channelDir, "clone", "-b", channel, flutterRepo, ".") + err = ProcessRunner("git", channelDir, "clone", "-b", channel, flutterRepo, channelDir) if err != nil { return err } @@ -174,7 +195,7 @@ func FlutterChannelClone(channel string) error { func FlutterVersionClone(version string) error { if !IsValidFlutterVersion(version) { - return errors.New(fmt.Sprintf("%s is not a valid version", version)) + return fmt.Errorf("%s is not a valid version", version) } Verbosef("%s is a valid flutter version", version) if checkInstalledCorrectly(version) { @@ -187,9 +208,9 @@ func FlutterVersionClone(version string) error { err := os.MkdirAll(versionDir, 0755) if err != nil { - return errors.New(fmt.Sprintf("Cannot creat directory for version %s: %v", version, err)) + return fmt.Errorf("Cannot creat directory for version %s: %v", version, err) } - err = ProcessRunner("git", versionDir, "clone", "-b", version, flutterRepo, ".") + err = ProcessRunner("git", versionDir, "clone", "-b", version, flutterRepo, versionDir) if err != nil { return err } @@ -318,7 +339,7 @@ func projectFlutterLink(dir string, depth int) string { return "" } - depth -= 1 + depth-- return projectFlutterLink(filepath.Dir(dir), depth) }