init.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package bundle
  2. import (
  3. _ "embed"
  4. "errors"
  5. "os"
  6. "path"
  7. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/bundle_entities"
  8. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/manifest_entities"
  9. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  10. "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
  11. tea "github.com/charmbracelet/bubbletea"
  12. )
  13. //go:embed templates/icon.svg
  14. var BUNDLE_ICON []byte
  15. func generateNewBundle() (*bundle_entities.Bundle, error) {
  16. m := newProfile()
  17. p := tea.NewProgram(m)
  18. if result, err := p.Run(); err != nil {
  19. return nil, err
  20. } else {
  21. if _, ok := result.(profile); ok {
  22. author := m.inputs[1].Value()
  23. name := m.inputs[0].Value()
  24. description := m.inputs[2].Value()
  25. bundle := &bundle_entities.Bundle{
  26. Name: name,
  27. Icon: "icon.svg",
  28. Labels: plugin_entities.NewI18nObject(name),
  29. Description: plugin_entities.NewI18nObject(description),
  30. Version: "0.0.1",
  31. Author: author,
  32. Type: manifest_entities.BundleType,
  33. Dependencies: []bundle_entities.Dependency{},
  34. }
  35. return bundle, nil
  36. } else {
  37. return nil, errors.New("invalid profile")
  38. }
  39. }
  40. }
  41. func InitBundle() {
  42. bundle, err := generateNewBundle()
  43. if err != nil {
  44. log.Error("Failed to generate new bundle: %v", err)
  45. return
  46. }
  47. // create bundle directory
  48. cwd, err := os.Getwd()
  49. if err != nil {
  50. log.Error("Error getting current directory: %v", err)
  51. return
  52. }
  53. bundleDir := path.Join(cwd, bundle.Name)
  54. if err := os.MkdirAll(bundleDir, 0755); err != nil {
  55. log.Error("Error creating bundle directory: %v", err)
  56. return
  57. }
  58. success := false
  59. defer func() {
  60. if !success {
  61. os.RemoveAll(bundleDir)
  62. }
  63. }()
  64. // save
  65. bundleYaml := marshalYamlBytes(bundle)
  66. if err := os.WriteFile(path.Join(bundleDir, "manifest.yaml"), bundleYaml, 0644); err != nil {
  67. log.Error("Error saving manifest.yaml: %v", err)
  68. return
  69. }
  70. // create _assets directory
  71. if err := os.MkdirAll(path.Join(bundleDir, "_assets"), 0755); err != nil {
  72. log.Error("Error creating _assets directory: %v", err)
  73. return
  74. }
  75. // create _assets/icon.svg
  76. if err := os.WriteFile(path.Join(bundleDir, "_assets", "icon.svg"), BUNDLE_ICON, 0644); err != nil {
  77. log.Error("Error saving icon.svg: %v", err)
  78. return
  79. }
  80. success = true
  81. log.Info("Bundle created successfully: %s", bundleDir)
  82. }