Skip to content

Commit

Permalink
finish feat/init
Browse files Browse the repository at this point in the history
  • Loading branch information
xzyaoi committed Dec 19, 2018
1 parent bee0c18 commit 0c2b09f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
1 change: 0 additions & 1 deletion cli/config.go
Expand Up @@ -115,7 +115,6 @@ func createFileIfNotExist(filePath string) {
func validateConfig() {
if !isRoot() {
homepath := getHomeDir()
log.Println(homepath)
// Validate CVPM Path
cvpmPath := filepath.Join(homepath, "cvpm")
createFolderIfNotExist(cvpmPath)
Expand Down
53 changes: 29 additions & 24 deletions cli/handler.go
Expand Up @@ -7,8 +7,8 @@ and etc. */
package main

import (
"errors"
"bufio"
"errors"
"fmt"
"github.com/fatih/color"
"github.com/getsentry/raven-go"
Expand Down Expand Up @@ -123,6 +123,7 @@ func RepoHandler(c *cli.Context) {
// Handle Config Related Command

// validate if python/pip/others exists or does not change

func validateIfProgramAllowed(rawInput string) error {
input := strings.TrimSpace(rawInput)
if input == "y" || input == "Y" || input == "Yes" || input == "" {
Expand All @@ -131,11 +132,25 @@ func validateIfProgramAllowed(rawInput string) error {
if _, err := os.Stat(input); os.IsNotExist(err) {
return errors.New(input + " not exists")
} else {
return errors.New("Unknown Error")
return nil
}
}
}

// trigger and parse input filepath
func InputAndParseConfigContent(label string, validate promptui.ValidateFunc) string {
prompt := promptui.Prompt{
Label: label,
Validate: validate,
}
result, err := prompt.Run()
if err != nil {
fmt.Printf("%v\n", err)
return ""
}
return result
}

func ConfigHandler(c *cli.Context) {
homepath, _ := homedir.Dir()
configFilePath := filepath.Join(homepath, "cvpm", "config.toml")
Expand All @@ -154,33 +169,17 @@ func ConfigHandler(c *cli.Context) {
var nextConfig cvpmConfig
nextConfig.Local.LocalFolder = prevConfig.Local.LocalFolder
// Handle Python Location
promptPy := promptui.Prompt{
Label: "Python(3) Path",
Validate: validateIfProgramAllowed,
}
fmt.Printf("Original Python Location [" + prevConfig.Local.Python + "]")
result, err := promptPy.Run()
if err != nil {
fmt.Printf("%v\n", err)
return
}
newPyLocation := strings.TrimSpace(result)
fmt.Println("Original Python Location: " + prevConfig.Local.Python)
newPyLocation := InputAndParseConfigContent("Python(3)", validateIfProgramAllowed)
newPyLocation = strings.TrimSpace(newPyLocation)
if newPyLocation == "y" || newPyLocation == "Y" || newPyLocation == "Yes" || newPyLocation == "" {
newPyLocation = prevConfig.Local.Python
}
nextConfig.Local.Python = newPyLocation
// Handle Pypi Location
fmt.Printf("Original Pip Location [" + prevConfig.Local.Pip + "]")
promptPip := promptui.Prompt{
Label: "Pip(3) Path",
Validate: validateIfProgramAllowed,
}
result, err = promptPip.Run()
if err != nil {
fmt.Printf("%v\n", err)
return
}
newPipLocation := strings.TrimSpace(result)
fmt.Println("Original Pip Location: " + prevConfig.Local.Pip)
newPipLocation := InputAndParseConfigContent("Pip(3)", validateIfProgramAllowed)
newPipLocation = strings.TrimSpace(newPipLocation)
if newPipLocation == "y" || newPipLocation == "Y" || newPipLocation == "Yes" || newPipLocation == "" {
newPipLocation = prevConfig.Local.Pip
}
Expand All @@ -197,4 +196,10 @@ func InitHandler(c *cli.Context) {
panic(err)
}
InitNewRepo(result)
// rename {package_name} to real package name
pyFolderName := filepath.Join(result, "{package_name}")
err = os.Rename(pyFolderName, filepath.Join(result, result))
if err != nil {
fmt.Println(err)
}
}

0 comments on commit 0c2b09f

Please sign in to comment.