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.

143 lines
2.7 KiB

  1. package Package
  2. import (
  3. "errors"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "PackageManager/Archive"
  11. "PackageManager/Client/Filesystem"
  12. "PackageManager/Color"
  13. "PackageManager/Helper"
  14. )
  15. func CreatePackage() error {
  16. var (
  17. dirtyFiles map[int]string
  18. newFiles map[int]string
  19. pkgFiles map[int]string = make(map[int]string)
  20. choices string
  21. choicesSplit []string
  22. filePath string
  23. pkgName string
  24. pkgVersion string
  25. pkgNameVersion string
  26. tmpDir string
  27. index int
  28. ok bool
  29. e error
  30. )
  31. fmt.Println("Initialising package creation...")
  32. dirtyFiles, newFiles, e = Filesystem.GetFilesystemDiff()
  33. if e != nil {
  34. return e
  35. }
  36. fmt.Println("\nModified files...")
  37. for i, file := range dirtyFiles {
  38. fmt.Printf(
  39. "\t%d - %s\n",
  40. i,
  41. Color.Red(file),
  42. )
  43. }
  44. fmt.Println("\nNew files...")
  45. for i, file := range newFiles {
  46. fmt.Printf(
  47. "\t%d - %s\n",
  48. i,
  49. Color.Red(file),
  50. )
  51. }
  52. fmt.Println("Please select the files you would like to use to create the package. Leave empty for all.")
  53. choices = Helper.Input()
  54. if choices == "" {
  55. for i, file := range dirtyFiles {
  56. pkgFiles[i] = file
  57. }
  58. for i, file := range newFiles {
  59. pkgFiles[i] = file
  60. }
  61. } else {
  62. choicesSplit = strings.Split(choices, ",")
  63. for _, i := range choicesSplit {
  64. index, e = strconv.Atoi(i)
  65. if e != nil {
  66. // TODO: Handle this error
  67. panic(e)
  68. }
  69. filePath, ok = dirtyFiles[index]
  70. if !ok {
  71. filePath, ok = dirtyFiles[index]
  72. if !ok {
  73. return errors.New("Invalid package selection")
  74. }
  75. }
  76. pkgFiles[index] = filePath
  77. }
  78. }
  79. fmt.Println("Please enter the package name:")
  80. pkgName = Helper.Input()
  81. if pkgName == "" {
  82. return errors.New("Invalid package name")
  83. }
  84. fmt.Println("Please enter the package version:")
  85. pkgVersion = Helper.Input()
  86. if pkgVersion == "" {
  87. return errors.New("Invalid package name")
  88. }
  89. fmt.Printf("Package Name: %s\n", pkgName)
  90. fmt.Printf("Package Version: %s\n", pkgVersion)
  91. fmt.Println("Files to be added")
  92. for i, file := range pkgFiles {
  93. fmt.Printf(
  94. "\t%d - %s\n",
  95. i,
  96. Color.Green(file),
  97. )
  98. }
  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 = Archive.TarGzip(tmpDir, pkgNameVersion+".tar.gz")
  113. if e != nil {
  114. return e
  115. }
  116. fmt.Printf(
  117. Color.Green("\nSuccessfully created package %s\n"),
  118. pkgNameVersion,
  119. )
  120. return nil
  121. }