Improved package messages and added two functions to compile from source code
This commit is contained in:
		@@ -171,12 +171,13 @@ var installCmd = &cobra.Command{
 | 
			
		||||
			return
 | 
			
		||||
 | 
			
		||||
		default:
 | 
			
		||||
			fmt.Printf(":: Founded %d versions for %s\n Select 1\n", len(pkgs), nameToQuery)
 | 
			
		||||
			fmt.Printf(":: Founded %d versions for (%s)\n Select 1 to install\n", len(pkgs), nameToQuery)
 | 
			
		||||
			for i, q := range pkgs {
 | 
			
		||||
				fmt.Printf("[%d] %s : %s\n     %s\n", i, q.Realname, q.Version, q.Description)
 | 
			
		||||
			}
 | 
			
		||||
			var choice int
 | 
			
		||||
 | 
			
		||||
			fmt.Print(">> ")
 | 
			
		||||
			fmt.Fscan(bufio.NewReader(os.Stdin), &choice)
 | 
			
		||||
			if choice > len(pkgs) || choice < 0 {
 | 
			
		||||
				fmt.Println("invalid option")
 | 
			
		||||
@@ -495,10 +496,11 @@ func Install(packagepath string, serial uint) error {
 | 
			
		||||
	var destDir = filepath.Join(cfg.Config.DataDir, name)
 | 
			
		||||
 | 
			
		||||
	if cfg.Config.LastDataDir != cfg.Config.DataDir {
 | 
			
		||||
		fmt.Printf("Ooops... Data directory has been changed from (%s), to (%s), do you want to cancel the installation and Sync first?\n y/n? ", cfg.Config.LastDataDir, cfg.Config.DataDir)
 | 
			
		||||
		fmt.Printf("Ooops... Data directory has been changed from (%s), to (%s), do you want to cancel the installation and Sync first? [Y/n]\n", cfg.Config.LastDataDir, cfg.Config.DataDir)
 | 
			
		||||
 | 
			
		||||
		var answer string
 | 
			
		||||
 | 
			
		||||
		fmt.Print(">> ")
 | 
			
		||||
		fmt.Scan(&answer)
 | 
			
		||||
 | 
			
		||||
		if answer == "y" || answer == "Y" {
 | 
			
		||||
@@ -1151,7 +1153,8 @@ func Sync(url string) error {
 | 
			
		||||
		fmt.Println("[y] Yes [n] No, [x] Ignore it and stop to show this message (not recommended)")
 | 
			
		||||
 | 
			
		||||
		var answer string
 | 
			
		||||
		fmt.Scanln(&answer)
 | 
			
		||||
		fmt.Print(">> ")
 | 
			
		||||
		fmt.Scan(&answer)
 | 
			
		||||
 | 
			
		||||
		switch answer {
 | 
			
		||||
		case "n":
 | 
			
		||||
@@ -1172,7 +1175,8 @@ func Sync(url string) error {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			f.WriteString("\n\n# BE CAREFULL CHANGING BIN_DIR, BECAUSE THE BINARIES DON'T MOVE AUTOMATICALLY\n# NEVER CHANGE lastDataDir")
 | 
			
		||||
			os.Remove(cfg.Config.LastDataDir)
 | 
			
		||||
 | 
			
		||||
			return nil
 | 
			
		||||
 | 
			
		||||
		case "y":
 | 
			
		||||
			if err := os.MkdirAll(cfg.Config.DataDir, 0755); err != nil {
 | 
			
		||||
@@ -1227,6 +1231,7 @@ func Sync(url string) error {
 | 
			
		||||
			os.Remove(cfg.Config.LastDataDir)
 | 
			
		||||
			bar.Finish()
 | 
			
		||||
 | 
			
		||||
			return nil
 | 
			
		||||
		default:
 | 
			
		||||
			if err := os.MkdirAll(cfg.Config.DataDir, 0755); err != nil {
 | 
			
		||||
				return err
 | 
			
		||||
@@ -1262,7 +1267,7 @@ func Sync(url string) error {
 | 
			
		||||
				return err
 | 
			
		||||
			}
 | 
			
		||||
			bar.Finish()
 | 
			
		||||
 | 
			
		||||
			return nil
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -1325,7 +1330,8 @@ func Unninstall(realname string) error {
 | 
			
		||||
	}
 | 
			
		||||
	fmt.Printf(":: Sure you will remove %s ? [Y/n] ", realname)
 | 
			
		||||
	var answer string
 | 
			
		||||
	fmt.Scanf("%s", &answer)
 | 
			
		||||
	fmt.Print(">> ")
 | 
			
		||||
	fmt.Scan(&answer)
 | 
			
		||||
 | 
			
		||||
	if answer != "y" && answer != "Y" {
 | 
			
		||||
		return fmt.Errorf("operation cancelled")
 | 
			
		||||
@@ -1459,10 +1465,10 @@ func Upgrade(packagepath string, og_realname string, serial uint) error {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if cfg.Config.LastDataDir != cfg.Config.DataDir {
 | 
			
		||||
		fmt.Printf("Ooops... Data directory has been changed from (%s), to (%s), do you want to cancel the installation and Sync first?\n y/n? ", cfg.Config.LastDataDir, cfg.Config.DataDir)
 | 
			
		||||
		fmt.Printf("Ooops... Data directory has been changed from (%s), to (%s), do you want to cancel the installation and Sync first? [Y/n]\n", cfg.Config.LastDataDir, cfg.Config.DataDir)
 | 
			
		||||
 | 
			
		||||
		var answer string
 | 
			
		||||
 | 
			
		||||
		fmt.Print(">> ")
 | 
			
		||||
		fmt.Scan(&answer)
 | 
			
		||||
 | 
			
		||||
		if answer == "y" || answer == "Y" {
 | 
			
		||||
 
 | 
			
		||||
@@ -16,6 +16,21 @@ import (
 | 
			
		||||
	lua "github.com/yuin/gopher-lua"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var AllowedCmds = map[string]string{
 | 
			
		||||
	"go":          "go",          // "Go code compiler"
 | 
			
		||||
	"gcc":         "gcc",         // "C"
 | 
			
		||||
	"g++":         "g++",         // "C++"
 | 
			
		||||
	"rustc":       "rustc",       // "Rust"
 | 
			
		||||
	"javac":       "javac",       // "Java"
 | 
			
		||||
	"luac":        "luac",        // "Lua"
 | 
			
		||||
	"pyinstaller": "pyinstaller", // "Python"
 | 
			
		||||
	"kotlinc":     "kotlinc",     // "Kotlin"
 | 
			
		||||
	"mcs":         "mcs",         // "C# compiler"
 | 
			
		||||
	"swiftc":      "swiftc",      // "Swift compiler"
 | 
			
		||||
	"ts":          "tsc",         // "TypeScript compiler"
 | 
			
		||||
	"ruby":        "rubyc",       // "Ruby compiler"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type ConfigTOML struct {
 | 
			
		||||
	Config struct {
 | 
			
		||||
		HttpPort           int    `toml:"httpPort"`
 | 
			
		||||
@@ -437,3 +452,99 @@ func LMkdir(L *lua.LState) int {
 | 
			
		||||
	L.Push(lua.LNil)
 | 
			
		||||
	return 2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func LuaCompile(L *lua.LState, sandbox string) int {
 | 
			
		||||
	lang := L.CheckString(1)
 | 
			
		||||
	args := []string{}
 | 
			
		||||
	for i := 2; i <= L.GetTop(); i++ {
 | 
			
		||||
 | 
			
		||||
		if strings.Contains(L.CheckString(i), "/") {
 | 
			
		||||
 | 
			
		||||
			tryintoacess, err := filepath.Abs(filepath.Clean(L.CheckString(i)))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				L.Push(lua.LFalse)
 | 
			
		||||
				L.Push(lua.LString("[packets] invalid filepath\n" + err.Error()))
 | 
			
		||||
				return 2
 | 
			
		||||
			}
 | 
			
		||||
			if !strings.HasPrefix(tryintoacess, sandbox) {
 | 
			
		||||
				L.Push(lua.LFalse)
 | 
			
		||||
				L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
				return 2
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		args = append(args, L.CheckString(i))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	bin, suc := AllowedCmds[lang]
 | 
			
		||||
	if !suc {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsupported language"))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cmd := exec.Command(bin, args...)
 | 
			
		||||
	cmd.Dir = sandbox
 | 
			
		||||
	out, err := cmd.CombinedOutput()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] compile failed\n" + err.Error() + "\n" + string(out)))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
	if err := cmd.Run(); err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] compile failed\n" + err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	L.Push(lua.LTrue)
 | 
			
		||||
	L.Push(lua.LString(string(out)))
 | 
			
		||||
	return 2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func CompileRequirements(L *lua.LState, sandbox string) int {
 | 
			
		||||
 | 
			
		||||
	cmdLang := L.CheckString(1)
 | 
			
		||||
 | 
			
		||||
	if strings.Contains(L.CheckString(2), "/") {
 | 
			
		||||
 | 
			
		||||
		tryintoacess, err := filepath.Abs(filepath.Clean(L.CheckString(2)))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			L.Push(lua.LFalse)
 | 
			
		||||
			L.Push(lua.LString("[packets] invalid filepath\n" + err.Error()))
 | 
			
		||||
			return 2
 | 
			
		||||
		}
 | 
			
		||||
		if !strings.HasPrefix(tryintoacess, sandbox) {
 | 
			
		||||
			L.Push(lua.LFalse)
 | 
			
		||||
			L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
			return 2
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var err error
 | 
			
		||||
 | 
			
		||||
	switch cmdLang {
 | 
			
		||||
	case "python":
 | 
			
		||||
		cmd := exec.Command("pip", "install", "--target", filepath.Join(sandbox, "tmp/build"), "-r", L.CheckString(2))
 | 
			
		||||
		cmd.Dir = filepath.Join(sandbox, "data")
 | 
			
		||||
		err = cmd.Run()
 | 
			
		||||
	case "java":
 | 
			
		||||
		cmd := exec.Command("mvn", "dependency:copy-dependencies", "-DoutputDirectory="+filepath.Join(sandbox, "tmp/build"))
 | 
			
		||||
		cmd.Dir = L.CheckString(2)
 | 
			
		||||
		err = cmd.Run()
 | 
			
		||||
	case "ruby":
 | 
			
		||||
		cmd := exec.Command("bundle", "install", "--path", filepath.Join(sandbox, "tmp/build"))
 | 
			
		||||
		cmd.Dir = L.CheckString(2)
 | 
			
		||||
		err = cmd.Run()
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] requirements install failed\n" + err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	L.Push(lua.LTrue)
 | 
			
		||||
	L.Push(lua.LNil)
 | 
			
		||||
	return 2
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user