Replace logging system with slog and tint for structured colored output
- 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>
This commit is contained in:
parent
b43e7ac3ec
commit
73149001eb
11 changed files with 80 additions and 46 deletions
2
Makefile
2
Makefile
|
@ -136,7 +136,7 @@ verify: clean lint test build ## Verify build (clean, lint, test, build)
|
||||||
|
|
||||||
# Quick development build
|
# Quick development build
|
||||||
.PHONY: dev
|
.PHONY: dev
|
||||||
dev: fmt lint build ## Quick development build (fmt, lint, build)
|
dev: fmt lint snapshot ## Quick development build (fmt, lint, build)
|
||||||
|
|
||||||
# Check changes target
|
# Check changes target
|
||||||
.PHONY: check-changes
|
.PHONY: check-changes
|
||||||
|
|
10
client.go
10
client.go
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
@ -22,14 +21,13 @@ func getClient(ctx context.Context) (*model.Client4, error) {
|
||||||
|
|
||||||
client, connected := getUnixClient(socketPath)
|
client, connected := getUnixClient(socketPath)
|
||||||
if connected {
|
if connected {
|
||||||
log.Printf("Connecting using local mode over %s", socketPath)
|
Logger.Info("Connecting using local mode", "socket_path", socketPath)
|
||||||
|
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if os.Getenv("MM_LOCALSOCKETPATH") != "" {
|
if os.Getenv("MM_LOCALSOCKETPATH") != "" {
|
||||||
log.Printf("No socket found at %s for local mode deployment. "+
|
Logger.Info("No socket found for local mode deployment. Attempting to authenticate with credentials.", "socket_path", socketPath)
|
||||||
"Attempting to authenticate with credentials.", socketPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
siteURL := os.Getenv("MM_SERVICESETTINGS_SITEURL")
|
siteURL := os.Getenv("MM_SERVICESETTINGS_SITEURL")
|
||||||
|
@ -44,7 +42,7 @@ func getClient(ctx context.Context) (*model.Client4, error) {
|
||||||
client = model.NewAPIv4Client(siteURL)
|
client = model.NewAPIv4Client(siteURL)
|
||||||
|
|
||||||
if adminToken != "" {
|
if adminToken != "" {
|
||||||
log.Printf("Authenticating using token against %s.", siteURL)
|
Logger.Info("Authenticating using token", "site_url", siteURL)
|
||||||
client.SetToken(adminToken)
|
client.SetToken(adminToken)
|
||||||
|
|
||||||
return client, nil
|
return client, nil
|
||||||
|
@ -52,7 +50,7 @@ func getClient(ctx context.Context) (*model.Client4, error) {
|
||||||
|
|
||||||
if adminUsername != "" && adminPassword != "" {
|
if adminUsername != "" && adminPassword != "" {
|
||||||
client := model.NewAPIv4Client(siteURL)
|
client := model.NewAPIv4Client(siteURL)
|
||||||
log.Printf("Authenticating as %s against %s.", adminUsername, siteURL)
|
Logger.Info("Authenticating with credentials", "username", adminUsername, "site_url", siteURL)
|
||||||
_, _, err := client.Login(ctx, adminUsername, adminPassword)
|
_, _, err := client.Login(ctx, adminUsername, adminPassword)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to login as %s: %w", adminUsername, err)
|
return nil, fmt.Errorf("failed to login as %s: %w", adminUsername, err)
|
||||||
|
|
|
@ -15,6 +15,9 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
// Initialize logger
|
||||||
|
pluginctl.InitLogger()
|
||||||
|
|
||||||
var pluginPath string
|
var pluginPath string
|
||||||
|
|
||||||
flag.StringVar(&pluginPath, "plugin-path", "", "Path to plugin directory (overrides PLUGINCTL_PLUGIN_PATH)")
|
flag.StringVar(&pluginPath, "plugin-path", "", "Path to plugin directory (overrides PLUGINCTL_PLUGIN_PATH)")
|
||||||
|
@ -22,7 +25,7 @@ func main() {
|
||||||
|
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
fmt.Fprintf(os.Stderr, "Error: No command specified\n\n")
|
pluginctl.Logger.Error("No command specified")
|
||||||
showUsage()
|
showUsage()
|
||||||
os.Exit(ExitError)
|
os.Exit(ExitError)
|
||||||
}
|
}
|
||||||
|
@ -34,7 +37,7 @@ func main() {
|
||||||
effectivePluginPath := pluginctl.GetEffectivePluginPath(pluginPath)
|
effectivePluginPath := pluginctl.GetEffectivePluginPath(pluginPath)
|
||||||
|
|
||||||
if err := runCommand(command, commandArgs, effectivePluginPath); err != nil {
|
if err := runCommand(command, commandArgs, effectivePluginPath); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
pluginctl.Logger.Error("Command failed", "error", err)
|
||||||
os.Exit(ExitError)
|
os.Exit(ExitError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,7 +71,7 @@ func runInfoCommand(args []string, pluginPath string) error {
|
||||||
|
|
||||||
func runVersionCommand(_ []string) error {
|
func runVersionCommand(_ []string) error {
|
||||||
version := pluginctl.GetVersion()
|
version := pluginctl.GetVersion()
|
||||||
fmt.Printf("pluginctl version %s\n", version)
|
pluginctl.Logger.Info("pluginctl version", "version", version)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -89,7 +92,7 @@ func runUpdateAssetsCommand(args []string, pluginPath string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func showUsage() {
|
func showUsage() {
|
||||||
fmt.Printf(`pluginctl - Mattermost Plugin Development CLI
|
usageText := `pluginctl - Mattermost Plugin Development CLI
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
pluginctl [global options] <command> [command options] [arguments...]
|
pluginctl [global options] <command> [command options] [arguments...]
|
||||||
|
@ -127,5 +130,6 @@ Environment Variables:
|
||||||
|
|
||||||
For more information about Mattermost plugin development, visit:
|
For more information about Mattermost plugin development, visit:
|
||||||
https://developers.mattermost.com/integrate/plugins/
|
https://developers.mattermost.com/integrate/plugins/
|
||||||
`)
|
`
|
||||||
|
pluginctl.Logger.Info(usageText)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package pluginctl
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/mattermost/mattermost/server/public/model"
|
"github.com/mattermost/mattermost/server/public/model"
|
||||||
)
|
)
|
||||||
|
@ -13,7 +12,7 @@ func RunDisableCommand(args []string, pluginPath string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func disablePlugin(ctx context.Context, client *model.Client4, pluginID string) error {
|
func disablePlugin(ctx context.Context, client *model.Client4, pluginID string) error {
|
||||||
log.Print("Disabling plugin.")
|
Logger.Info("Disabling plugin")
|
||||||
_, err := client.DisablePlugin(ctx, pluginID)
|
_, err := client.DisablePlugin(ctx, pluginID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to disable plugin: %w", err)
|
return fmt.Errorf("failed to disable plugin: %w", err)
|
||||||
|
|
|
@ -3,7 +3,6 @@ package pluginctl
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
|
|
||||||
"github.com/mattermost/mattermost/server/public/model"
|
"github.com/mattermost/mattermost/server/public/model"
|
||||||
)
|
)
|
||||||
|
@ -13,7 +12,7 @@ func RunEnableCommand(args []string, pluginPath string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func enablePlugin(ctx context.Context, client *model.Client4, pluginID string) error {
|
func enablePlugin(ctx context.Context, client *model.Client4, pluginID string) error {
|
||||||
log.Print("Enabling plugin.")
|
Logger.Info("Enabling plugin")
|
||||||
_, err := client.EnablePlugin(ctx, pluginID)
|
_, err := client.EnablePlugin(ctx, pluginID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to enable plugin: %w", err)
|
return fmt.Errorf("failed to enable plugin: %w", err)
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -303,6 +303,7 @@ require (
|
||||||
github.com/ldez/tagliatelle v0.5.0 // indirect
|
github.com/ldez/tagliatelle v0.5.0 // indirect
|
||||||
github.com/leonklingele/grouper v1.1.2 // indirect
|
github.com/leonklingele/grouper v1.1.2 // indirect
|
||||||
github.com/letsencrypt/boulder v0.0.0-20250411005613-d800055fe666 // indirect
|
github.com/letsencrypt/boulder v0.0.0-20250411005613-d800055fe666 // indirect
|
||||||
|
github.com/lmittmann/tint v1.1.2 // indirect
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||||
github.com/macabu/inamedparam v0.1.3 // indirect
|
github.com/macabu/inamedparam v0.1.3 // indirect
|
||||||
github.com/mailru/easyjson v0.9.0 // indirect
|
github.com/mailru/easyjson v0.9.0 // indirect
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -870,6 +870,8 @@ github.com/leonklingele/grouper v1.1.2 h1:o1ARBDLOmmasUaNDesWqWCIFH3u7hoFlM84Yrj
|
||||||
github.com/leonklingele/grouper v1.1.2/go.mod h1:6D0M/HVkhs2yRKRFZUoGjeDy7EZTfFBE9gl4kjmIGkA=
|
github.com/leonklingele/grouper v1.1.2/go.mod h1:6D0M/HVkhs2yRKRFZUoGjeDy7EZTfFBE9gl4kjmIGkA=
|
||||||
github.com/letsencrypt/boulder v0.0.0-20250411005613-d800055fe666 h1:ndfLOJNaxu0fX358UKxtq2bU8IMASWi87Hn0Nv/TIoY=
|
github.com/letsencrypt/boulder v0.0.0-20250411005613-d800055fe666 h1:ndfLOJNaxu0fX358UKxtq2bU8IMASWi87Hn0Nv/TIoY=
|
||||||
github.com/letsencrypt/boulder v0.0.0-20250411005613-d800055fe666/go.mod h1:WGXwLq/jKt0kng727wv6a0h0q7TVC+MwS2S75rcqL+4=
|
github.com/letsencrypt/boulder v0.0.0-20250411005613-d800055fe666/go.mod h1:WGXwLq/jKt0kng727wv6a0h0q7TVC+MwS2S75rcqL+4=
|
||||||
|
github.com/lmittmann/tint v1.1.2 h1:2CQzrL6rslrsyjqLDwD11bZ5OpLBPU+g3G/r5LSfS8w=
|
||||||
|
github.com/lmittmann/tint v1.1.2/go.mod h1:HIS3gSy7qNwGCj+5oRjAutErFBl4BzdQP6cJZ0NfMwE=
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
|
github.com/lunixbochs/vtclean v1.0.0/go.mod h1:pHhQNgMf3btfWnGBVipUOjRYhoOsdGqdm/+2c2E2WMI=
|
||||||
|
|
45
info.go
45
info.go
|
@ -19,63 +19,58 @@ func InfoCommand() error {
|
||||||
|
|
||||||
// PrintPluginInfo displays formatted plugin information.
|
// PrintPluginInfo displays formatted plugin information.
|
||||||
func PrintPluginInfo(manifest *model.Manifest) error {
|
func PrintPluginInfo(manifest *model.Manifest) error {
|
||||||
fmt.Printf("Plugin Information:\n")
|
Logger.Info("Plugin Information:")
|
||||||
fmt.Printf("==================\n\n")
|
Logger.Info("==================")
|
||||||
|
|
||||||
// Basic plugin info
|
// Basic plugin info
|
||||||
fmt.Printf("ID: %s\n", manifest.Id)
|
Logger.Info("ID:", "value", manifest.Id)
|
||||||
fmt.Printf("Name: %s\n", manifest.Name)
|
Logger.Info("Name:", "value", manifest.Name)
|
||||||
fmt.Printf("Version: %s\n", manifest.Version)
|
Logger.Info("Version:", "value", manifest.Version)
|
||||||
|
|
||||||
// Minimum Mattermost version
|
// Minimum Mattermost version
|
||||||
if manifest.MinServerVersion != "" {
|
if manifest.MinServerVersion != "" {
|
||||||
fmt.Printf("Min MM Version: %s\n", manifest.MinServerVersion)
|
Logger.Info("Min MM Version:", "value", manifest.MinServerVersion)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Min MM Version: Not specified\n")
|
Logger.Info("Min MM Version:", "value", "Not specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Description if available
|
// Description if available
|
||||||
if manifest.Description != "" {
|
if manifest.Description != "" {
|
||||||
fmt.Printf("Description: %s\n", manifest.Description)
|
Logger.Info("Description:", "value", manifest.Description)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("\nCode Components:\n")
|
Logger.Info("Code Components:")
|
||||||
fmt.Printf("================\n")
|
Logger.Info("================")
|
||||||
|
|
||||||
// Server code presence
|
// Server code presence
|
||||||
if HasServerCode(manifest) {
|
if HasServerCode(manifest) {
|
||||||
fmt.Printf("Server Code: Yes\n")
|
Logger.Info("Server Code:", "value", "Yes")
|
||||||
if manifest.Server != nil && len(manifest.Server.Executables) > 0 {
|
if manifest.Server != nil && len(manifest.Server.Executables) > 0 {
|
||||||
fmt.Printf(" Executables: ")
|
var executables []string
|
||||||
first := true
|
|
||||||
for platform := range manifest.Server.Executables {
|
for platform := range manifest.Server.Executables {
|
||||||
if !first {
|
executables = append(executables, platform)
|
||||||
fmt.Printf(", ")
|
|
||||||
}
|
|
||||||
fmt.Printf("%s", platform)
|
|
||||||
first = false
|
|
||||||
}
|
}
|
||||||
fmt.Printf("\n")
|
Logger.Info("Executables:", "platforms", executables)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Server Code: No\n")
|
Logger.Info("Server Code:", "value", "No")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Webapp code presence
|
// Webapp code presence
|
||||||
if HasWebappCode(manifest) {
|
if HasWebappCode(manifest) {
|
||||||
fmt.Printf("Webapp Code: Yes\n")
|
Logger.Info("Webapp Code:", "value", "Yes")
|
||||||
if manifest.Webapp != nil && manifest.Webapp.BundlePath != "" {
|
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 {
|
} else {
|
||||||
fmt.Printf("Webapp Code: No\n")
|
Logger.Info("Webapp Code:", "value", "No")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Settings schema
|
// Settings schema
|
||||||
if manifest.SettingsSchema != nil {
|
if manifest.SettingsSchema != nil {
|
||||||
fmt.Printf("Settings Schema: Yes\n")
|
Logger.Info("Settings Schema:", "value", "Yes")
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("Settings Schema: No\n")
|
Logger.Info("Settings Schema:", "value", "No")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
37
logger.go
Normal file
37
logger.go
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
package pluginctl
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/lmittmann/tint"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Logger is the global logger instance
|
||||||
|
var Logger *slog.Logger
|
||||||
|
|
||||||
|
// InitLogger initializes the global logger
|
||||||
|
func InitLogger() {
|
||||||
|
// Create a tint handler for colorized output
|
||||||
|
handler := tint.NewHandler(os.Stderr, &tint.Options{
|
||||||
|
Level: slog.LevelInfo,
|
||||||
|
TimeFormat: time.Kitchen,
|
||||||
|
AddSource: false,
|
||||||
|
NoColor: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
Logger = slog.New(handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetLogLevel sets the minimum logging level
|
||||||
|
func SetLogLevel(level slog.Level) {
|
||||||
|
handler := tint.NewHandler(os.Stderr, &tint.Options{
|
||||||
|
Level: level,
|
||||||
|
TimeFormat: time.Kitchen,
|
||||||
|
AddSource: false,
|
||||||
|
NoColor: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
Logger = slog.New(handler)
|
||||||
|
}
|
|
@ -18,7 +18,7 @@ func RunUpdateAssetsCommand(args []string, pluginPath string) error {
|
||||||
return fmt.Errorf("updateassets command does not accept arguments")
|
return fmt.Errorf("updateassets command does not accept arguments")
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Updating assets in plugin directory: %s\n", pluginPath)
|
Logger.Info("Updating assets in plugin directory", "path", pluginPath)
|
||||||
|
|
||||||
// Load plugin manifest to check for webapp code
|
// Load plugin manifest to check for webapp code
|
||||||
manifest, err := LoadPluginManifestFromPath(pluginPath)
|
manifest, err := LoadPluginManifestFromPath(pluginPath)
|
||||||
|
@ -82,7 +82,7 @@ func RunUpdateAssetsCommand(args []string, pluginPath string) error {
|
||||||
if err := os.WriteFile(targetPath, content, 0644); err != nil {
|
if err := os.WriteFile(targetPath, content, 0644); err != nil {
|
||||||
return fmt.Errorf("failed to write file %s: %w", targetPath, err)
|
return fmt.Errorf("failed to write file %s: %w", targetPath, err)
|
||||||
}
|
}
|
||||||
fmt.Printf("Updated file: %s\n", relativePath)
|
Logger.Info("Updated file", "path", relativePath)
|
||||||
updatedCount++
|
updatedCount++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -93,6 +93,6 @@ func RunUpdateAssetsCommand(args []string, pluginPath string) error {
|
||||||
return fmt.Errorf("failed to update assets: %w", err)
|
return fmt.Errorf("failed to update assets: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Assets updated successfully! (%d files updated)\n", updatedCount)
|
Logger.Info("Assets updated successfully!", "files_updated", updatedCount)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
package pluginctl
|
package pluginctl
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RunVersionCommand implements the 'version' command functionality.
|
// RunVersionCommand implements the 'version' command functionality.
|
||||||
func RunVersionCommand(args []string) error {
|
func RunVersionCommand(args []string) error {
|
||||||
version := GetVersion()
|
version := GetVersion()
|
||||||
fmt.Printf("pluginctl version %s\n", version)
|
Logger.Info("pluginctl version", "version", version)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue