-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
handler.go
132 lines (126 loc) · 3.77 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"bufio"
"fmt"
"github.com/fatih/color"
"github.com/mitchellh/go-homedir"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
func InstallHandler(c *cli.Context) {
config := readConfig()
localFolder := config.Local.LocalFolder
remoteURL := c.Args().Get(0)
if remoteURL == "cvpm:test" {
color.Cyan("Installing... Please wait patiently")
pip([]string{"install", "--index-url", "https://test.pypi.org/simple/", "cvpm", "--user"})
return
} else {
color.Cyan("Installing to " + localFolder)
}
var repoFolder string
var repo Repository
// Download Codebase
if strings.HasPrefix(remoteURL, "https://github.com") {
repo = CloneFromGit(remoteURL, localFolder)
}
repoFolder = repo.LocalFolder
// Install Dependencies
color.Cyan("Installing Dependencies... please wait patiently")
InstallDependencies(repoFolder)
color.Blue("Generating Runners")
GeneratingRunners(repoFolder)
color.Cyan("Adding to Local Configuration")
config.Repositories = addRepo(config.Repositories, repo)
writeConfig(config)
}
func listRepos(c *cli.Context) {
config := readConfig()
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"No.", "Name", "Vendor", "Path"})
for i, repo := range config.Repositories {
table.Append([]string{strconv.Itoa(i), repo.Name, repo.Vendor, repo.LocalFolder})
}
table.Render()
}
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")
}
}
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)
case "ps":
requestParams := map[string]string{}
ClientGet("repos", requestParams)
default:
color.Red("Command Not Supported!")
}
}
func ConfigHandler(c *cli.Context) {
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 occurred!")
}
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)
}