main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "github.com/mattermost/mattermost-server/v5/model"
  8. "github.com/pkg/errors"
  9. )
  10. const pluginIDGoFileTemplate = `// This file is automatically generated. Do not modify it manually.
  11. package main
  12. import (
  13. "strings"
  14. "github.com/mattermost/mattermost-server/v5/model"
  15. )
  16. var manifest *model.Manifest
  17. const manifestStr = ` + "`" + `
  18. %s
  19. ` + "`" + `
  20. func init() {
  21. manifest = model.ManifestFromJson(strings.NewReader(manifestStr))
  22. }
  23. `
  24. func main() {
  25. if len(os.Args) <= 1 {
  26. panic("no cmd specified")
  27. }
  28. manifest, err := findManifest()
  29. if err != nil {
  30. panic("failed to find manifest: " + err.Error())
  31. }
  32. cmd := os.Args[1]
  33. switch cmd {
  34. case "id":
  35. dumpPluginID(manifest)
  36. case "version":
  37. dumpPluginVersion(manifest)
  38. case "has_server":
  39. if manifest.HasServer() {
  40. fmt.Printf("true")
  41. }
  42. case "has_webapp":
  43. if manifest.HasWebapp() {
  44. fmt.Printf("true")
  45. }
  46. case "apply":
  47. if err := applyManifest(manifest); err != nil {
  48. panic("failed to apply manifest: " + err.Error())
  49. }
  50. default:
  51. panic("unrecognized command: " + cmd)
  52. }
  53. }
  54. func findManifest() (*model.Manifest, error) {
  55. _, manifestFilePath, err := model.FindManifest(".")
  56. if err != nil {
  57. return nil, errors.Wrap(err, "failed to find manifest in current working directory")
  58. }
  59. manifestFile, err := os.Open(manifestFilePath)
  60. if err != nil {
  61. return nil, errors.Wrapf(err, "failed to open %s", manifestFilePath)
  62. }
  63. defer manifestFile.Close()
  64. // Re-decode the manifest, disallowing unknown fields. When we write the manifest back out,
  65. // we don't want to accidentally clobber anything we won't preserve.
  66. var manifest model.Manifest
  67. decoder := json.NewDecoder(manifestFile)
  68. decoder.DisallowUnknownFields()
  69. if err = decoder.Decode(&manifest); err != nil {
  70. return nil, errors.Wrap(err, "failed to parse manifest")
  71. }
  72. return &manifest, nil
  73. }
  74. // dumpPluginId writes the plugin id from the given manifest to standard out
  75. func dumpPluginID(manifest *model.Manifest) {
  76. fmt.Printf("%s", manifest.Id)
  77. }
  78. // dumpPluginVersion writes the plugin version from the given manifest to standard out
  79. func dumpPluginVersion(manifest *model.Manifest) {
  80. fmt.Printf("%s", manifest.Version)
  81. }
  82. // applyManifest propagates the plugin_id into the server and webapp folders, as necessary
  83. func applyManifest(manifest *model.Manifest) error {
  84. if manifest.HasServer() {
  85. // generate JSON representation of Manifest.
  86. manifestBytes, err := json.MarshalIndent(manifest, "", " ")
  87. if err != nil {
  88. return err
  89. }
  90. manifestStr := string(manifestBytes)
  91. // write generated code to file by using Go file template.
  92. if err := ioutil.WriteFile(
  93. "server/manifest.go",
  94. []byte(fmt.Sprintf(pluginIDGoFileTemplate, manifestStr)),
  95. 0600,
  96. ); err != nil {
  97. return errors.Wrap(err, "failed to write server/manifest.go")
  98. }
  99. }
  100. return nil
  101. }