-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
372 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
Copyright © 2020 befovy <[email protected]> | ||
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" | ||
"github.com/befovy/fvm/fvmgo" | ||
"github.com/spf13/cobra" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
var cop bool | ||
var pos string | ||
|
||
func init() { | ||
importCommand.Flags().BoolVarP(&cop, "copy", "c", false, "copy files instead of move") | ||
importCommand.Flags().StringVar(&pos, "path", "", "special the installed flutter path to import") | ||
rootCmd.AddCommand(importCommand) | ||
} | ||
|
||
var importCommand = &cobra.Command{ | ||
Use: "import <name>", | ||
Short: "Import installed flutter into fvm", | ||
Long: "Import installed flutter into fvm.\n" + | ||
"If there are more than one flutter detected,\n" + | ||
"this sub command use the first one in path by default.\n" + | ||
"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.") | ||
} | ||
if len(args) > 1 { | ||
return errors.New("allows only one argument, the name to be imported as") | ||
} | ||
return nil | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
dst := args[0] | ||
choices := fvmgo.FlutterListInstalledSdks() | ||
if len(choices) > 0 { | ||
for _, choice := range choices { | ||
if dst == choice { | ||
ins := fvmgo.YellowV("%s", dst) | ||
fvmgo.Errorf("fvm has already installed flutter with channel or version: %v", ins) | ||
return | ||
} | ||
} | ||
} | ||
|
||
flutters := fvmgo.FlutterOutOfFvm(pos) | ||
var source string | ||
if flutters == nil || len(flutters) == 0 { | ||
fvmgo.Warnf("fvm did not detect any flutter outside of fvm") | ||
} else if len(flutters) > 1 { | ||
if len(pos) == 0 { | ||
fvmgo.Infof("There are %d flutters outside of fvm, you don't set flag --path, so fvm will import te first one", len(flutters)) | ||
source = flutters[0] | ||
} else { | ||
var match string | ||
for _, f := range flutters { | ||
if strings.HasPrefix(f, pos) { | ||
fvmgo.Infof("match %s", f) | ||
if len(match) == 0 { | ||
match = f | ||
source = match | ||
} else if match != f { | ||
fvmgo.Warnf("More than one installed flutter match the path: %v. You should provide a more detailed path", pos) | ||
source = "" | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
source = flutters[0] | ||
} | ||
|
||
if len(source) > 0 { | ||
dst = filepath.Join(fvmgo.VersionsDir(), dst) | ||
source = filepath.Dir(filepath.Dir(source)) | ||
fvmgo.Infof("%s will be imported into fvm", fvmgo.YellowV("%s", source)) | ||
if cop { | ||
fvmgo.Infof("Copy all files from %v to %v, please wait seconds", source, dst) | ||
} else { | ||
fvmgo.Infof("Move all files from %v to %v, please wait seconds", source, dst) | ||
} | ||
err := fvmgo.CopyDir(source, dst) | ||
if err != nil { | ||
fvmgo.Errorf("Failed to copy files, %v", err) | ||
} | ||
if !cop { | ||
err = os.RemoveAll(source) | ||
if err != nil { | ||
fvmgo.Errorf("Failed to delete files, %v", err) | ||
} | ||
} | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
Copyright © 2019 befovy <[email protected]> | ||
Copyright © 2019-2020 befovy <[email protected]> | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
|
@@ -16,8 +16,11 @@ limitations under the License. | |
package fvmgo | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// IsFileExists checks if a file exists and is not a directory | ||
|
@@ -57,7 +60,117 @@ func IsSymlink(name string) bool { | |
return (info.Mode() & os.ModeSymlink) != 0 | ||
} | ||
|
||
func IsNotFound(path string) bool { | ||
_, err := os.Lstat(path) | ||
return os.IsNotExist(err) | ||
func IsNotFound(name string) bool { | ||
_, err := os.Lstat(name) | ||
return err != nil | ||
} | ||
|
||
func IsExecutable(name string) bool { | ||
info, err := os.Stat(name) | ||
if err != nil { | ||
return false | ||
} | ||
return !info.IsDir() && ((info.Mode() & 0111) != 0) | ||
} | ||
|
||
// CopyFile copies the contents of the file named src to the file named | ||
// by dst. The file will be created if it does not already exist. If the | ||
// destination file exists, all it's contents will be replaced by the contents | ||
// of the source file. The file mode will be copied from the source and | ||
// the copied data is synced/flushed to stable storage. | ||
func CopyFile(src, dst string) (err error) { | ||
in, err := os.Open(src) | ||
if err != nil { | ||
return | ||
} | ||
defer in.Close() | ||
|
||
out, err := os.Create(dst) | ||
if err != nil { | ||
return | ||
} | ||
defer func() { | ||
if e := out.Close(); e != nil { | ||
err = e | ||
} | ||
}() | ||
|
||
_, err = io.Copy(out, in) | ||
if err != nil { | ||
return | ||
} | ||
|
||
err = out.Sync() | ||
if err != nil { | ||
return | ||
} | ||
|
||
si, err := os.Stat(src) | ||
if err != nil { | ||
return | ||
} | ||
err = os.Chmod(dst, si.Mode()) | ||
if err != nil { | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
// CopyDir recursively copies a directory tree, attempting to preserve permissions. | ||
// Source directory must exist, destination directory must *not* exist. | ||
// Symlinks are ignored and skipped. | ||
func CopyDir(src string, dst string) (err error) { | ||
src = filepath.Clean(src) | ||
dst = filepath.Clean(dst) | ||
|
||
si, err := os.Stat(src) | ||
if err != nil { | ||
return err | ||
} | ||
if !si.IsDir() { | ||
return fmt.Errorf("source is not a directory") | ||
} | ||
|
||
_, err = os.Stat(dst) | ||
if err != nil && !os.IsNotExist(err) { | ||
return | ||
} | ||
if err == nil { | ||
return fmt.Errorf("destination already exists") | ||
} | ||
|
||
err = os.MkdirAll(dst, si.Mode()) | ||
if err != nil { | ||
return | ||
} | ||
|
||
entries, err := ioutil.ReadDir(src) | ||
if err != nil { | ||
return | ||
} | ||
|
||
for _, entry := range entries { | ||
srcPath := filepath.Join(src, entry.Name()) | ||
dstPath := filepath.Join(dst, entry.Name()) | ||
|
||
if entry.IsDir() { | ||
err = CopyDir(srcPath, dstPath) | ||
if err != nil { | ||
return | ||
} | ||
} else { | ||
// Skip symlinks. | ||
if entry.Mode()&os.ModeSymlink != 0 { | ||
continue | ||
} | ||
|
||
err = CopyFile(srcPath, dstPath) | ||
if err != nil { | ||
return | ||
} | ||
} | ||
} | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
Copyright © 2020 befovy <[email protected]> | ||
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 fvmgo | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
) | ||
|
||
func TestFileExist(t *testing.T) { | ||
if runtime.GOOS == "darwin" { | ||
if !IsFileExists("/bin/bash") { | ||
t.Fail() | ||
} | ||
if IsFileExists("/bin/helloworld") { | ||
t.Fail() | ||
} | ||
} | ||
} | ||
|
||
func TestIsDirectory(t *testing.T) { | ||
if runtime.GOOS == "darwin" { | ||
if !IsDirectory("/bin") { | ||
t.Fail() | ||
} | ||
|
||
if IsDirectory("bin/bash") { | ||
t.Fail() | ||
} | ||
} | ||
} | ||
|
||
func TestIsNotFound(t *testing.T) { | ||
if !IsNotFound("/usr/local/fvm/versions/.DS_Store/.github") { | ||
t.Fail() | ||
} | ||
} |
Oops, something went wrong.