forked from slope-lang/slope
Very basic module installation flag added
parent
d86516f34f
commit
4a916d6fb3
@ -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…
Reference in New Issue