diff --git a/.gitignore b/.gitignore index 8dc59ef..028d8fd 100644 --- a/.gitignore +++ b/.gitignore @@ -31,8 +31,3 @@ Thumbs.db .env .env.local .claude - -# Ignore all files in testdata except plugin.json -testdata/**/* -!testdata/**/ -!testdata/**/plugin.json diff --git a/assets/.golangci.yml b/assets/.golangci.yml index 59726f7..5682ada 100644 --- a/assets/.golangci.yml +++ b/assets/.golangci.yml @@ -6,7 +6,7 @@ linters-settings: gofmt: simplify: true goimports: - local-prefixes: {{.GoModule}} + local-prefixes: github.com/mattermost/mattermost-starter-template govet: check-shadowing: true enable-all: true diff --git a/assets/build/utils.mk b/assets/build/utils.mk index eb44805..3dd5c52 100644 --- a/assets/build/utils.mk +++ b/assets/build/utils.mk @@ -33,7 +33,7 @@ endif mock: ifneq ($(HAS_SERVER),) go install github.com/golang/mock/mockgen@v1.6.0 - mockgen -destination=server/command/mocks/mock_commands.go -package=mocks {{.GoModule}}/server/command Command + mockgen -destination=server/command/mocks/mock_commands.go -package=mocks github.com/mattermost/mattermost-plugin-starter-template/server/command Command endif ## Show help documentation. diff --git a/info.go b/info.go index fb1e3b8..24a12df 100644 --- a/info.go +++ b/info.go @@ -28,28 +28,28 @@ func PrintPluginInfo(manifest *model.Manifest) error { // printBasicInfo prints basic plugin information. func printBasicInfo(manifest *model.Manifest) { - fmt.Println("Plugin Information:") - fmt.Println("==================") + Logger.Info("Plugin Information:") + Logger.Info("==================") - fmt.Printf("ID: %s\n", manifest.Id) - fmt.Printf("Name: %s\n", manifest.Name) - fmt.Printf("Version: %s\n", manifest.Version) + Logger.Info("ID:", "value", manifest.Id) + Logger.Info("Name:", "value", manifest.Name) + Logger.Info("Version:", "value", manifest.Version) minVersion := manifest.MinServerVersion if minVersion == "" { minVersion = "Not specified" } - fmt.Printf("Min MM Version: %s\n", minVersion) + Logger.Info("Min MM Version:", "value", minVersion) if manifest.Description != "" { - fmt.Printf("Description: %s\n", manifest.Description) + Logger.Info("Description:", "value", manifest.Description) } } // printCodeComponents prints information about server and webapp code. func printCodeComponents(manifest *model.Manifest) { - fmt.Println("Code Components:") - fmt.Println("================") + Logger.Info("Code Components:") + Logger.Info("================") printServerCodeInfo(manifest) printWebappCodeInfo(manifest) @@ -58,33 +58,28 @@ func printCodeComponents(manifest *model.Manifest) { // printServerCodeInfo prints server code information. func printServerCodeInfo(manifest *model.Manifest) { if HasServerCode(manifest) { - fmt.Println("Server Code: Yes") + Logger.Info("Server Code:", "value", "Yes") if manifest.Server != nil && len(manifest.Server.Executables) > 0 { - fmt.Print("Executables: ") - first := true + var executables []string for platform := range manifest.Server.Executables { - if !first { - fmt.Print(", ") - } - fmt.Print(platform) - first = false + executables = append(executables, platform) } - fmt.Println() + Logger.Info("Executables:", "platforms", executables) } } else { - fmt.Println("Server Code: No") + Logger.Info("Server Code:", "value", "No") } } // printWebappCodeInfo prints webapp code information. func printWebappCodeInfo(manifest *model.Manifest) { if HasWebappCode(manifest) { - fmt.Println("Webapp Code: Yes") + Logger.Info("Webapp Code:", "value", "Yes") if manifest.Webapp != nil && manifest.Webapp.BundlePath != "" { - fmt.Printf("Bundle Path: %s\n", manifest.Webapp.BundlePath) + Logger.Info("Bundle Path:", "value", manifest.Webapp.BundlePath) } } else { - fmt.Println("Webapp Code: No") + Logger.Info("Webapp Code:", "value", "No") } } @@ -94,7 +89,7 @@ func printSettingsSchema(manifest *model.Manifest) { if manifest.SettingsSchema != nil { value = "Yes" } - fmt.Printf("Settings Schema: %s\n", value) + Logger.Info("Settings Schema:", "value", value) } // InfoCommandWithPath implements the 'info' command with a custom path. diff --git a/updateassets.go b/updateassets.go index 705c1db..60196cd 100644 --- a/updateassets.go +++ b/updateassets.go @@ -8,9 +8,6 @@ import ( "os" "path/filepath" "strings" - "text/template" - - "github.com/mattermost/mattermost/server/public/model" ) //go:embed assets/* @@ -48,7 +45,6 @@ func RunUpdateAssetsCommand(args []string, pluginPath string) error { hasWebapp: hasWebapp, updatedCount: &updatedCount, pluginCtlConfig: pluginCtlConfig, - manifest: manifest, } err = fs.WalkDir(assetsFS, "assets", func(path string, d fs.DirEntry, err error) error { @@ -125,19 +121,6 @@ type AssetProcessorConfig struct { hasWebapp bool updatedCount *int pluginCtlConfig *PluginCtlConfig - manifest *model.Manifest -} - -// GoModule represents information from go.mod file. -type GoModule struct { - Module string - Version string -} - -// TemplateContext holds the data available to templates. -type TemplateContext struct { - Manifest *model.Manifest - GoModule *GoModule } func processAssetEntry(path string, d fs.DirEntry, err error, config AssetProcessorConfig) error { @@ -168,21 +151,21 @@ func processAssetEntry(path string, d fs.DirEntry, err error, config AssetProces return createDirectory(targetPath) } - return processAssetFile(path, targetPath, relativePath, config) + return processAssetFile(path, targetPath, relativePath, config.updatedCount) } -func processAssetFile(embeddedPath, targetPath, relativePath string, config AssetProcessorConfig) error { - shouldUpdate, err := shouldUpdateFile(embeddedPath, targetPath, config) +func processAssetFile(embeddedPath, targetPath, relativePath string, updatedCount *int) error { + shouldUpdate, err := shouldUpdateFile(embeddedPath, targetPath) if err != nil { return err } if shouldUpdate { - err = updateFile(embeddedPath, targetPath, relativePath, config) + err = updateFile(embeddedPath, targetPath, relativePath) if err != nil { return err } - (*config.updatedCount)++ + (*updatedCount)++ } return nil @@ -196,11 +179,10 @@ func createDirectory(targetPath string) error { return nil } -func shouldUpdateFile(embeddedPath, targetPath string, config AssetProcessorConfig) (bool, error) { - // Process the template to get the final content - processedContent, err := processTemplate(embeddedPath, config.manifest, config.pluginPath) +func shouldUpdateFile(embeddedPath, targetPath string) (bool, error) { + content, err := assetsFS.ReadFile(embeddedPath) if err != nil { - return false, fmt.Errorf("failed to process template %s: %w", embeddedPath, err) + return false, fmt.Errorf("failed to read embedded file %s: %w", embeddedPath, err) } existingContent, err := os.ReadFile(targetPath) @@ -209,14 +191,13 @@ func shouldUpdateFile(embeddedPath, targetPath string, config AssetProcessorConf return true, nil //nolint:nilerr } - return !bytes.Equal(existingContent, processedContent), nil + return !bytes.Equal(existingContent, content), nil } -func updateFile(embeddedPath, targetPath, relativePath string, config AssetProcessorConfig) error { - // Process the template to get the final content - processedContent, err := processTemplate(embeddedPath, config.manifest, config.pluginPath) +func updateFile(embeddedPath, targetPath, relativePath string) error { + content, err := assetsFS.ReadFile(embeddedPath) if err != nil { - return fmt.Errorf("failed to process template %s: %w", embeddedPath, err) + return fmt.Errorf("failed to read embedded file %s: %w", embeddedPath, err) } parentDir := filepath.Dir(targetPath) @@ -224,7 +205,7 @@ func updateFile(embeddedPath, targetPath, relativePath string, config AssetProce return fmt.Errorf("failed to create parent directory %s: %w", parentDir, err) } - if err := os.WriteFile(targetPath, processedContent, filePermissions); err != nil { + if err := os.WriteFile(targetPath, content, filePermissions); err != nil { return fmt.Errorf("failed to write file %s: %w", targetPath, err) } @@ -232,73 +213,3 @@ func updateFile(embeddedPath, targetPath, relativePath string, config AssetProce return nil } - -// processTemplate processes a template file with the manifest context. -func processTemplate(embeddedPath string, manifest *model.Manifest, pluginPath string) ([]byte, error) { - // Read the template content - templateContent, err := assetsFS.ReadFile(embeddedPath) - if err != nil { - return nil, fmt.Errorf("failed to read embedded file %s: %w", embeddedPath, err) - } - - // Parse go.mod file to get module information - goMod, err := parseGoModule(pluginPath) - if err != nil { - return nil, fmt.Errorf("failed to parse go.mod file: %w", err) - } - - // Create template context - context := TemplateContext{ - Manifest: manifest, - GoModule: goMod, - } - - // Create and parse the template - tmpl, err := template.New(embeddedPath).Parse(string(templateContent)) - if err != nil { - return nil, fmt.Errorf("failed to parse template %s: %w", embeddedPath, err) - } - - // Execute the template - var buf bytes.Buffer - err = tmpl.Execute(&buf, context) - if err != nil { - return nil, fmt.Errorf("failed to execute template %s: %w", embeddedPath, err) - } - - return buf.Bytes(), nil -} - -// parseGoModule parses the go.mod file to extract module information. -func parseGoModule(pluginPath string) (*GoModule, error) { - goModPath := filepath.Join(pluginPath, "go.mod") - - content, err := os.ReadFile(goModPath) - if err != nil { - // If go.mod doesn't exist, return nil without error - if os.IsNotExist(err) { - return nil, nil - } - - return nil, fmt.Errorf("failed to read go.mod file: %w", err) - } - - goMod := &GoModule{} - lines := strings.Split(string(content), "\n") - - for _, line := range lines { - line = strings.TrimSpace(line) - - // Parse module line - if strings.HasPrefix(line, "module ") { - goMod.Module = strings.TrimSpace(strings.TrimPrefix(line, "module ")) - } - - // Parse go version line - if strings.HasPrefix(line, "go ") { - goMod.Version = strings.TrimSpace(strings.TrimPrefix(line, "go ")) - } - } - - return goMod, nil -}