PackageManager just because
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
3.2 KiB

3 years ago
3 years ago
3 years ago
  1. package Package
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "os/exec"
  8. "path/filepath"
  9. "strconv"
  10. "strings"
  11. bolt "go.etcd.io/bbolt"
  12. "PackageManager/Archive"
  13. "PackageManager/Client/Database"
  14. "PackageManager/Client/Filesystem"
  15. "PackageManager/Color"
  16. "PackageManager/Helper"
  17. "PackageManager/Variables"
  18. )
  19. func editManifestFile(filePath string) error {
  20. var (
  21. cmd *exec.Cmd
  22. )
  23. cmd = exec.Command(Variables.Editor, filePath)
  24. cmd.Stdin = os.Stdin
  25. cmd.Stdout = os.Stdout
  26. return cmd.Run()
  27. }
  28. func writeManifestFile(path, name, version string) error {
  29. var (
  30. manifest string
  31. filePath string
  32. e error
  33. )
  34. manifest, e = Manifest{
  35. Name: name,
  36. Version: version,
  37. Dependancies: make(map[string]string),
  38. }.CreateManifestString()
  39. if e != nil {
  40. return e
  41. }
  42. filePath = filepath.Join(path, "manifest.yml")
  43. return ioutil.WriteFile(filePath, []byte(manifest), 0644)
  44. }
  45. func CreatePackage() error {
  46. var (
  47. picksBucket *bolt.Bucket
  48. pickedFiles []string
  49. pkgFiles []string
  50. choices string
  51. choicesSplit []string
  52. pkgName string
  53. pkgVersion string
  54. pkgNameVersion string
  55. index int
  56. err error
  57. )
  58. fmt.Println("Initialising package creation...")
  59. err = Database.FsDB.View(func(tx *bolt.Tx) error {
  60. picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
  61. picksBucket.ForEach(func(k, v []byte) error {
  62. pickedFiles = append(pickedFiles, string(k))
  63. return nil
  64. })
  65. return nil
  66. })
  67. if err != nil {
  68. return err
  69. }
  70. fmt.Println("Added files:")
  71. Filesystem.PrintFiles(pickedFiles, Color.Green, true)
  72. fmt.Println("Please select the files you would like to use to create the package. Leave empty for all.")
  73. choices = Helper.Input()
  74. if choices == "" {
  75. pkgFiles = append(pkgFiles, pickedFiles...)
  76. } else {
  77. choicesSplit = strings.Split(choices, ",")
  78. for _, i := range choicesSplit {
  79. index, err = strconv.Atoi(i)
  80. if err != nil {
  81. return err
  82. }
  83. if len(pickedFiles) < index {
  84. return errors.New("invalid choice")
  85. }
  86. pkgFiles = append(pkgFiles, pickedFiles[index])
  87. }
  88. }
  89. fmt.Println("Please enter the package name:")
  90. pkgName = Helper.Input()
  91. if pkgName == "" {
  92. return errors.New("invalid package name")
  93. }
  94. fmt.Println("Please enter the package version:")
  95. pkgVersion = Helper.Input()
  96. if pkgVersion == "" {
  97. return errors.New("invalid package name")
  98. }
  99. fmt.Printf("Package Name: %s\n", pkgName)
  100. fmt.Printf("Package Version: %s\n", pkgVersion)
  101. fmt.Println("Files to be added")
  102. Filesystem.PrintFiles(pkgFiles, Color.Green, false)
  103. // TODO: Move this check to below manifest file
  104. fmt.Println("Is this correct? [y/N]")
  105. if strings.ToLower(Helper.Input()) != "y" {
  106. return errors.New("user aborted")
  107. }
  108. pkgNameVersion = fmt.Sprintf("%s-%s", pkgName, pkgVersion)
  109. err = writeManifestFile("/tmp/", pkgName, pkgVersion)
  110. if err != nil {
  111. return err
  112. }
  113. err = editManifestFile(filepath.Join("/tmp/", "manifest.yml"))
  114. if err != nil {
  115. fmt.Println(Color.Fatal(err))
  116. }
  117. // TODO: Write this file to a better spot?
  118. pkgFiles = append(pkgFiles, "/tmp/manifest.yml")
  119. err = Archive.CreateArchive(pkgFiles, pkgNameVersion+".tar.gz")
  120. if err != nil {
  121. return err
  122. }
  123. fmt.Printf(
  124. Color.Green("\nSuccessfully created package %s\n"),
  125. pkgNameVersion,
  126. )
  127. return nil
  128. }