Skip to content

Commit

Permalink
fix handler for configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
xzyaoi committed Sep 19, 2018
1 parent 0a39913 commit e28a6bd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
8 changes: 8 additions & 0 deletions cli/config.go
Original file line number Diff line number Diff line change
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
}
40 changes: 37 additions & 3 deletions cli/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

0 comments on commit e28a6bd

Please sign in to comment.