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.

154 lines
3.1 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 writeManifestFile(path, name, version string) error {
  20. var (
  21. cmd *exec.Cmd
  22. manifest string
  23. filePath string
  24. e error
  25. )
  26. manifest = fmt.Sprintf(`name: "%s"
  27. version: "%s"
  28. dependancies:
  29. - pkg1: v1.0.0
  30. - pkg2: v1.0.0
  31. `, name, version)
  32. filePath = filepath.Join(path, "manifest.yml")
  33. e = ioutil.WriteFile(filePath, []byte(manifest), 0644)
  34. if e != nil {
  35. return e
  36. }
  37. cmd = exec.Command(Variables.Editor, filePath)
  38. cmd.Stdin = os.Stdin
  39. cmd.Stdout = os.Stdout
  40. return cmd.Run()
  41. }
  42. func CreatePackage() error {
  43. var (
  44. picksBucket *bolt.Bucket
  45. pickedFiles []string
  46. pkgFiles []string
  47. choices string
  48. choicesSplit []string
  49. pkgName string
  50. pkgVersion string
  51. pkgNameVersion string
  52. tmpDir string
  53. index int
  54. e error
  55. )
  56. fmt.Println("Initialising package creation...")
  57. e = Database.FsDB.View(func(tx *bolt.Tx) error {
  58. picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
  59. picksBucket.ForEach(func(k, v []byte) error {
  60. pickedFiles = append(pickedFiles, string(k))
  61. return nil
  62. })
  63. return nil
  64. })
  65. fmt.Println("Added files:")
  66. Filesystem.PrintFiles(pickedFiles, Color.Green, true)
  67. fmt.Println("Please select the files you would like to use to create the package. Leave empty for all.")
  68. choices = Helper.Input()
  69. if choices == "" {
  70. pkgFiles = pickedFiles
  71. } else {
  72. choicesSplit = strings.Split(choices, ",")
  73. for _, i := range choicesSplit {
  74. index, e = strconv.Atoi(i)
  75. if e != nil {
  76. // TODO: Handle this error
  77. panic(e)
  78. }
  79. if len(pickedFiles) < index {
  80. return errors.New("Invalid choice")
  81. }
  82. pkgFiles = append(pkgFiles, pickedFiles[index])
  83. }
  84. }
  85. fmt.Println("Please enter the package name:")
  86. pkgName = Helper.Input()
  87. if pkgName == "" {
  88. return errors.New("Invalid package name")
  89. }
  90. fmt.Println("Please enter the package version:")
  91. pkgVersion = Helper.Input()
  92. if pkgVersion == "" {
  93. return errors.New("Invalid package name")
  94. }
  95. fmt.Printf("Package Name: %s\n", pkgName)
  96. fmt.Printf("Package Version: %s\n", pkgVersion)
  97. fmt.Println("Files to be added")
  98. Filesystem.PrintFiles(pkgFiles, Color.Green, false)
  99. fmt.Println("Is this correct? [y/N]")
  100. if strings.ToLower(Helper.Input()) != "y" {
  101. return errors.New("User aborted")
  102. }
  103. pkgNameVersion = fmt.Sprintf("%s-%s", pkgName, pkgVersion)
  104. tmpDir, e = ioutil.TempDir("/tmp", pkgNameVersion)
  105. if e != nil {
  106. return e
  107. }
  108. defer os.RemoveAll(tmpDir)
  109. for _, file := range pkgFiles {
  110. Filesystem.CopyFile(file, filepath.Join(tmpDir, file))
  111. }
  112. e = writeManifestFile(tmpDir, pkgName, pkgVersion)
  113. if e != nil {
  114. return e
  115. }
  116. e = Archive.TarGzip(tmpDir, pkgNameVersion+".tar.gz")
  117. if e != nil {
  118. return e
  119. }
  120. fmt.Printf(
  121. Color.Green("\nSuccessfully created package %s\n"),
  122. pkgNameVersion,
  123. )
  124. return nil
  125. }