From e28a6bdf817c70c408d8d7c736d3aaf0a5a366c7 Mon Sep 17 00:00:00 2001 From: xzyaoi Date: Wed, 19 Sep 2018 21:48:30 +0800 Subject: [PATCH 1/3] fix handler for configuration file --- cli/config.go | 8 ++++++++ cli/handler.go | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/cli/config.go b/cli/config.go index e6c3f8119..bfa374393 100644 --- a/cli/config.go +++ b/cli/config.go @@ -53,3 +53,11 @@ func writeConfig(config cvpmConfig) { log.Fatal(err) } } + +func getDefaultConfig() cvpmConfig { + localPath, _ := homedir.Dir() + cvpmPath := filepath.Join(localPath, "cvpm") + var defaultLocal = local{LocalFolder:cvpmPath, Pip:"pip", Python:"python"} + var defaultCVPMConfig = cvpmConfig{Local: defaultLocal, Repositories: []Repository{}} + return defaultCVPMConfig +} diff --git a/cli/handler.go b/cli/handler.go index 5b271e53f..ff2350a5a 100644 --- a/cli/handler.go +++ b/cli/handler.go @@ -5,10 +5,13 @@ import ( "github.com/olekukonko/tablewriter" "github.com/urfave/cli" "github.com/mitchellh/go-homedir" + "fmt" "os" "strconv" "strings" + "bufio" "path/filepath" + "log" ) func InstallHandler(c *cli.Context) { @@ -85,14 +88,45 @@ func RepoHandler(c *cli.Context) { } func ConfigHandler(c* cli.Context) { - configFilePath := filepath.Join(homedir.Dir(), "cvpm", "config.toml") + homepath, _ := homedir.Dir() + configFilePath := filepath.Join(homepath, "cvpm", "config.toml") if _, err := os.Stat(configFilePath); os.IsNotExist(err) { + // config file not exists, create it file, err := os.Create(configFilePath) if err != nil { color.Red("An error occured!") } defer file.Close() + // config file not exists, write default to it + writeConfig(getDefaultConfig()) } - + prevConfig := readConfig() + var nextConfig cvpmConfig + nextConfig.Local.LocalFolder = prevConfig.Local.LocalFolder + // Handle Python Location + reader := bufio.NewReader(os.Stdin) + fmt.Printf("Python Location[" + prevConfig.Local.Python + "]") + newPyLocation, _ := reader.ReadString('\n') + newPyLocation = strings.TrimSpace(newPyLocation) + if newPyLocation == "y" || newPyLocation == "Y" || newPyLocation == "Yes" || newPyLocation == "" { + newPyLocation = prevConfig.Local.Python + } else { + if _, err := os.Stat(newPyLocation); os.IsNotExist(err) { + log.Fatal("Python executable file not found: No such file") + } + } + nextConfig.Local.Python = newPyLocation + // Handle Pypi Location + fmt.Printf("Pip Location[" + prevConfig.Local.Pip + "]") + newPipLocation, _:= reader.ReadString('\n') + newPipLocation = strings.TrimSpace(newPipLocation) + if newPipLocation == "y" || newPipLocation == "Y" || newPipLocation == "Yes" || newPipLocation == "" { + newPipLocation = prevConfig.Local.Pip + } else { + if _, err := os.Stat(newPipLocation); os.IsNotExist(err) { + log.Fatal("Pip executable file not found: No such file") + } + } + nextConfig.Local.Pip = newPipLocation + writeConfig(nextConfig) } - From c03821ff1e278f6a03c17d7fbfc16498559fae40 Mon Sep 17 00:00:00 2001 From: xzyaoi Date: Wed, 19 Sep 2018 21:55:38 +0800 Subject: [PATCH 2/3] format code --- cli/config.go | 2 +- cli/handler.go | 14 +++++++------- cli/query.go | 10 +++++----- cli/repository.go | 2 +- cli/service.go | 6 +++--- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/cli/config.go b/cli/config.go index bfa374393..23e23d381 100644 --- a/cli/config.go +++ b/cli/config.go @@ -57,7 +57,7 @@ func writeConfig(config cvpmConfig) { func getDefaultConfig() cvpmConfig { localPath, _ := homedir.Dir() cvpmPath := filepath.Join(localPath, "cvpm") - var defaultLocal = local{LocalFolder:cvpmPath, Pip:"pip", Python:"python"} + var defaultLocal = local{LocalFolder: cvpmPath, Pip: "pip", Python: "python"} var defaultCVPMConfig = cvpmConfig{Local: defaultLocal, Repositories: []Repository{}} return defaultCVPMConfig } diff --git a/cli/handler.go b/cli/handler.go index ff2350a5a..ccf5b8872 100644 --- a/cli/handler.go +++ b/cli/handler.go @@ -1,17 +1,17 @@ package main import ( + "bufio" + "fmt" "github.com/fatih/color" + "github.com/mitchellh/go-homedir" "github.com/olekukonko/tablewriter" "github.com/urfave/cli" - "github.com/mitchellh/go-homedir" - "fmt" + "log" "os" + "path/filepath" "strconv" "strings" - "bufio" - "path/filepath" - "log" ) func InstallHandler(c *cli.Context) { @@ -87,7 +87,7 @@ func RepoHandler(c *cli.Context) { } } -func ConfigHandler(c* cli.Context) { +func ConfigHandler(c *cli.Context) { homepath, _ := homedir.Dir() configFilePath := filepath.Join(homepath, "cvpm", "config.toml") if _, err := os.Stat(configFilePath); os.IsNotExist(err) { @@ -118,7 +118,7 @@ func ConfigHandler(c* cli.Context) { nextConfig.Local.Python = newPyLocation // Handle Pypi Location fmt.Printf("Pip Location[" + prevConfig.Local.Pip + "]") - newPipLocation, _:= reader.ReadString('\n') + newPipLocation, _ := reader.ReadString('\n') newPipLocation = strings.TrimSpace(newPipLocation) if newPipLocation == "y" || newPipLocation == "Y" || newPipLocation == "Yes" || newPipLocation == "" { newPipLocation = prevConfig.Local.Pip diff --git a/cli/query.go b/cli/query.go index 8cee55232..cc72a5155 100644 --- a/cli/query.go +++ b/cli/query.go @@ -1,10 +1,10 @@ package main import ( - "github.com/mitchellh/go-homedir" "github.com/levigross/grequests" - "os/user" + "github.com/mitchellh/go-homedir" "log" + "os/user" ) func ClientPost(endpoint string, params map[string]string) { @@ -15,8 +15,8 @@ func ClientPost(endpoint string, params map[string]string) { } resp, err := grequests.Post(url, &grequests.RequestOptions{ Headers: map[string]string{"X-Current-User": currentUser.Username}, - JSON: params, - IsAjax: true, + JSON: params, + IsAjax: true, }) if err != nil { log.Fatal(err) @@ -34,7 +34,7 @@ func ClientGet(endpoint string, params map[string]string) { } resp, err := grequests.Get(url, &grequests.RequestOptions{ Headers: map[string]string{"X-Current-Homedir": myhomedir}, - Params: params, + Params: params, }) if err != nil { log.Fatal(err) diff --git a/cli/repository.go b/cli/repository.go index 6b400ad57..68f997445 100644 --- a/cli/repository.go +++ b/cli/repository.go @@ -33,7 +33,7 @@ func readRepos() []Repository { return repos } -func readClientRepos (currentHomedir string) []Repository { +func readClientRepos(currentHomedir string) []Repository { configs := readClientConfig(currentHomedir) repos := configs.Repositories return repos diff --git a/cli/service.go b/cli/service.go index e9c2bce0e..7ff300e39 100644 --- a/cli/service.go +++ b/cli/service.go @@ -3,8 +3,8 @@ package main import ( "github.com/kardianos/service" "log" - "os/user" "os" + "os/user" "runtime" ) @@ -24,7 +24,7 @@ func getCVPMDConfig() *service.Config { currentUser, _ := user.Current() var realUsername string if currentUser.Username == "root" && runtime.GOOS != "windows" { - realUsername = os.Getenv("SUDO_USER") + realUsername = os.Getenv("SUDO_USER") } else { realUsername = currentUser.Username } @@ -33,7 +33,7 @@ func getCVPMDConfig() *service.Config { DisplayName: "CVPM Daemon", Description: "Computer Vision Package Manager[Daemon]", Arguments: []string{"daemon", "run"}, - UserName: realUsername, + UserName: realUsername, } return srvConf } From 81ea64f2e60f6066c68473799fcdee1b6a27797c Mon Sep 17 00:00:00 2001 From: xzyaoi Date: Wed, 19 Sep 2018 21:56:57 +0800 Subject: [PATCH 3/3] fix a typo --- cli/handler.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/handler.go b/cli/handler.go index ccf5b8872..5299df27d 100644 --- a/cli/handler.go +++ b/cli/handler.go @@ -94,7 +94,7 @@ func ConfigHandler(c *cli.Context) { // config file not exists, create it file, err := os.Create(configFilePath) if err != nil { - color.Red("An error occured!") + color.Red("An error occurred!") } defer file.Close() // config file not exists, write default to it