Skip to content

Commit

Permalink
add lots of features in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
xzyaoi committed Sep 11, 2018
1 parent 59036e4 commit 5da39e7
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 15 deletions.
1 change: 1 addition & 0 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type cvpmConfig struct {
type local struct {
LocalFolder string
Pip string
Python string
}

var apiURL = "http://192.168.1.12:8080/"
Expand Down
10 changes: 6 additions & 4 deletions cli/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,29 @@ package main

import (
"strings"
"log"
"github.com/urfave/cli"
"github.com/fatih/color"
)

func InstallHandler (c *cli.Context) {
config := readConfig()
localFolder := config.Local.LocalFolder
remoteURL := c.Args().Get(0)
if (remoteURL == "cvpm:test") {
log.Printf("Installing... Please wait patiently")
color.Cyan("Installing... Please wait patiently")
pip([]string{"install", "--index-url", "https://test.pypi.org/simple/", "cvpm", "--user"})
return
} else {
log.Printf("Installing to " + localFolder)
color.Cyan("Installing to " + localFolder)
}
var repoFolder string
// Download Codebase
if strings.HasPrefix(remoteURL, "https://github.com") {
repoFolder = CloneFromGit(remoteURL, localFolder)
}
// Install Dependencies
log.Printf("Installing Dependencies... Please wait patiently")
color.Cyan("Installing Dependencies... please wait patiently")
InstallDependencies(repoFolder)
color.Blue("Generating Runners")
GeneratingRunners(repoFolder)
}
43 changes: 39 additions & 4 deletions cli/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,51 @@ import (
"os"
"log"
"path/filepath"
"fmt"
"strings"
"gopkg.in/src-d/go-git.v4"
"github.com/fatih/color"
"github.com/BurntSushi/toml"
)

type Repository struct {
Name string
LocalFolder string
}

type solver struct {
Name string
Class string
}

type solvers struct {
Solvers []solver
}

func readRepos () {

}

func addRepo () {

}

func delRepo () {

}

func CloneFromGit (remoteURL string, targetFolder string) string {
localFolderName := strings.Split(remoteURL, "/")
vendorName := localFolderName[len(localFolderName)-2]
repoName := localFolderName[len(localFolderName)-1]
localFolder := filepath.Join(targetFolder, repoName)
log.Printf("Cloning " + remoteURL + " into " + localFolder)
localFolder := filepath.Join(targetFolder, vendorName,repoName)
color.Cyan("Cloning " + remoteURL + " into " +localFolder)
_, err := git.PlainClone(localFolder, false, &git.CloneOptions{
URL: remoteURL,
Progress: os.Stdout,
})
if err != nil {
log.Fatal(err)
fmt.Println(err)
}
return localFolder
}
Expand All @@ -32,6 +58,15 @@ func InstallCVPMPyPI () {
}

func InstallDependencies (localFolder string) {
log.Printf("Installing Dependencies from " + localFolder)
pip([]string{"install", "-r", filepath.Join(localFolder, "requirements.txt"), "--user"})
}

func GeneratingRunners (localFolder string) {
var mySolvers solvers
cvpmFile := filepath.Join(localFolder, "cvpm.toml")
if _, err := toml.DecodeFile(cvpmFile, &mySolvers); err!=nil {
log.Fatal(err)
}
fmt.Println(len(mySolvers.Solvers))
renderRunnerTpl(localFolder, mySolvers)
}
4 changes: 4 additions & 0 deletions cli/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ func pip(args []string) {
_ = _execCommand(localPip, args)
}

func python(args []string) {

}

func _execCommand(commandName string, params []string) bool {
cmd := exec.Command(commandName, params...)
output, err := cmd.CombinedOutput()
Expand Down
44 changes: 44 additions & 0 deletions cli/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"log"
"io/ioutil"
"path/filepath"
"net/http"
"strings"
"github.com/flosch/pongo2"
)

func _getRunnerTpl () string {
var runnerTpl = "https://tpl.cvtron.xyz/runners/runner.tpl"
resp, err := http.Get(runnerTpl)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
return string(body)
}

func renderRunnerTpl (localFolder string, mysolvers solvers) {
tpl, err := pongo2.FromString(_getRunnerTpl())
if err != nil {
log.Fatal(err)
}
for _, solver := range mysolvers.Solvers {
writeRunner(tpl, localFolder, solver)
}
}

func writeRunner (tpl *pongo2.Template, localFolder string, Solver solver) {
filename := "runner_" + Solver.Name + ".py"
fileFullPath := filepath.Join(localFolder, filename)
tplContext := strings.Split(Solver.Class, "/")
out, err := tpl.Execute(pongo2.Context{"Package":tplContext[0],"Filename":tplContext[1], "Classname": tplContext[2]})
if err != nil {
log.Fatal(err)
}
err = ioutil.WriteFile(fileFullPath, []byte(out), 0644)
if err != nil {
log.Fatal(err)
}
}
7 changes: 0 additions & 7 deletions scripts/runner.tpl

This file was deleted.

0 comments on commit 5da39e7

Please sign in to comment.