Skip to content

Commit

Permalink
add feat/init
Browse files Browse the repository at this point in the history
  • Loading branch information
xzyaoi committed Dec 19, 2018
1 parent 9d04346 commit bee0c18
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 32 deletions.
2 changes: 1 addition & 1 deletion cli/daemon.go
Expand Up @@ -4,8 +4,8 @@ You can uninstall that service by using cvpm daemon uninstall */
package main

import (
"github.com/gin-contrib/static"
"github.com/fatih/color"
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
"github.com/googollee/go-socket.io"
"github.com/hpcloud/tail"
Expand Down
61 changes: 43 additions & 18 deletions cli/handler.go
Expand Up @@ -7,11 +7,12 @@ and etc. */
package main

import (
"errors"
"bufio"
"fmt"
"github.com/manifoldco/promptui"
"github.com/fatih/color"
"github.com/getsentry/raven-go"
"github.com/manifoldco/promptui"
"github.com/mitchellh/go-homedir"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
Expand Down Expand Up @@ -54,7 +55,7 @@ func InstallHandler(c *cli.Context) {
pip([]string{"install", "cvpm", "--user"})
return
} else if remoteURL == "webui" {
InstallWebUi()
InstallWebUi()
} else {
color.Cyan("Installing to " + localFolder)
}
Expand Down Expand Up @@ -112,12 +113,29 @@ func RepoHandler(c *cli.Context) {
case "ps":
requestParams := map[string]string{}
ClientGet("repos", requestParams)
case "init":
InitHandler(c)
default:
color.Red("Command Not Supported!")
}
}

// 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 == "" {
return nil
} else {
if _, err := os.Stat(input); os.IsNotExist(err) {
return errors.New(input + " not exists")
} else {
return errors.New("Unknown Error")
}
}
}

func ConfigHandler(c *cli.Context) {
homepath, _ := homedir.Dir()
configFilePath := filepath.Join(homepath, "cvpm", "config.toml")
Expand All @@ -136,36 +154,43 @@ func ConfigHandler(c *cli.Context) {
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)
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)
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)
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)
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)
}

func InitHandler(c *cli.Context) {
prompt := promptui.Prompt{
Label: "String",
Label: "Your Package Name",
}
result, err := prompt.Run()
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions cli/repository.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/fatih/color"
"github.com/getsentry/raven-go"
"gopkg.in/src-d/go-git.v4"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand Down Expand Up @@ -99,7 +100,7 @@ func runRepo(Vendor string, Name string, Solver string, Port string) {
}
}

func CloneFromGit(remoteURL string, targetFolder string) Repository {
func CloneFromGit(remoteURL string, targetFolder string) {
color.Cyan("Cloning " + remoteURL + " into " + targetFolder)
_, err := git.PlainClone(targetFolder, false, &git.CloneOptions{
URL: remoteURL,
Expand All @@ -109,8 +110,6 @@ func CloneFromGit(remoteURL string, targetFolder string) Repository {
raven.CaptureErrorAndWait(err, nil)
fmt.Println(err)
}
repo := Repository{Name: repoName, Vendor: vendorName, LocalFolder: targetFolder}
return repo
}

func InstallDependencies(localFolder string) {
Expand Down Expand Up @@ -173,8 +172,9 @@ func InstallFromGit(remoteURL string) {
vendorName := localFolderName[len(localFolderName)-2]
repoName := localFolderName[len(localFolderName)-1]
localFolder := filepath.Join(config.Local.LocalFolder, vendorName, repoName)
CloneFromGit(remoteURL, localFolder)
repo = Repository{Name: repoName, Vendor: vendorName, LocalFolder: localFolder}

repo = CloneFromGit(remoteURL, localFolder)
repoFolder := repo.LocalFolder
InstallDependencies(repoFolder)
GeneratingRunners(repoFolder)
Expand All @@ -184,7 +184,7 @@ func InstallFromGit(remoteURL string) {
}

// Init a new repoo by using bolierplate
func InitNewRepo (repoName string) {
bolierplateURL := "https://github.com/cvmodel/bolierplate.git"
CloneFromGit(bolierplateURL, repoName)
func InitNewRepo(repoName string) {
bolierplateURL := "https://github.com/cvmodel/bolierplate.git"
CloneFromGit(bolierplateURL, repoName)
}
10 changes: 5 additions & 5 deletions cli/utils.go
@@ -1,10 +1,10 @@
package main

import (
"io/ioutil"
"net"
"os"
"os/user"
"io/ioutil"
"path/filepath"
"strconv"
"time"
Expand Down Expand Up @@ -78,13 +78,13 @@ func findNextOpenPort(port int) string {
return strPort
}

func readFileContent(filepath) string {
func readFileContent(filename string) string {
var content string
byte_content, err := ioutil.ReadFile(filepath)
byte_content, err := ioutil.ReadFile(filename)
if err != nil {
content = "Read " + filepath + "Failed!"
content = "Read " + filename + "Failed!"
} else {
content = string(byte_content)
}
return content
}
}
2 changes: 1 addition & 1 deletion cli/webui.go
@@ -1,6 +1,6 @@
package main

// Install Web UI -> Download Latest and Unzip
func InstallWebUi () {
func InstallWebUi() {

}

0 comments on commit bee0c18

Please sign in to comment.