From 2931dd5c83cd82c7eb1453f66dbdbc884b55f51b Mon Sep 17 00:00:00 2001 From: Xiaozhe Yao Date: Tue, 25 Sep 2018 21:36:30 +0800 Subject: [PATCH] Fix #3 --- cli/config.go | 25 +++++++++++++++++++++++++ cli/handler.go | 12 ++++++++++++ cli/main.go | 10 +--------- cvpm/solver.py | 2 +- cvpm/utility.py | 3 +++ 5 files changed, 42 insertions(+), 10 deletions(-) diff --git a/cli/config.go b/cli/config.go index 23e23d381..8efa5b204 100644 --- a/cli/config.go +++ b/cli/config.go @@ -1,6 +1,7 @@ package main import ( + "os" "bytes" "github.com/BurntSushi/toml" "github.com/mitchellh/go-homedir" @@ -22,6 +23,17 @@ type local struct { var apiURL = "http://192.168.1.12:8080/" +func isPathExists(path string) (bool, error) { + _, err := os.Stat(path) + if err != nil { + return true, nil + } + if os.IsNotExist(err) { + return false, nil + } + return false, err +} + func readConfig() cvpmConfig { var config cvpmConfig homepath, _ := homedir.Dir() @@ -61,3 +73,16 @@ func getDefaultConfig() cvpmConfig { var defaultCVPMConfig = cvpmConfig{Local: defaultLocal, Repositories: []Repository{}} return defaultCVPMConfig } + +func validateConfig() { + localConfig := readConfig() + // Validate CVPM Path + cvpmPath := filepath.Join(homepath, "cvpm") + exist, err := isPathExists(cvpmPath) + if !exist { + err:= os.Mkdir(cvpmPath, os.ModePerm) + if err != nil { + log.Fatal(err) + } + } +} \ No newline at end of file diff --git a/cli/handler.go b/cli/handler.go index 5299df27d..550877c93 100644 --- a/cli/handler.go +++ b/cli/handler.go @@ -14,6 +14,18 @@ import ( "strings" ) +func LoginHandler(c *cli.Context) { + reader := bufio.NewReader(os.Stdin) + fmt.Printf("Username: ") + username, _ := reader.ReadString('\n') + username = strings.TrimSpace(username) + fmt.Printf("Password: ") + bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin)) + password := strings.TrimSpace(string(bytePassword)) + u := User{username, password, ""} + currentUser = u.login() +} + func InstallHandler(c *cli.Context) { config := readConfig() localFolder := config.Local.LocalFolder diff --git a/cli/main.go b/cli/main.go index 7e440c049..84608c78e 100644 --- a/cli/main.go +++ b/cli/main.go @@ -25,15 +25,7 @@ func main() { { Name: "login", Action: func(c *cli.Context) error { - reader := bufio.NewReader(os.Stdin) - fmt.Printf("Username: ") - username, _ := reader.ReadString('\n') - username = strings.TrimSpace(username) - fmt.Printf("Password: ") - bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin)) - password := strings.TrimSpace(string(bytePassword)) - u := User{username, password, ""} - currentUser = u.login() + LoginHandler(c) return nil }, }, diff --git a/cvpm/solver.py b/cvpm/solver.py index c3716b703..4b4300141 100644 --- a/cvpm/solver.py +++ b/cvpm/solver.py @@ -32,7 +32,7 @@ def help_message(self): members = self.bundle.members() return json.dumps(members) else: - return json.dumps({"error": "Initializing...", "code": "101"}), 101 + return json.dumps({"error": "Failed to start", "code": "101"}), 101 def _prepare_models(self, toml_file): parsed_toml = toml.load(toml_file) diff --git a/cvpm/utility.py b/cvpm/utility.py index a5ecf63e6..53640a2a5 100644 --- a/cvpm/utility.py +++ b/cvpm/utility.py @@ -19,6 +19,9 @@ def __init__(self): pass def download(self, url, target): + # check if target folder exists + if not os.path.isdir(target): + os.makedirs(target) filename = url.split('/')[-1] file_size = int(urlopen(url).info().get('Content-Length', -1)) chunk_size = 1024