- Add logger.go with public Logger variable and InitLogger function - Replace all fmt.Printf/fmt.Fprintf calls with structured Logger.Info/Logger.Error - Initialize logger in main function for consistent access across packages - Keep fmt.Errorf for proper error creation (standard Go practice) - Add tint dependency for colorized terminal output with timestamps - Convert user output to structured logging with key-value pairs - Update info command to use structured logging for plugin details - Update updateassets command to use structured progress logging - Update version command to use structured logging - Update authentication logging in client.go with structured fields - Update enable/disable commands to use structured logging - Remove unused fmt imports after conversion All output now uses slog with tint for beautiful, structured, colorized logging while maintaining proper error handling with fmt.Errorf for error creation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
98 lines
2.5 KiB
Go
98 lines
2.5 KiB
Go
package pluginctl
|
|
|
|
import (
|
|
"bytes"
|
|
"embed"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
//go:embed assets/**/*
|
|
var assetsFS embed.FS
|
|
|
|
func RunUpdateAssetsCommand(args []string, pluginPath string) error {
|
|
if len(args) > 0 {
|
|
return fmt.Errorf("updateassets command does not accept arguments")
|
|
}
|
|
|
|
Logger.Info("Updating assets in plugin directory", "path", pluginPath)
|
|
|
|
// Load plugin manifest to check for webapp code
|
|
manifest, err := LoadPluginManifestFromPath(pluginPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to load plugin manifest: %w", err)
|
|
}
|
|
|
|
// Check if the plugin has webapp code according to manifest
|
|
hasWebapp := HasWebappCode(manifest)
|
|
|
|
// Counter for updated files
|
|
var updatedCount int
|
|
|
|
// Walk through the embedded assets
|
|
err = fs.WalkDir(assetsFS, "assets", func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Skip the root assets directory
|
|
if path == "assets" {
|
|
return nil
|
|
}
|
|
|
|
// Remove the "assets/" prefix to get the relative path
|
|
relativePath := path[7:] // len("assets/") = 7
|
|
|
|
// Skip webapp assets if plugin doesn't have webapp code
|
|
if !hasWebapp && strings.HasPrefix(relativePath, "webapp") {
|
|
return nil
|
|
}
|
|
|
|
targetPath := filepath.Join(pluginPath, relativePath)
|
|
|
|
if d.IsDir() {
|
|
// Create directory if it doesn't exist
|
|
if err := os.MkdirAll(targetPath, 0755); err != nil {
|
|
return fmt.Errorf("failed to create directory %s: %w", targetPath, err)
|
|
}
|
|
} else {
|
|
// Read file content from embedded FS
|
|
content, err := assetsFS.ReadFile(path)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read embedded file %s: %w", path, err)
|
|
}
|
|
|
|
// Check if target file exists and compare content
|
|
existingContent, err := os.ReadFile(targetPath)
|
|
if err == nil && bytes.Equal(existingContent, content) {
|
|
// File exists and content is identical, skip update
|
|
return nil
|
|
}
|
|
|
|
// Create parent directory if it doesn't exist
|
|
parentDir := filepath.Dir(targetPath)
|
|
if err := os.MkdirAll(parentDir, 0755); err != nil {
|
|
return fmt.Errorf("failed to create parent directory %s: %w", parentDir, err)
|
|
}
|
|
|
|
// Write file to target location
|
|
if err := os.WriteFile(targetPath, content, 0644); err != nil {
|
|
return fmt.Errorf("failed to write file %s: %w", targetPath, err)
|
|
}
|
|
Logger.Info("Updated file", "path", relativePath)
|
|
updatedCount++
|
|
}
|
|
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to update assets: %w", err)
|
|
}
|
|
|
|
Logger.Info("Assets updated successfully!", "files_updated", updatedCount)
|
|
return nil
|
|
}
|