Very basic module installation flag added

master
sloum 2 years ago
parent d86516f34f
commit 4a916d6fb3

@ -280,6 +280,7 @@ func main() {
verFlag := flag.Bool("v", false, "Print version information and exit")
runCommand := flag.String("run", "", "A quoted string of commands to run and get output from")
preloadEnv := flag.Bool("L", false, "Preload all files in the slope preload directory")
moduleInstall := flag.String("install", "", "URL to a module's git repo")
flag.Parse()
args := flag.Args()
@ -288,8 +289,12 @@ func main() {
os.Exit(0)
}
Init()
if *moduleInstall != "" {
InstallModule(*moduleInstall)
os.Exit(0)
}
Init()
if *runCommand != "" {
if *preloadEnv {

@ -0,0 +1,78 @@
package main
import (
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
func ModName(url, modDir string) string {
base := path.Base(url)
if strings.HasSuffix(base, ".git") {
base = base[:len(base)-4]
}
_, err := os.Stat(filepath.Join(modDir, base))
if err != nil && base != "" {
return base
}
modbase := base
var agreement string
for {
fmt.Printf("---> The module %q already exists.\n---> Enter an alternate import name to use or ^C to cancel: ", modbase)
fmt.Scanln(&base)
fmt.Printf("-> Are you happy with %q?\n-> Type 'yes' to proceed: ", base)
fmt.Scanln(&agreement)
if strings.ToLower(agreement) == "yes" {
break
}
}
return base
}
func Clone(url, modDir string) (string, error) {
pwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("Unable to get current working directory\n")
}
defer func(){os.Chdir(pwd)}()
err = os.Chdir(modDir)
if err != nil && os.IsNotExist(err) {
createDataDirs(modDir)
err := os.Chdir(modDir)
if err != nil {
return "", fmt.Errorf("Unable to navigate to module directory: %s \n", modDir)
}
} else if err != nil {
return "", fmt.Errorf("Unable to navigate to module directory: %s\n", modDir)
}
moduleName := ModName(url, modDir)
fmt.Printf("Cloning %q...\n", moduleName)
cmd := exec.Command("git", "clone", url, moduleName)
err = cmd.Run()
if err != nil {
return "", err
}
return filepath.Join(modDir, moduleName), nil
}
func InstallModule(url string) {
fmt.Println("NOTE: Submodules and module dependencies are not yet supported and will need to be acquired manually by a separate module installation")
modDir := getModBaseDir()
p, err := Clone(url, modDir)
if err != nil || p == "" {
fmt.Fprintf(os.Stderr, err.Error())
return
}
_, err = os.Stat(filepath.Join(p, "main.slo"))
if err != nil && os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "Invalid module, no 'main.slo' file. Removing from module folder\n")
_ = os.RemoveAll(p)
return
}
fmt.Println("Module successfully installed")
}
Loading…
Cancel
Save