|
|
- package Package
-
- import (
- "errors"
- "fmt"
- "io/ioutil"
- "os"
- "path/filepath"
- "strconv"
- "strings"
-
- "PackageManager/Archive"
- "PackageManager/Client/Filesystem"
- "PackageManager/Color"
- "PackageManager/Helper"
- )
-
- func CreatePackage() error {
- var (
- dirtyFiles map[int]string
- newFiles map[int]string
- pkgFiles map[int]string = make(map[int]string)
- choices string
- choicesSplit []string
- filePath string
- pkgName string
- pkgVersion string
- pkgNameVersion string
- tmpDir string
- index int
- ok bool
- e error
- )
-
- fmt.Println("Initialising package creation...")
-
- dirtyFiles, newFiles, e = Filesystem.GetFilesystemDiff()
- if e != nil {
- return e
- }
-
- fmt.Println("\nModified files...")
- for i, file := range dirtyFiles {
- fmt.Printf(
- "\t%d - %s\n",
- i,
- Color.Red(file),
- )
- }
-
- fmt.Println("\nNew files...")
- for i, file := range newFiles {
- fmt.Printf(
- "\t%d - %s\n",
- i,
- Color.Red(file),
- )
- }
-
- 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 i, file := range dirtyFiles {
- pkgFiles[i] = file
- }
- for i, file := range newFiles {
- pkgFiles[i] = file
- }
- } else {
-
- choicesSplit = strings.Split(choices, ",")
-
- for _, i := range choicesSplit {
- index, e = strconv.Atoi(i)
- if e != nil {
- // TODO: Handle this error
- panic(e)
- }
- filePath, ok = dirtyFiles[index]
- if !ok {
- filePath, ok = dirtyFiles[index]
- if !ok {
- return errors.New("Invalid package selection")
- }
- }
- pkgFiles[index] = filePath
- }
- }
-
- 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")
- for i, file := range pkgFiles {
- fmt.Printf(
- "\t%d - %s\n",
- i,
- Color.Green(file),
- )
- }
-
- 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)
-
- tmpDir, e = ioutil.TempDir("/tmp", pkgNameVersion)
- if e != nil {
- return e
- }
- defer os.RemoveAll(tmpDir)
-
- for _, file := range pkgFiles {
- Filesystem.CopyFile(file, filepath.Join(tmpDir, file))
- }
-
- e = Archive.TarGzip(tmpDir, pkgNameVersion+".tar.gz")
- if e != nil {
- return e
- }
-
- fmt.Printf(
- Color.Green("\nSuccessfully created package %s\n"),
- pkgNameVersion,
- )
-
- return nil
- }
|