bundle.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package main
  2. import (
  3. "fmt"
  4. "path/filepath"
  5. "strconv"
  6. "github.com/langgenius/dify-plugin-daemon/cmd/commandline/bundle"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/bundle_entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  9. "github.com/spf13/cobra"
  10. )
  11. var (
  12. bundleCreateCommand = &cobra.Command{
  13. Use: "init",
  14. Short: "Create a bundle",
  15. Long: "Create a bundle",
  16. Run: func(c *cobra.Command, args []string) {
  17. bundle.InitBundle()
  18. },
  19. }
  20. bundleAnalyzeCommand = &cobra.Command{
  21. Use: "analyze [bundle_path]",
  22. Short: "List all dependencies",
  23. Long: "List all dependencies in the specified bundle",
  24. Args: cobra.ExactArgs(1),
  25. Run: func(c *cobra.Command, args []string) {
  26. bundlePath := args[0]
  27. bundle.ListDependencies(bundlePath)
  28. },
  29. }
  30. bundleAppendDependencyCommand = &cobra.Command{
  31. Use: "append",
  32. Short: "Append a dependency",
  33. Long: "Append a dependency",
  34. }
  35. bundleAppendGithubDependencyCommand = &cobra.Command{
  36. Use: "github [bundle_path]",
  37. Short: "Append a github dependency",
  38. Long: "Append a github dependency to the specified bundle",
  39. Args: cobra.ExactArgs(1),
  40. Run: func(c *cobra.Command, args []string) {
  41. bundlePath := args[0]
  42. repoPattern := c.Flag("repo_pattern").Value.String()
  43. githubPattern, err := bundle_entities.NewGithubRepoPattern(repoPattern)
  44. if err != nil {
  45. log.Error("Invalid github repo pattern: %v", err)
  46. return
  47. }
  48. bundle.AddGithubDependency(bundlePath, githubPattern)
  49. },
  50. }
  51. bundleAppendMarketplaceDependencyCommand = &cobra.Command{
  52. Use: "marketplace [bundle_path]",
  53. Short: "Append a marketplace dependency",
  54. Long: "Append a marketplace dependency to the specified bundle",
  55. Args: cobra.ExactArgs(1),
  56. Run: func(c *cobra.Command, args []string) {
  57. bundlePath := args[0]
  58. marketplacePatternString := c.Flag("marketplace_pattern").Value.String()
  59. marketplacePattern, err := bundle_entities.NewMarketplacePattern(marketplacePatternString)
  60. if err != nil {
  61. log.Error("Invalid marketplace pattern: %v", err)
  62. return
  63. }
  64. bundle.AddMarketplaceDependency(bundlePath, marketplacePattern)
  65. },
  66. }
  67. bundleAppendPackageDependencyCommand = &cobra.Command{
  68. Use: "package [bundle_path]",
  69. Short: "Append a local package dependency",
  70. Long: "Append a local package dependency to the specified bundle",
  71. Args: cobra.ExactArgs(1),
  72. Run: func(c *cobra.Command, args []string) {
  73. bundlePath := args[0]
  74. packagePath := c.Flag("package_path").Value.String()
  75. bundle.AddPackageDependency(bundlePath, packagePath)
  76. },
  77. }
  78. bundleRegenerateCommand = &cobra.Command{
  79. Use: "regenerate [bundle_path]",
  80. Short: "Regenerate the bundle",
  81. Long: "Regenerate the specified bundle",
  82. Args: cobra.ExactArgs(1),
  83. Run: func(c *cobra.Command, args []string) {
  84. bundlePath := args[0]
  85. bundle.RegenerateBundle(bundlePath)
  86. },
  87. }
  88. bundleRemoveDependencyCommand = &cobra.Command{
  89. Use: "remove [bundle_path]",
  90. Short: "Remove a dependency",
  91. Long: "Remove a dependency from the specified bundle",
  92. Args: cobra.ExactArgs(1),
  93. Run: func(c *cobra.Command, args []string) {
  94. bundlePath := args[0]
  95. index := c.Flag("index").Value.String()
  96. indexInt, err := strconv.Atoi(index)
  97. if err != nil {
  98. log.Error("Invalid index: %v", err)
  99. return
  100. }
  101. bundle.RemoveDependency(bundlePath, indexInt)
  102. },
  103. }
  104. bundleBumpVersionCommand = &cobra.Command{
  105. Use: "bump [bundle_path]",
  106. Short: "Bump the version of the bundle",
  107. Long: "Bump the version of the specified bundle",
  108. Args: cobra.ExactArgs(1),
  109. Run: func(c *cobra.Command, args []string) {
  110. bundlePath := args[0]
  111. targetVersion := c.Flag("target_version").Value.String()
  112. bundle.BumpVersion(bundlePath, targetVersion)
  113. },
  114. }
  115. bundlePackageCommand = &cobra.Command{
  116. Use: "package [bundle_path]",
  117. Short: "Package the bundle",
  118. Long: "Package the specified bundle",
  119. Args: cobra.ExactArgs(1),
  120. Run: func(c *cobra.Command, args []string) {
  121. bundlePath := args[0]
  122. // using filename of input_path as output_path if not specified
  123. outputPath := ""
  124. if c.Flag("output_path").Value.String() != "" {
  125. outputPath = c.Flag("output_path").Value.String()
  126. } else {
  127. base := filepath.Base(bundlePath)
  128. if base == "." || base == "/" {
  129. fmt.Println("Error: invalid input path, you should specify the path outside of bundle directory")
  130. return
  131. }
  132. outputPath = base + ".difybndl"
  133. }
  134. bundle.PackageBundle(bundlePath, outputPath)
  135. },
  136. }
  137. )
  138. func init() {
  139. bundleCommand.AddCommand(bundleCreateCommand)
  140. bundleCommand.AddCommand(bundleAppendDependencyCommand)
  141. bundleAppendDependencyCommand.AddCommand(bundleAppendGithubDependencyCommand)
  142. bundleAppendDependencyCommand.AddCommand(bundleAppendMarketplaceDependencyCommand)
  143. bundleAppendDependencyCommand.AddCommand(bundleAppendPackageDependencyCommand)
  144. bundleCommand.AddCommand(bundleRemoveDependencyCommand)
  145. bundleCommand.AddCommand(bundleRegenerateCommand)
  146. bundleCommand.AddCommand(bundleBumpVersionCommand)
  147. bundleCommand.AddCommand(bundlePackageCommand)
  148. bundleCommand.AddCommand(bundleAnalyzeCommand)
  149. bundleAppendGithubDependencyCommand.Flags().StringP("repo_pattern", "r", "", "github repo pattern")
  150. bundleAppendGithubDependencyCommand.MarkFlagRequired("repo_pattern")
  151. bundleAppendMarketplaceDependencyCommand.Flags().StringP("marketplace_pattern", "m", "", "marketplace pattern")
  152. bundleAppendMarketplaceDependencyCommand.MarkFlagRequired("marketplace_pattern")
  153. bundleAppendPackageDependencyCommand.Flags().StringP("package_path", "p", "", "path to the package")
  154. bundleAppendPackageDependencyCommand.MarkFlagRequired("package_path")
  155. bundleRemoveDependencyCommand.Flags().StringP("index", "i", "", "index of the dependency")
  156. bundleRemoveDependencyCommand.MarkFlagRequired("index")
  157. bundleBumpVersionCommand.Flags().StringP("target_version", "t", "", "target version")
  158. bundleBumpVersionCommand.MarkFlagRequired("target_version")
  159. bundlePackageCommand.Flags().StringP("output_path", "o", "", "output path")
  160. }