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.3 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. pkg string
  56. index int
  57. e error
  58. )
  59. fmt.Println("Initialising package creation...")
  60. e = Database.FsDB.View(func(tx *bolt.Tx) error {
  61. picksBucket = tx.Bucket(Variables.FsHashPicksBucket)
  62. picksBucket.ForEach(func(k, v []byte) error {
  63. pickedFiles = append(pickedFiles, string(k))
  64. return nil
  65. })
  66. return nil
  67. })
  68. fmt.Println("Added files:")
  69. Filesystem.PrintFiles(pickedFiles, Color.Green, true)
  70. fmt.Println("Please select the files you would like to use to create the package. Leave empty for all.")
  71. choices = Helper.Input()
  72. if choices == "" {
  73. for _, pkg = range pickedFiles {
  74. pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, pkg))
  75. }
  76. } else {
  77. choicesSplit = strings.Split(choices, ",")
  78. for _, i := range choicesSplit {
  79. index, e = strconv.Atoi(i)
  80. if e != nil {
  81. // TODO: Handle this error
  82. panic(e)
  83. }
  84. if len(pickedFiles) < index {
  85. return errors.New("Invalid choice")
  86. }
  87. pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, pickedFiles[index]))
  88. }
  89. }
  90. fmt.Println("Please enter the package name:")
  91. pkgName = Helper.Input()
  92. if pkgName == "" {
  93. return errors.New("Invalid package name")
  94. }
  95. fmt.Println("Please enter the package version:")
  96. pkgVersion = Helper.Input()
  97. if pkgVersion == "" {
  98. return errors.New("Invalid package name")
  99. }
  100. fmt.Printf("Package Name: %s\n", pkgName)
  101. fmt.Printf("Package Version: %s\n", pkgVersion)
  102. fmt.Println("Files to be added")
  103. Filesystem.PrintFiles(pkgFiles, Color.Green, false)
  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. e = writeManifestFile("/tmp/", pkgName, pkgVersion)
  110. if e != nil {
  111. return e
  112. }
  113. e = editManifestFile(filepath.Join("/tmp/", "manifest.yml"))
  114. if e != nil {
  115. fmt.Println(Color.Fatal(e))
  116. }
  117. // TODO: Write this file to a better spot?
  118. pkgFiles = append(pkgFiles, "/tmp/manifest.yml")
  119. e = Archive.CreateArchive(pkgFiles, pkgNameVersion+".tar.gz")
  120. if e != nil {
  121. return e
  122. }
  123. fmt.Printf(
  124. Color.Green("\nSuccessfully created package %s\n"),
  125. pkgNameVersion,
  126. )
  127. return nil
  128. }