|
|
- package Package
-
- import (
- "errors"
- "fmt"
- "io/ioutil"
- "os"
- "os/exec"
- "path/filepath"
- "strconv"
- "strings"
-
- bolt "go.etcd.io/bbolt"
-
- "PackageManager/Archive"
- "PackageManager/Client/Database"
- "PackageManager/Client/Filesystem"
- "PackageManager/Color"
- "PackageManager/Helper"
- "PackageManager/Variables"
- )
-
- func editManifestFile(filePath string) error {
- var (
- cmd *exec.Cmd
- )
- cmd = exec.Command(Variables.Editor, filePath)
- cmd.Stdin = os.Stdin
- cmd.Stdout = os.Stdout
- return cmd.Run()
- }
-
- func writeManifestFile(path, name, version string) error {
- var (
- manifest string
- filePath string
- e error
- )
-
- manifest, e = Manifest{
- Name: name,
- Version: version,
- Dependancies: make(map[string]string),
- }.CreateManifestString()
-
- if e != nil {
- return e
- }
-
- filePath = filepath.Join(path, "manifest.yml")
-
- return ioutil.WriteFile(filePath, []byte(manifest), 0644)
- }
-
- func CreatePackage() error {
- var (
- picksBucket *bolt.Bucket
- pickedFiles []string
- pkgFiles []string
- choices string
- choicesSplit []string
- pkgName string
- pkgVersion string
- pkgNameVersion string
- pkg string
- index int
- e error
- )
-
- fmt.Println("Initialising package creation...")
-
- e = Database.FsDB.View(func(tx *bolt.Tx) error {
- picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
-
- picksBucket.ForEach(func(k, v []byte) error {
- pickedFiles = append(pickedFiles, string(k))
- return nil
- })
-
- return nil
- })
-
- fmt.Println("Added files:")
- Filesystem.PrintFiles(pickedFiles, Color.Green, true)
-
- fmt.Println("Please select the files you would like to use to create the package. Leave empty for all.")
- choices = Helper.Input()
-
- if choices == "" {
- for _, pkg = range pickedFiles {
- pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, pkg))
- }
- } else {
- choicesSplit = strings.Split(choices, ",")
-
- for _, i := range choicesSplit {
- index, e = strconv.Atoi(i)
- if e != nil {
- // TODO: Handle this error
- panic(e)
- }
- if len(pickedFiles) < index {
- return errors.New("Invalid choice")
- }
- pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, pickedFiles[index]))
- }
- }
-
- fmt.Println("Please enter the package name:")
- pkgName = Helper.Input()
- if pkgName == "" {
- return errors.New("Invalid package name")
- }
-
- fmt.Println("Please enter the package version:")
- pkgVersion = Helper.Input()
- if pkgVersion == "" {
- return errors.New("Invalid package name")
- }
-
- fmt.Printf("Package Name: %s\n", pkgName)
- fmt.Printf("Package Version: %s\n", pkgVersion)
-
- fmt.Println("Files to be added")
- Filesystem.PrintFiles(pkgFiles, Color.Green, false)
-
- fmt.Println("Is this correct? [y/N]")
- if strings.ToLower(Helper.Input()) != "y" {
- return errors.New("User aborted")
- }
-
- pkgNameVersion = fmt.Sprintf("%s-%s", pkgName, pkgVersion)
-
- e = writeManifestFile("/tmp/", pkgName, pkgVersion)
- if e != nil {
- return e
- }
-
- e = editManifestFile(filepath.Join("/tmp/", "manifest.yml"))
- if e != nil {
- fmt.Println(Color.Fatal(e.Error))
- }
-
- // TODO: Write this file to a better spot?
- pkgFiles = append(pkgFiles, "/tmp/manifest.yml")
-
- e = Archive.CreateArchive(pkgFiles, pkgNameVersion+".tar.gz")
- if e != nil {
- return e
- }
-
- fmt.Printf(
- Color.Green("\nSuccessfully created package %s\n"),
- pkgNameVersion,
- )
-
- return nil
- }
|