Clean Install function and Download function

This commit is contained in:
2025-09-13 22:57:52 -03:00
parent 8de2eaced7
commit f3ccd6d683
4 changed files with 162 additions and 0 deletions

View File

@@ -1,15 +1,19 @@
package main
import (
"archive/tar"
"database/sql"
"errors"
"fmt"
"io"
"log"
"os"
"packets/internal"
"path/filepath"
"strings"
"time"
"github.com/klauspost/compress/zstd"
"github.com/pelletier/go-toml/v2"
"github.com/spf13/cobra"
_ "modernc.org/sqlite"
@@ -97,6 +101,88 @@ func init() {
}
}
// Install exctract and install from a package file
func Install(file *os.File) error {
manifest, err := internal.ReadManifest(file)
if err != nil {
return err
}
name := &manifest.Info.Name
configuration, err := internal.GetConfigTOML()
if err != nil {
return err
}
destDir := filepath.Join(configuration.Config.Data_d, *name)
zstdReader, err := zstd.NewReader(file)
if err != nil {
return err
}
defer zstdReader.Close()
tarReader := tar.NewReader(zstdReader)
for {
hdr, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
rel := filepath.Clean(hdr.Name)
if rel == ".." || strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
continue
}
if err := os.MkdirAll(destDir, 0755); err != nil {
return err
}
absPath := filepath.Join(destDir, rel)
switch hdr.Typeflag {
case tar.TypeDir:
err = os.MkdirAll(absPath, os.FileMode(hdr.Mode))
if err != nil {
return err
}
case tar.TypeReg:
err = os.MkdirAll(filepath.Dir(absPath), 0755)
if err != nil {
return err
}
out, err := os.Create(absPath)
if err != nil {
return err
}
_, err = io.Copy(out, tarReader)
out.Close()
if err != nil {
return err
}
err = os.Chmod(absPath, os.FileMode(hdr.Mode))
if err != nil {
return err
}
}
}
return nil
}
// COBRA CMDS
var rootCmd = &cobra.Command{Use: "packets"}