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.

161 lines
3.4 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. err error
  58. )
  59. fmt.Println("Initialising package creation...")
  60. err = 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. if err != nil {
  69. return err
  70. }
  71. fmt.Println("Added files:")
  72. Filesystem.PrintFiles(pickedFiles, Color.Green, true)
  73. fmt.Println("Please select the files you would like to use to create the package. Leave empty for all.")
  74. choices = Helper.Input()
  75. if choices == "" {
  76. for _, pkg = range pickedFiles {
  77. pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, pkg))
  78. }
  79. } else {
  80. choicesSplit = strings.Split(choices, ",")
  81. for _, i := range choicesSplit {
  82. index, err = strconv.Atoi(i)
  83. if err != nil {
  84. return err
  85. }
  86. if len(pickedFiles) < index {
  87. return errors.New("invalid choice")
  88. }
  89. pkgFiles = append(pkgFiles, filepath.Join(Variables.RootDir, pickedFiles[index]))
  90. }
  91. }
  92. fmt.Println("Please enter the package name:")
  93. pkgName = Helper.Input()
  94. if pkgName == "" {
  95. return errors.New("invalid package name")
  96. }
  97. fmt.Println("Please enter the package version:")
  98. pkgVersion = Helper.Input()
  99. if pkgVersion == "" {
  100. return errors.New("invalid package name")
  101. }
  102. fmt.Printf("Package Name: %s\n", pkgName)
  103. fmt.Printf("Package Version: %s\n", pkgVersion)
  104. fmt.Println("Files to be added")
  105. Filesystem.PrintFiles(pkgFiles, Color.Green, false)
  106. // TODO: Move this check to below manifest file
  107. fmt.Println("Is this correct? [y/N]")
  108. if strings.ToLower(Helper.Input()) != "y" {
  109. return errors.New("user aborted")
  110. }
  111. pkgNameVersion = fmt.Sprintf("%s-%s", pkgName, pkgVersion)
  112. err = writeManifestFile("/tmp/", pkgName, pkgVersion)
  113. if err != nil {
  114. return err
  115. }
  116. err = editManifestFile(filepath.Join("/tmp/", "manifest.yml"))
  117. if err != nil {
  118. fmt.Println(Color.Fatal(err))
  119. }
  120. // TODO: Write this file to a better spot?
  121. pkgFiles = append(pkgFiles, "/tmp/manifest.yml")
  122. err = Archive.CreateArchive(pkgFiles, pkgNameVersion+".tar.gz")
  123. if err != nil {
  124. return err
  125. }
  126. fmt.Printf(
  127. Color.Green("\nSuccessfully created package %s\n"),
  128. pkgNameVersion,
  129. )
  130. return nil
  131. }