Refactor logging and variable names for consistency; update log messages for clarity
This commit is contained in:
		@@ -27,7 +27,8 @@ var publicKey []byte
 | 
			
		||||
// init is doing some verifications
 | 
			
		||||
func init() {
 | 
			
		||||
	log.SetPrefix("error: ")
 | 
			
		||||
	log.SetFlags(log.Lshortfile)
 | 
			
		||||
	log.SetFlags(0)
 | 
			
		||||
	//log.SetFlags(log.Lshortfile)
 | 
			
		||||
	_, err := os.Stat(consts.DefaultLinux_d)
 | 
			
		||||
	if os.IsNotExist(err) {
 | 
			
		||||
		err := os.Mkdir(consts.DefaultLinux_d, 0777)
 | 
			
		||||
@@ -191,10 +192,10 @@ var installCmd = &cobra.Command{
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
				}
 | 
			
		||||
				if installed {
 | 
			
		||||
					fmt.Printf(":: Package %s is already installed\n", inputName)
 | 
			
		||||
					log.Printf("package %s is already installed\n", inputName)
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
				fmt.Printf(":: Checking dependencies of (%s)\n", inputName)
 | 
			
		||||
				fmt.Printf("Checking dependencies of (%s)\n", inputName)
 | 
			
		||||
				dependencies, err := utils.GetDependencies(inputName)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
@@ -219,7 +220,7 @@ var installCmd = &cobra.Command{
 | 
			
		||||
					wg.Wait()
 | 
			
		||||
 | 
			
		||||
				}
 | 
			
		||||
				fmt.Printf(":: Downloading (%s) \n", inputName)
 | 
			
		||||
				fmt.Printf("Downloading (%s) \n", inputName)
 | 
			
		||||
				p, err := utils.GetPackage(inputName)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
@@ -278,11 +279,11 @@ var installCmd = &cobra.Command{
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				if installed {
 | 
			
		||||
					fmt.Printf(":: Package %s is already installed\n", pkgs[0].Name)
 | 
			
		||||
					log.Printf("Package %s is already installed\n", pkgs[0].Name)
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				fmt.Printf(":: Checking dependencies of (%s)\n", pkgs[0].Name)
 | 
			
		||||
				fmt.Printf("Checking dependencies of (%s)\n", pkgs[0].Name)
 | 
			
		||||
				dependencies, err := utils.GetDependencies(pkgs[0].Name)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
@@ -309,7 +310,7 @@ var installCmd = &cobra.Command{
 | 
			
		||||
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				fmt.Printf(":: Downloading %s \n", pkgs[0].Name)
 | 
			
		||||
				fmt.Printf("Downloading %s \n", pkgs[0].Name)
 | 
			
		||||
				p, err := utils.GetPackage(pkgs[0].Name)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
@@ -362,11 +363,11 @@ var installCmd = &cobra.Command{
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
				}
 | 
			
		||||
				if installed {
 | 
			
		||||
					fmt.Printf(":: Package %s is already installed\n", pkgs[choice].Name)
 | 
			
		||||
					log.Printf("package %s is already installed\n", pkgs[choice].Name)
 | 
			
		||||
					continue
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				fmt.Printf(":: Checking dependencies of (%s)\n", pkgs[choice].Name)
 | 
			
		||||
				fmt.Printf("Checking dependencies of (%s)\n", pkgs[choice].Name)
 | 
			
		||||
				dependencies, err := utils.GetDependencies(pkgs[choice].Name)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
@@ -393,7 +394,7 @@ var installCmd = &cobra.Command{
 | 
			
		||||
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				fmt.Printf(":: Downloading %s \n", pkgs[choice].Name)
 | 
			
		||||
				fmt.Printf("Downloading %s \n", pkgs[choice].Name)
 | 
			
		||||
				p, err := utils.GetPackage(pkgs[choice].Name)
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
@@ -435,7 +436,7 @@ var removeCmd = &cobra.Command{
 | 
			
		||||
	Args:  cobra.MinimumNArgs(1),
 | 
			
		||||
	Short: "Remove a package from the given names",
 | 
			
		||||
	Run: func(cmd *cobra.Command, args []string) {
 | 
			
		||||
		fmt.Println(":: This command will remove permanently this packages, are you sure? (y/N)\n>>")
 | 
			
		||||
		fmt.Print(":: This command will remove permanently this packages, are you sure? (y/N)\n>> ")
 | 
			
		||||
		var a string
 | 
			
		||||
		fmt.Scanf("%s", &a)
 | 
			
		||||
		if a != "y" && a != "Y" {
 | 
			
		||||
@@ -458,7 +459,6 @@ var removeCmd = &cobra.Command{
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
				}
 | 
			
		||||
 | 
			
		||||
				fmt.Println(filepath.Join(packageDir, "manifest.toml"))
 | 
			
		||||
				f, err := os.Open(filepath.Join(packageDir, "manifest.toml"))
 | 
			
		||||
				if err != nil {
 | 
			
		||||
					log.Fatal(err)
 | 
			
		||||
@@ -483,6 +483,7 @@ var removeCmd = &cobra.Command{
 | 
			
		||||
 | 
			
		||||
				os.Exit(0)
 | 
			
		||||
			}
 | 
			
		||||
			log.Fatalf("%s not installed", pkgName)
 | 
			
		||||
		}
 | 
			
		||||
	},
 | 
			
		||||
}
 | 
			
		||||
@@ -497,14 +498,14 @@ func main() {
 | 
			
		||||
func AyncFullInstall(dep string, storePackages bool, installPath string, wg *sync.WaitGroup, mu *sync.Mutex) {
 | 
			
		||||
	defer wg.Done()
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("Downloading %s \n", dep)
 | 
			
		||||
	fmt.Printf(" Downloading %s \n", dep)
 | 
			
		||||
	p, err := utils.GetPackage(dep)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println("--ERROR--\n", err)
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("Installing %s \n", dep)
 | 
			
		||||
	fmt.Printf(" Installing %s \n", dep)
 | 
			
		||||
	if err := packets.InstallPackage(p.PackageF, installPath); err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
@@ -520,7 +521,7 @@ func AyncFullInstall(dep string, storePackages bool, installPath string, wg *syn
 | 
			
		||||
 | 
			
		||||
		err = p.AddToInstalledDB(1, installPath)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println("--ERROR--\n", err)
 | 
			
		||||
			log.Println(err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
 
 | 
			
		||||
@@ -23,7 +23,7 @@ func GetSandBox(sandboxdir string) (lua.LState, error) {
 | 
			
		||||
	L.SetGlobal("SAFE_MODE", lua.LTrue)
 | 
			
		||||
 | 
			
		||||
	L.SetGlobal("PACKETS_DATADIR", lua.LString(cfg.Config.Data_d))
 | 
			
		||||
	L.SetGlobal("PACKETS_BIN_DIR", lua.LString(cfg.Config.Bin_d))
 | 
			
		||||
	L.SetGlobal("packets_bin_dir", lua.LString(cfg.Config.Bin_d))
 | 
			
		||||
 | 
			
		||||
	L.SetGlobal("path_join", L.NewFunction(Ljoin))
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -119,15 +119,17 @@ func IsSafe(str string) bool {
 | 
			
		||||
 | 
			
		||||
func LSafeRemove(L *lua.LState) int {
 | 
			
		||||
	filename := L.CheckString(1)
 | 
			
		||||
	fmt.Printf("   remove %s\n", filename)
 | 
			
		||||
 | 
			
		||||
	if !IsSafe(filename) {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
		L.Push(lua.LString("unsafe filepath"))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
	err := os.RemoveAll(filename)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] remove failed\n" + err.Error()))
 | 
			
		||||
		L.Push(lua.LString(err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
	L.Push(lua.LTrue)
 | 
			
		||||
@@ -139,15 +141,16 @@ func LSafeRename(L *lua.LState) int {
 | 
			
		||||
	oldname := L.CheckString(1)
 | 
			
		||||
	newname := L.CheckString(2)
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("   move %s -> %s\n", oldname, newname)
 | 
			
		||||
	if !IsSafe(oldname) || !IsSafe(newname) {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
		L.Push(lua.LString("unsafe filepath"))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := os.Rename(oldname, newname); err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] rename failed\n" + err.Error()))
 | 
			
		||||
		L.Push(lua.LString(err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -158,6 +161,8 @@ func LSafeCopy(L *lua.LState) int {
 | 
			
		||||
	oldname := L.CheckString(1)
 | 
			
		||||
	newname := L.CheckString(2)
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("   copy %s -> %s\n", oldname, newname)
 | 
			
		||||
 | 
			
		||||
	if !IsSafe(oldname) || !IsSafe(newname) {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
@@ -166,7 +171,7 @@ func LSafeCopy(L *lua.LState) int {
 | 
			
		||||
 | 
			
		||||
	if err := utils.CopyDir(oldname, newname); err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] error while copy"))
 | 
			
		||||
		L.Push(lua.LString(err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -180,6 +185,8 @@ func LSymlink(L *lua.LState) int {
 | 
			
		||||
	fileName := L.CheckString(1)
 | 
			
		||||
	destination := L.CheckString(2)
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("   symlink %s -> %s\n", fileName, destination)
 | 
			
		||||
 | 
			
		||||
	if !IsSafe(fileName) || !IsSafe(destination) {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
@@ -189,7 +196,7 @@ func LSymlink(L *lua.LState) int {
 | 
			
		||||
	_ = os.RemoveAll(destination)
 | 
			
		||||
	if err := os.Symlink(fileName, destination); err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] symlink failed\n" + err.Error()))
 | 
			
		||||
		L.Push(lua.LString(err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -223,13 +230,13 @@ func LOpen(L *lua.LState) int {
 | 
			
		||||
 | 
			
		||||
	if !IsSafe(path) {
 | 
			
		||||
		L.Push(lua.LNil)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
		L.Push(lua.LString("unsafe filepath"))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
	file, err := os.OpenFile(path, modeFlags(mode), 0644)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LNil)
 | 
			
		||||
		L.Push(lua.LString("[packets] open failed\n" + err.Error()))
 | 
			
		||||
		L.Push(lua.LString(err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -259,16 +266,17 @@ func Ljoin(L *lua.LState) int {
 | 
			
		||||
func LMkdir(L *lua.LState) int {
 | 
			
		||||
	path := L.CheckString(1)
 | 
			
		||||
	perm := L.CheckInt(2)
 | 
			
		||||
	fmt.Printf("   mkdir %s \n", path)
 | 
			
		||||
 | 
			
		||||
	if !IsSafe(path) {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsafe filepath\n"))
 | 
			
		||||
		L.Push(lua.LString("unsafe filepath"))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err := os.MkdirAll(path, os.FileMode(perm)); err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] mkdir failed\n" + err.Error()))
 | 
			
		||||
		L.Push(lua.LString(err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -287,15 +295,14 @@ func LCompile(L *lua.LState) int {
 | 
			
		||||
			tryintoacess, err := filepath.Abs(filepath.Clean(filepath.Join(SandboxDir, L.CheckString(i))))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				L.Push(lua.LFalse)
 | 
			
		||||
				L.Push(lua.LString("[packets] invalid filepath\n" + err.Error()))
 | 
			
		||||
				L.Push(lua.LString(err.Error()))
 | 
			
		||||
				return 2
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			fmt.Printf("sandboxdir: (%s) acessto: (%s)\n", SandboxDir, tryintoacess)
 | 
			
		||||
			rel, err := filepath.Rel(SandboxDir, tryintoacess)
 | 
			
		||||
			if err != nil || strings.HasPrefix(rel, "..") {
 | 
			
		||||
				L.Push(lua.LFalse)
 | 
			
		||||
				L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
				L.Push(lua.LString("unsafe filepath"))
 | 
			
		||||
				return 2
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
@@ -306,21 +313,22 @@ func LCompile(L *lua.LState) int {
 | 
			
		||||
	bin, suc := AllowedCmds[lang]
 | 
			
		||||
	if !suc {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] unsupported language"))
 | 
			
		||||
		L.Push(lua.LString("unsupported language"))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	cmd := exec.Command(bin, args...)
 | 
			
		||||
	cmd.Dir = SandboxDir
 | 
			
		||||
	out, err := cmd.CombinedOutput()
 | 
			
		||||
	fmt.Printf("   compiling with %s", bin)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] compile failed\n" + err.Error() + "\n" + string(out)))
 | 
			
		||||
		L.Push(lua.LString(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()))
 | 
			
		||||
		L.Push(lua.LString(err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -338,18 +346,19 @@ func LCompileRequirements(L *lua.LState) int {
 | 
			
		||||
		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()))
 | 
			
		||||
			L.Push(lua.LString(err.Error()))
 | 
			
		||||
			return 2
 | 
			
		||||
		}
 | 
			
		||||
		if !strings.HasPrefix(tryintoacess, SandboxDir) {
 | 
			
		||||
			L.Push(lua.LFalse)
 | 
			
		||||
			L.Push(lua.LString("[packets] unsafe filepath"))
 | 
			
		||||
			L.Push(lua.LString("unsafe filepath"))
 | 
			
		||||
			return 2
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	var err error
 | 
			
		||||
 | 
			
		||||
	fmt.Printf("   installing requirements with %s", cmdLang)
 | 
			
		||||
	switch cmdLang {
 | 
			
		||||
	case "python":
 | 
			
		||||
		cmd := exec.Command("pip", "install", "--target", filepath.Join(SandboxDir, "tmp/build"), "-r", L.CheckString(2))
 | 
			
		||||
@@ -367,7 +376,7 @@ func LCompileRequirements(L *lua.LState) int {
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		L.Push(lua.LFalse)
 | 
			
		||||
		L.Push(lua.LString("[packets] requirements install failed\n" + err.Error()))
 | 
			
		||||
		L.Push(lua.LString(err.Error()))
 | 
			
		||||
		return 2
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@@ -375,3 +384,18 @@ func LCompileRequirements(L *lua.LState) int {
 | 
			
		||||
	L.Push(lua.LNil)
 | 
			
		||||
	return 2
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func LError(L *lua.LState) int {
 | 
			
		||||
	n := L.GetTop()
 | 
			
		||||
	parts := make([]any, 0, n)
 | 
			
		||||
 | 
			
		||||
	for i := 1; i <= n; i++ {
 | 
			
		||||
		val := L.Get(i)
 | 
			
		||||
		parts = append(parts, val.String())
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	Llogger().Panic(parts...)
 | 
			
		||||
	return 0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func Llogger() *log.Logger { return log.New(os.Stderr, "   script error: ", 0) }
 | 
			
		||||
 
 | 
			
		||||
@@ -89,7 +89,7 @@ func InstallPackage(file []byte, destDir string) error {
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	L.SetGlobal("DATA_DIR", lua.LString(filepath.Join(destDir, "data")))
 | 
			
		||||
	L.SetGlobal("data_dir", lua.LString(filepath.Join(destDir, "data")))
 | 
			
		||||
	L.SetGlobal("script", lua.LString(manifest.Hooks.Install))
 | 
			
		||||
 | 
			
		||||
	if err := L.DoFile(filepath.Join(destDir, manifest.Hooks.Install)); err != nil {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user