Clean Install function and Download function
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
package internal
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/klauspost/compress/zstd"
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
)
|
||||
|
||||
// const
|
||||
@@ -17,6 +22,25 @@ const DefaultData_d = "/opt/packets"
|
||||
|
||||
// errors
|
||||
var ErrResponseNot200OK = errors.New("the request is not 200, download failed")
|
||||
var ErrCantFindManifestTOML = errors.New("can't find manifest.toml when trying to read the packagefile")
|
||||
|
||||
// toml files
|
||||
|
||||
type Manifest struct {
|
||||
Info struct {
|
||||
Name string `toml:"name"`
|
||||
Version string `toml:"version"`
|
||||
Description string `toml:"description"`
|
||||
Dependencies []string `toml:"dependencies"`
|
||||
Author string `toml:"author"`
|
||||
Family string `toml:"family"`
|
||||
Serial uint `toml:"serial"`
|
||||
} `toml:"Info"`
|
||||
Hooks struct {
|
||||
Install string `toml:"install"`
|
||||
Remove string `toml:"remove"`
|
||||
} `toml:"Hooks"`
|
||||
}
|
||||
|
||||
type ConfigTOML struct {
|
||||
Config struct {
|
||||
@@ -77,3 +101,52 @@ func DefaultConfigTOML() (*ConfigTOML, error) {
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
func ReadManifest(file *os.File) (*Manifest, error) {
|
||||
zstdReader, err := zstd.NewReader(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer zstdReader.Close()
|
||||
|
||||
tarReader := tar.NewReader(zstdReader)
|
||||
for {
|
||||
header, err := tarReader.Next()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if filepath.Base(header.Name) == "manifest.toml" {
|
||||
decoder := toml.NewDecoder(tarReader)
|
||||
|
||||
var manifest Manifest
|
||||
|
||||
if err := decoder.Decode(&manifest); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return &manifest, nil
|
||||
}
|
||||
|
||||
}
|
||||
return nil, ErrCantFindManifestTOML
|
||||
}
|
||||
|
||||
func GetConfigTOML() (*ConfigTOML, error) {
|
||||
f, err := os.Open(filepath.Join(DefaultLinux_d, "config.toml"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
decoder := toml.NewDecoder(f)
|
||||
|
||||
var config ConfigTOML
|
||||
if err := decoder.Decode(&config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user