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.

152 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, e = Manifest{
  27. Name: name,
  28. Version: version,
  29. Dependancies: make(map[string]string),
  30. }.CreateManifestString()
  31. if e != nil {
  32. return e
  33. }
  34. filePath = filepath.Join(path, "manifest.yml")
  35. e = ioutil.WriteFile(filePath, []byte(manifest), 0644)
  36. if e != nil {
  37. return e
  38. }
  39. cmd = exec.Command(Variables.Editor, filePath)
  40. cmd.Stdin = os.Stdin
  41. cmd.Stdout = os.Stdout
  42. return cmd.Run()
  43. }
  44. func CreatePackage() error {
  45. var (
  46. picksBucket *bolt.Bucket
  47. pickedFiles []string
  48. pkgFiles []string
  49. choices string
  50. choicesSplit []string
  51. pkgName string
  52. pkgVersion string
  53. pkgNameVersion string
  54. pkg string
  55. index int
  56. e error
  57. )
  58. fmt.Println("Initialising package creation...")
  59. e = 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. fmt.Println("Added files:")
  68. Filesystem.PrintFiles(pickedFiles, Color.Green, true)
  69. fmt.Println("Please select the files you would like to use to create the package. Leave empty for all.")
  70. choices = Helper.Input()
  71. if choices == "" {
  72. for _, pkg = range pickedFiles {
  73. pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, pkg))
  74. }
  75. } else {
  76. choicesSplit = strings.Split(choices, ",")
  77. for _, i := range choicesSplit {
  78. index, e = strconv.Atoi(i)
  79. if e != nil {
  80. // TODO: Handle this error
  81. panic(e)
  82. }
  83. if len(pickedFiles) < index {
  84. return errors.New("Invalid choice")
  85. }
  86. pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, 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. fmt.Println("Is this correct? [y/N]")
  104. if strings.ToLower(Helper.Input()) != "y" {
  105. return errors.New("User aborted")
  106. }
  107. pkgNameVersion = fmt.Sprintf("%s-%s", pkgName, pkgVersion)
  108. e = writeManifestFile("/tmp/", pkgName, pkgVersion)
  109. if e != nil {
  110. return e
  111. }
  112. // TODO: Write this file to a better spot?
  113. pkgFiles = append(pkgFiles, "/tmp/manifest.yml")
  114. e = Archive.CreateArchive(pkgFiles, pkgNameVersion+".tar.gz")
  115. if e != nil {
  116. return e
  117. }
  118. fmt.Printf(
  119. Color.Green("\nSuccessfully created package %s\n"),
  120. pkgNameVersion,
  121. )
  122. return nil
  123. }