pluginctl/manifest.go
Felipe Martin 6403d1a51d
Add manifest apply command to generate server and webapp manifest files
🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-07-28 18:00:41 +02:00

147 lines
4 KiB
Go

package pluginctl
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"text/template"
"github.com/mattermost/mattermost/server/public/model"
)
const (
// File permissions for generated directories and files.
manifestDirPerm = 0o750
manifestFilePerm = 0o600
)
const pluginIDGoFileTemplate = `// This file is automatically generated. Do not modify it manually.
package main
import (
"encoding/json"
"strings"
"github.com/mattermost/mattermost/server/public/model"
)
var manifest *model.Manifest
const manifestStr = ` + "`%s`" + `
func init() {
_ = json.NewDecoder(strings.NewReader(manifestStr)).Decode(&manifest)
}
`
const pluginIDJSFileTemplate = `// This file is automatically generated. Do not modify it manually.
const manifest = JSON.parse(` + "`%s`" + `);
export default manifest;
`
// applyManifest generates manifest files for server and webapp components.
func applyManifest(manifest *model.Manifest, pluginPath string) error {
manifestBytes, err := json.Marshal(manifest)
if err != nil {
return fmt.Errorf("failed to marshal manifest: %w", err)
}
manifestStr := string(manifestBytes)
// Generate server manifest file if server exists
if HasServerCode(manifest) {
serverDir := filepath.Join(pluginPath, "server")
if err := os.MkdirAll(serverDir, manifestDirPerm); err != nil {
return fmt.Errorf("failed to create server directory: %w", err)
}
serverManifestPath := filepath.Join(serverDir, "manifest.go")
serverContent := fmt.Sprintf(pluginIDGoFileTemplate, manifestStr)
if err := os.WriteFile(serverManifestPath, []byte(serverContent), manifestFilePerm); err != nil {
return fmt.Errorf("failed to write server manifest: %w", err)
}
Logger.Info("Generated server manifest", "path", serverManifestPath)
}
// Generate webapp manifest file if webapp exists
if HasWebappCode(manifest) {
webappDir := filepath.Join(pluginPath, "webapp", "src")
if err := os.MkdirAll(webappDir, manifestDirPerm); err != nil {
return fmt.Errorf("failed to create webapp directory: %w", err)
}
webappManifestPath := filepath.Join(webappDir, "manifest.ts")
webappContent := fmt.Sprintf(pluginIDJSFileTemplate, manifestStr)
if err := os.WriteFile(webappManifestPath, []byte(webappContent), manifestFilePerm); err != nil {
return fmt.Errorf("failed to write webapp manifest: %w", err)
}
Logger.Info("Generated webapp manifest", "path", webappManifestPath)
}
return nil
}
// RunManifestCommand implements the 'manifest' command functionality with subcommands.
func RunManifestCommand(args []string, pluginPath string) error {
if len(args) == 0 {
return fmt.Errorf("manifest command requires a subcommand: get {{.field_name}}, apply, check")
}
// Convert to absolute path
absPath, err := filepath.Abs(pluginPath)
if err != nil {
return fmt.Errorf("failed to resolve path: %w", err)
}
// Load plugin manifest
manifest, err := LoadPluginManifestFromPath(absPath)
if err != nil {
return fmt.Errorf("failed to load plugin manifest: %w", err)
}
subcommand := args[0]
switch subcommand {
case "get":
if len(args) < 2 {
return fmt.Errorf("get subcommand requires a template expression (e.g., {{.id}}, {{.version}})")
}
templateStr := args[1]
// Parse and execute template with manifest as context
tmpl, err := template.New("manifest").Parse(templateStr)
if err != nil {
return fmt.Errorf("failed to parse template: %w", err)
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, manifest); err != nil {
return fmt.Errorf("failed to execute template: %w", err)
}
fmt.Print(buf.String())
case "apply":
if err := applyManifest(manifest, absPath); err != nil {
return fmt.Errorf("failed to apply manifest: %w", err)
}
case "check":
if err := manifest.IsValid(); err != nil {
Logger.Error("Plugin manifest validation failed", "error", err)
return err
}
Logger.Info("Plugin manifest is valid")
default:
return fmt.Errorf("unknown subcommand: %s. Available subcommands: get {{.field_name}}, apply, check",
subcommand)
}
return nil
}