|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/mattermost/mattermost-server/v6/model" |
| 9 | + "github.com/pkg/errors" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + if len(os.Args) <= 1 { |
| 14 | + panic("no cmd specified") |
| 15 | + } |
| 16 | + |
| 17 | + manifest, err := findManifest() |
| 18 | + if err != nil { |
| 19 | + panic("failed to find manifest: " + err.Error()) |
| 20 | + } |
| 21 | + |
| 22 | + cmd := os.Args[1] |
| 23 | + switch cmd { |
| 24 | + case "id": |
| 25 | + dumpPluginID(manifest) |
| 26 | + |
| 27 | + case "version": |
| 28 | + dumpPluginVersion(manifest) |
| 29 | + |
| 30 | + case "has_server": |
| 31 | + if manifest.HasServer() { |
| 32 | + fmt.Printf("true") |
| 33 | + } |
| 34 | + |
| 35 | + case "has_webapp": |
| 36 | + if manifest.HasWebapp() { |
| 37 | + fmt.Printf("true") |
| 38 | + } |
| 39 | + |
| 40 | + default: |
| 41 | + panic("unrecognized command: " + cmd) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func findManifest() (*model.Manifest, error) { |
| 46 | + _, manifestFilePath, err := model.FindManifest(".") |
| 47 | + if err != nil { |
| 48 | + return nil, errors.Wrap(err, "failed to find manifest in current working directory") |
| 49 | + } |
| 50 | + manifestFile, err := os.Open(manifestFilePath) |
| 51 | + if err != nil { |
| 52 | + return nil, errors.Wrapf(err, "failed to open %s", manifestFilePath) |
| 53 | + } |
| 54 | + defer manifestFile.Close() |
| 55 | + |
| 56 | + // Re-decode the manifest, disallowing unknown fields. When we write the manifest back out, |
| 57 | + // we don't want to accidentally clobber anything we won't preserve. |
| 58 | + var manifest model.Manifest |
| 59 | + decoder := json.NewDecoder(manifestFile) |
| 60 | + decoder.DisallowUnknownFields() |
| 61 | + if err = decoder.Decode(&manifest); err != nil { |
| 62 | + return nil, errors.Wrap(err, "failed to parse manifest") |
| 63 | + } |
| 64 | + |
| 65 | + return &manifest, nil |
| 66 | +} |
| 67 | + |
| 68 | +// dumpPluginId writes the plugin id from the given manifest to standard out |
| 69 | +func dumpPluginID(manifest *model.Manifest) { |
| 70 | + fmt.Printf("%s", manifest.Id) |
| 71 | +} |
| 72 | + |
| 73 | +// dumpPluginVersion writes the plugin version from the given manifest to standard out |
| 74 | +func dumpPluginVersion(manifest *model.Manifest) { |
| 75 | + fmt.Printf("%s", manifest.Version) |
| 76 | +} |
0 commit comments