Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/unarxiv/CVPM
Browse files Browse the repository at this point in the history
  • Loading branch information
xzyaoi committed Sep 19, 2018
2 parents 166af94 + 81ea64f commit d702755
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 16 deletions.
8 changes: 8 additions & 0 deletions cli/config.go
Expand Up @@ -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
}
48 changes: 41 additions & 7 deletions cli/handler.go
@@ -1,14 +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"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"path/filepath"
)

func InstallHandler(c *cli.Context) {
Expand Down Expand Up @@ -84,15 +87,46 @@ func RepoHandler(c *cli.Context) {
}
}

func ConfigHandler(c* cli.Context) {
configFilePath := filepath.Join(homedir.Dir(), "cvpm", "config.toml")
func ConfigHandler(c *cli.Context) {
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!")
color.Red("An error occurred!")
}
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)
}

10 changes: 5 additions & 5 deletions 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) {
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cli/repository.go
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions cli/service.go
Expand Up @@ -3,8 +3,8 @@ package main
import (
"github.com/kardianos/service"
"log"
"os/user"
"os"
"os/user"
"runtime"
)

Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit d702755

Please sign in to comment.