-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
sfermi
committed
Sep 10, 2018
1 parent
f7d44e6
commit 1efa186
Showing
7 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package main | ||
|
||
import( | ||
"log" | ||
"path/filepath" | ||
"github.com/BurntSushi/toml" | ||
"github.com/mitchellh/go-homedir" | ||
) | ||
|
||
type cvpmConfig struct { | ||
Local local `toml:"local"` | ||
} | ||
|
||
type local struct { | ||
LocalFolder string | ||
Pip string | ||
} | ||
|
||
var apiURL = "http://192.168.1.12:8080/" | ||
|
||
func readConfig () cvpmConfig { | ||
var config cvpmConfig | ||
homepath, _ := homedir.Dir() | ||
configFile := filepath.Join(homepath, "cvpm", "config.toml") | ||
if _, err := toml.DecodeFile(configFile, &config); err!=nil { | ||
log.Fatal(err) | ||
return config | ||
} | ||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package main | ||
|
||
import ( | ||
"strings" | ||
"log" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
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") | ||
pip([]string{"install", "--index-url", "https://test.pypi.org/simple/", "cvpm", "--user"}) | ||
return | ||
} else { | ||
log.Printf("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") | ||
InstallDependencies(repoFolder) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"github.com/urfave/cli" | ||
"golang.org/x/crypto/ssh/terminal" | ||
"log" | ||
"os" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
func main() { | ||
sessionToken := getCache("session-token") | ||
var currentUser User | ||
if sessionToken != "" { | ||
currentUser = User{"", "", sessionToken} | ||
// currentUser.become() | ||
} | ||
cvpm := cli.NewApp() | ||
cvpm.Name = "CVPM" | ||
cvpm.Usage = "Computer Vision Package Manager" | ||
cvpm.Commands = []cli.Command{ | ||
{ | ||
Name: "login", | ||
Action: func(c *cli.Context) error { | ||
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 nil | ||
}, | ||
}, | ||
{ | ||
Name: "install", | ||
Action: func(c *cli.Context) error { | ||
InstallHandler(c) | ||
return nil | ||
}, | ||
}, | ||
} | ||
err := cvpm.Run(os.Args) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"log" | ||
"path/filepath" | ||
"strings" | ||
"gopkg.in/src-d/go-git.v4" | ||
) | ||
|
||
type Repository struct { | ||
Name string | ||
} | ||
|
||
func CloneFromGit (remoteURL string, targetFolder string) string { | ||
localFolderName := strings.Split(remoteURL, "/") | ||
repoName := localFolderName[len(localFolderName)-1] | ||
localFolder := filepath.Join(targetFolder, repoName) | ||
log.Printf("Cloning " + remoteURL + " into " + localFolder) | ||
_, err := git.PlainClone(localFolder, false, &git.CloneOptions{ | ||
URL: remoteURL, | ||
Progress: os.Stdout, | ||
}) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
return localFolder | ||
} | ||
|
||
func InstallCVPMPyPI () { | ||
|
||
} | ||
|
||
func InstallDependencies (localFolder string) { | ||
log.Printf("Installing Dependencies from " + localFolder) | ||
pip([]string{"install", "-r", filepath.Join(localFolder, "requirements.txt"), "--user"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/syndtr/goleveldb/leveldb" | ||
"log" | ||
) | ||
|
||
var store *leveldb.DB | ||
var cacheInited bool | ||
|
||
func initCache() { | ||
if cacheInited { | ||
} else { | ||
var err error | ||
store, err = leveldb.OpenFile(".data.db", nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
cacheInited = true | ||
} | ||
} | ||
|
||
func setCache(key string, value string) { | ||
initCache() | ||
if err := store.Put([]byte(key), []byte(value), nil); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func getCache(key string) string { | ||
initCache() | ||
result, _ := store.Get([]byte(key), nil) | ||
return string(result) | ||
} | ||
|
||
func deleteKey(key string) *error { | ||
initCache() | ||
err := store.Delete([]byte(key), nil) | ||
return &err | ||
} | ||
|
||
func closeCache() { | ||
err := store.Close() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"os/exec" | ||
) | ||
|
||
func pip(args []string) { | ||
config := readConfig() | ||
localPip := config.Local.Pip | ||
_ = _execCommand(localPip, args) | ||
} | ||
|
||
func _execCommand(commandName string, params []string) bool { | ||
cmd := exec.Command(commandName, params...) | ||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
log.Fatal(err) | ||
return false | ||
} | ||
log.Printf(string(output)) | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/fatih/color" | ||
"github.com/levigross/grequests" | ||
"log" | ||
) | ||
|
||
type User struct { | ||
Username string `json:"username"` | ||
Password string | ||
SessionToken string `json:"sessionToken"` | ||
} | ||
|
||
func (u *User) login() User { | ||
var respUser User | ||
loginURL := apiURL + "user/login" | ||
loginRequestHeader := &grequests.RequestOptions{ | ||
JSON: map[string]string{"username": u.Username, "password": u.Password}, | ||
IsAjax: true, | ||
} | ||
resp, err := grequests.Post(loginURL, loginRequestHeader) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if resp.Ok != true { | ||
fmt.Println("\nLogin Failed!") | ||
} else { | ||
_ = json.Unmarshal(resp.Bytes(), &respUser) | ||
setCache("session-token", respUser.SessionToken) | ||
color.Green("\nLogin Successfully") | ||
} | ||
return respUser | ||
} | ||
|
||
func (u *User) become() User { | ||
var respUser User | ||
becomeURL := apiURL + "user/me" | ||
becomeRequestHeader := &grequests.RequestOptions{ | ||
JSON: map[string]string{"sessionToken": u.SessionToken}, | ||
IsAjax: true, | ||
} | ||
resp, err := grequests.Post(becomeURL, becomeRequestHeader) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if resp.Ok != true { | ||
fmt.Println("Login Failed") | ||
} else { | ||
_ = json.Unmarshal(resp.Bytes(), &respUser) | ||
setCache("session-token", respUser.SessionToken) | ||
fmt.Printf("Hello, ") | ||
color.Cyan(respUser.Username) | ||
} | ||
return respUser | ||
} | ||
|
||
func (u *User) logOut() { | ||
err := deleteKey("session-token") | ||
if err == nil { | ||
fmt.Println("Logout Successfully") | ||
} else { | ||
panic(err) | ||
} | ||
} |