Skip to content

Commit

Permalink
code format
Browse files Browse the repository at this point in the history
  • Loading branch information
xzyaoi committed Sep 12, 2018
1 parent e54f225 commit 1397973
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 112 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ format:
docs:
cd docs && npm run docs:build

docs-dev:
cd docs && npm run docs:dev

package:
python setup.py sdist bdist_wheel

Expand Down
22 changes: 11 additions & 11 deletions cli/config.go
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
package main

import(
"log"
"io/ioutil"
"path/filepath"
import (
"bytes"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"bytes"
"io/ioutil"
"log"
"path/filepath"
)

type cvpmConfig struct {
Local local `toml:"local"`
Local local `toml:"local"`
Repositories []Repository `toml:repository`
}

type local struct {
LocalFolder string
Pip string
Python string
Pip string
Python string
}

var apiURL = "http://192.168.1.12:8080/"

func readConfig () cvpmConfig {
func readConfig() cvpmConfig {
var config cvpmConfig
homepath, _ := homedir.Dir()
configFile := filepath.Join(homepath, "cvpm", "config.toml")
if _, err := toml.DecodeFile(configFile, &config); err!=nil {
if _, err := toml.DecodeFile(configFile, &config); err != nil {
log.Fatal(err)
return config
}
return config
}

func writeConfig (config cvpmConfig) {
func writeConfig(config cvpmConfig) {
buf := new(bytes.Buffer)
if err := toml.NewEncoder(buf).Encode(config); err != nil {
log.Fatal(err)
Expand Down
22 changes: 12 additions & 10 deletions cli/daemon.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package main

import (
"github.com/gin-gonic/gin"
"github.com/fatih/color"
"github.com/gin-gonic/gin"
"net/http"
)

const DaemonPort = "10590"

var RunningRepos []Repository

type RunRepoRequest struct {
Name string `json:name`
Name string `json:name`
Vendor string `json:vendor`
Solver string `json:solver`
}

func PostRepoHandler (c *gin.Context) {
func PostRepoHandler(c *gin.Context) {
var runRepoRequest RunRepoRequest
c.BindJSON(&runRepoRequest)
go runRepo(runRepoRequest.Vendor, runRepoRequest.Name, runRepoRequest.Solver)
Expand All @@ -23,18 +25,18 @@ func PostRepoHandler (c *gin.Context) {
})
}

func GetReposHandler (c *gin.Context) {
func GetReposHandler(c *gin.Context) {

}

func runServer (port string) {
func runServer(port string) {
color.Red("Initiating")
r := gin.Default()
r.GET("/status", func (c *gin.Context) {
r := gin.Default()
r.GET("/status", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"daemon":"running",
"daemon": "running",
})
})
})
r.POST("/repo", PostRepoHandler)
r.Run("127.0.0.1:" + port)
r.Run("127.0.0.1:" + port)
}
60 changes: 30 additions & 30 deletions cli/handler.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package main

import (
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
"os"
"strconv"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
"github.com/fatih/color"
)

func InstallHandler (c *cli.Context) {
func InstallHandler(c *cli.Context) {
config := readConfig()
localFolder := config.Local.LocalFolder
remoteURL := c.Args().Get(0)
if (remoteURL == "cvpm:test") {
if remoteURL == "cvpm:test" {
color.Cyan("Installing... Please wait patiently")
pip([]string{"install", "--index-url", "https://test.pypi.org/simple/", "cvpm", "--user"})
return
Expand All @@ -34,10 +34,10 @@ func InstallHandler (c *cli.Context) {
GeneratingRunners(repoFolder)
color.Cyan("Adding to Local Configuration")
config.Repositories = addRepo(config.Repositories, repo)
writeConfig(config)
writeConfig(config)
}

func listRepos (c *cli.Context) {
func listRepos(c *cli.Context) {
config := readConfig()
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"No.", "Name", "Vendor", "Path"})
Expand All @@ -47,35 +47,35 @@ func listRepos (c *cli.Context) {
table.Render()
}

func DaemonHandler (c *cli.Context) {
func DaemonHandler(c *cli.Context) {
params := c.Args().Get(0)
switch params {
case "install":
InstallService()
case "uninstall":
UninstallService()
case "run":
runServer(DaemonPort)
default:
color.Red("Unsupported command")
case "install":
InstallService()
case "uninstall":
UninstallService()
case "run":
runServer(DaemonPort)
default:
color.Red("Unsupported command")
}
}

func RepoHandler (c *cli.Context) {
func RepoHandler(c *cli.Context) {
taskParams := c.Args().Get(0)
switch taskParams {
case "run":
solverstring := c.Args().Get(1)
runParams := strings.Split(solverstring, "/")
color.Cyan("Running " + runParams[0] + "/" + runParams[1] + "/" + runParams[2] )
requestParams := map[string]string{
"vendor": runParams[0],
"name": runParams[1],
"solver": runParams[2],
}
ClientPost("repo", requestParams)
// runRepo(runParams[0], runParams[1], runParams[2])
default:
color.Red("Command Not Supported!")
case "run":
solverstring := c.Args().Get(1)
runParams := strings.Split(solverstring, "/")
color.Cyan("Running " + runParams[0] + "/" + runParams[1] + "/" + runParams[2])
requestParams := map[string]string{
"vendor": runParams[0],
"name": runParams[1],
"solver": runParams[2],
}
ClientPost("repo", requestParams)
// runRepo(runParams[0], runParams[1], runParams[2])
default:
color.Red("Command Not Supported!")
}
}
2 changes: 1 addition & 1 deletion cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
// sessionToken := getCache("session-token")
var currentUser User
// if sessionToken != "" {
// currentUser = User{"", "", sessionToken}
// currentUser = User{"", "", sessionToken}
// currentUser.become()
// }
cvpm := cli.NewApp()
Expand Down
4 changes: 2 additions & 2 deletions cli/query.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package main

import (
"log"
"github.com/levigross/grequests"
"log"
)

func ClientPost(endpoint string, params map[string]string) {
url := "http://127.0.0.1:10590/" + endpoint
resp, err := grequests.Post(url, &grequests.RequestOptions{
JSON: params,
JSON: params,
IsAjax: true,
})
if err != nil {
Expand Down
50 changes: 25 additions & 25 deletions cli/repository.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,42 @@
package main

import (
"os"
"fmt"
"github.com/BurntSushi/toml"
"github.com/fatih/color"
"gopkg.in/src-d/go-git.v4"
"io/ioutil"
"log"
"os"
"path/filepath"
"fmt"
"strings"
"io/ioutil"
"gopkg.in/src-d/go-git.v4"
"github.com/fatih/color"
"github.com/BurntSushi/toml"
)

type Repository struct {
Name string
Name string
LocalFolder string
Vendor string
Vendor string
}

type solver struct {
Name string
Name string
Class string
}

type solvers struct {
Solvers []solver
}

func readRepos () []Repository {
func readRepos() []Repository {
configs := readConfig()
repos := configs.Repositories
return repos
}

func addRepo (repos []Repository, repo Repository) []Repository {
func addRepo(repos []Repository, repo Repository) []Repository {
alreadyInstalled := false
for _, existed_repo := range repos {
if (repo.Name == existed_repo.Name && repo.Vendor == existed_repo.Vendor) {
if repo.Name == existed_repo.Name && repo.Vendor == existed_repo.Vendor {
alreadyInstalled = true
}
}
Expand All @@ -47,23 +47,23 @@ func addRepo (repos []Repository, repo Repository) []Repository {
return repos
}

func delRepo (repos []Repository, Vendor string, Name string) []Repository {
func delRepo(repos []Repository, Vendor string, Name string) []Repository {
for i, repo := range repos {
if (repo.Name == Name && repo.Vendor == Vendor) {
if repo.Name == Name && repo.Vendor == Vendor {
repos = append(repos[:i], repos[i+1:]...)
}
}
return repos
}

func runRepo (Vendor string, Name string, Solver string) {
func runRepo(Vendor string, Name string, Solver string) {
repos := readRepos()
existed := false
for _, existed_repo := range repos {
if existed_repo.Name == Name && existed_repo.Vendor == Vendor {
files, _ := ioutil.ReadDir(existed_repo.LocalFolder)
for _, file := range files {
if (file.Name() == "runner_" + Solver + ".py") {
if file.Name() == "runner_"+Solver+".py" {
existed = true
runfileFullPath := filepath.Join(existed_repo.LocalFolder, file.Name())
python([]string{runfileFullPath})
Expand All @@ -76,31 +76,31 @@ func runRepo (Vendor string, Name string, Solver string) {
}
}

func CloneFromGit (remoteURL string, targetFolder string) Repository {
func CloneFromGit(remoteURL string, targetFolder string) Repository {
localFolderName := strings.Split(remoteURL, "/")
vendorName := localFolderName[len(localFolderName)-2]
repoName := localFolderName[len(localFolderName)-1]
localFolder := filepath.Join(targetFolder, vendorName,repoName)
color.Cyan("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,
URL: remoteURL,
Progress: os.Stdout,
})
if err != nil {
fmt.Println(err)
}
repo := Repository{Name:repoName, Vendor: vendorName, LocalFolder: localFolder}
repo := Repository{Name: repoName, Vendor: vendorName, LocalFolder: localFolder}
return repo
}

func InstallDependencies (localFolder string) {
func InstallDependencies(localFolder string) {
pip([]string{"install", "-r", filepath.Join(localFolder, "requirements.txt"), "--user"})
}

func GeneratingRunners (localFolder string) {
var mySolvers solvers
func GeneratingRunners(localFolder string) {
var mySolvers solvers
cvpmFile := filepath.Join(localFolder, "cvpm.toml")
if _, err := toml.DecodeFile(cvpmFile, &mySolvers); err!=nil {
if _, err := toml.DecodeFile(cvpmFile, &mySolvers); err != nil {
log.Fatal(err)
}
renderRunnerTpl(localFolder, mySolvers)
Expand Down
Loading

0 comments on commit 1397973

Please sign in to comment.