/
handler.go
171 lines (162 loc) · 4.71 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
This file defines the handlers for different command.
Login
Install
List
Repo
and etc.
*/
package main
import (
"bufio"
"fmt"
"github.com/fatih/color"
"github.com/getsentry/raven-go"
"github.com/mitchellh/go-homedir"
"github.com/olekukonko/tablewriter"
"github.com/urfave/cli"
"golang.org/x/crypto/ssh/terminal"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
)
// Handle User Login
func LoginHandler(c *cli.Context) User {
reader := bufio.NewReader(os.Stdin)
fmt.Printf("Username: ")
username, _ := reader.ReadString('\n')
username = strings.TrimSpace(username)
fmt.Printf("Password: ")
bytePassword, _ := terminal.ReadPassword(int(syscall.Stdin))
password := strings.TrimSpace(string(bytePassword))
u := User{username, password, ""}
currentUser := u.login()
return currentUser
}
// Handle installation
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)
}
// Handle List
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()
}
// Handle Daemon Related
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)
runningPort := c.Args().Get(2)
if (runningPort == "") {
runningPort = findNextOpenPort(8080)
color.Red("No Running Port specified! Server will listen on: " + runningPort)
}
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],
"port": runningPort,
}
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 {
raven.CaptureErrorAndWait(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)
}
func InitHandler(c *cli.Context) {
}