Skip to content

Commit

Permalink
Merge pull request #84 from ssotops/fix/install-plugins-local
Browse files Browse the repository at this point in the history
fix/install plugins local
  • Loading branch information
alechp authored Sep 24, 2024
2 parents 8241443 + baff220 commit 86b66a9
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 129 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore

logs
.repositories/gitspace/gs/*
cmd/cmd
archive-*
Expand Down
4 changes: 2 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"strings"

"github.com/charmbracelet/huh"
"github.com/charmbracelet/log"
"github.com/mitchellh/go-homedir"
"github.com/pelletier/go-toml"
"github.com/ssotops/gitspace-plugin-sdk/logger"
)

type Config struct {
Expand Down Expand Up @@ -57,7 +57,7 @@ func getCacheDir() (string, error) {
return cacheDir, nil
}

func getConfigFromUser(logger *log.Logger) (*Config, error) {
func getConfigFromUser(logger *logger.RateLimitedLogger) (*Config, error) {
defaultPath := "./gs.toml"

for {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/pelletier/go-toml v1.9.5
github.com/pelletier/go-toml/v2 v2.2.3
github.com/ssotops/gitspace-plugin-sdk v0.0.0-00010101000000-000000000000
github.com/ssotops/gitspace-plugin-sdk v0.0.0-20240923114730-3921de448c9c
golang.org/x/oauth2 v0.23.0
google.golang.org/protobuf v1.34.2
)
Expand Down Expand Up @@ -70,3 +70,5 @@ exclude github.com/ssotops/gitspace/examples/plugins v0.0.0
replace github.com/ssotops/gitspace/lib => ./lib

replace github.com/ssotops/gitspace-plugin-sdk => ./gs/gitspace-plugin-sdk

replace github.com/ssotops/gitspace-plugin-sdk/logger => ./gs/gitspace-plugin-sdk/logger
64 changes: 64 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package logger

import (
"os"
"path/filepath"
"sync"
"time"

"github.com/charmbracelet/log"
)

type RateLimitedLogger struct {
logger *log.Logger
fileLogger *log.Logger
lastLogTime map[string]time.Time
logInterval time.Duration
mu sync.Mutex
}

func NewRateLimitedLogger(logDir string) (*RateLimitedLogger, error) {
if err := os.MkdirAll(logDir, 0755); err != nil {
return nil, err
}

logFile, err := os.OpenFile(filepath.Join(logDir, "gitspace.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return nil, err
}

return &RateLimitedLogger{
logger: log.New(os.Stderr),
fileLogger: log.New(logFile),
lastLogTime: make(map[string]time.Time),
logInterval: time.Second * 5, // Log the same message at most once every 5 seconds
}, nil
}

func (l *RateLimitedLogger) Log(level log.Level, message string, keyvals ...interface{}) {
l.mu.Lock()
defer l.mu.Unlock()

now := time.Now()
if lastLog, exists := l.lastLogTime[message]; !exists || now.Sub(lastLog) >= l.logInterval {
l.logger.Log(level, message, keyvals...)
l.fileLogger.Log(level, message, keyvals...)
l.lastLogTime[message] = now
}
}

func (l *RateLimitedLogger) Info(message string, keyvals ...interface{}) {
l.Log(log.InfoLevel, message, keyvals...)
}

func (l *RateLimitedLogger) Debug(message string, keyvals ...interface{}) {
l.Log(log.DebugLevel, message, keyvals...)
}

func (l *RateLimitedLogger) Error(message string, keyvals ...interface{}) {
l.Log(log.ErrorLevel, message, keyvals...)
}

func (l *RateLimitedLogger) Warn(message string, keyvals ...interface{}) {
l.Log(log.WarnLevel, message, keyvals...)
}
17 changes: 14 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,27 @@ import (
"fmt"
"os"
"os/signal"
"path/filepath"
"syscall"

"github.com/charmbracelet/log"
"github.com/ssotops/gitspace-plugin-sdk/logger"
"github.com/ssotops/gitspace/plugin"
)

func main() {
logger := initLogger()
logDir := filepath.Join("logs", "gitspace")
logger, err := logger.NewRateLimitedLogger(logDir, "gitspace")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create logger: %v\n", err)
os.Exit(1)
}

logger.Info("Gitspace starting up")

// Set log level to Debug for detailed logging
logger.SetLogLevel(log.DebugLevel)

signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)

Expand All @@ -27,8 +38,8 @@ func main() {
logger.Debug("Config loaded successfully", "config_path", config.Global.Path)

// Initialize the plugin manager
pluginManager := plugin.NewManager()
err = pluginManager.DiscoverPlugins(logger)
pluginManager := plugin.NewManager(logger)
err = pluginManager.DiscoverPlugins()
if err != nil {
logger.Error("Failed to discover plugins", "error", err)
}
Expand Down
59 changes: 24 additions & 35 deletions plugin/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package plugin
import (
"encoding/json"
"fmt"
"path/filepath"

"github.com/charmbracelet/huh"
"github.com/charmbracelet/log"
pb "github.com/ssotops/gitspace-plugin-sdk/proto"
"github.com/ssotops/gitspace-plugin-sdk/logger"
)

func HandleInstallPlugin(logger *log.Logger, manager *Manager) error {
func HandleInstallPlugin(logger *logger.RateLimitedLogger, manager *Manager) error {
var installChoice string
err := huh.NewSelect[string]().
Title("Choose installation type").
Expand Down Expand Up @@ -54,32 +53,10 @@ func HandleInstallPlugin(logger *log.Logger, manager *Manager) error {
return fmt.Errorf("failed to install plugin: %w", err)
}

pluginsDir, err := getPluginsDir()
if err != nil {
return fmt.Errorf("failed to get plugins directory: %w", err)
}

// Load the manifest to get the plugin name
manifestPath := filepath.Join(pluginsDir, filepath.Base(source), "gitspace-plugin.toml")
manifest, err := loadPluginManifest(manifestPath)
if err != nil {
return fmt.Errorf("failed to load plugin manifest: %w", err)
}

pluginName := manifest.Metadata.Name
// pluginPath := filepath.Join(pluginsDir, pluginName, pluginName+".so")

// Load the plugin
err = manager.LoadPlugin(pluginName, logger)
if err != nil {
return fmt.Errorf("error loading plugin: %w", err)
}

logger.Info("Plugin installed and loaded successfully", "name", pluginName)
logger.Info("Plugin installed successfully")
return nil
}

func HandleUninstallPlugin(logger *log.Logger, manager *Manager) error {
func HandleUninstallPlugin(logger *logger.RateLimitedLogger, manager *Manager) error {
plugins, err := ListInstalledPlugins(logger)
if err != nil {
return fmt.Errorf("failed to list installed plugins: %w", err)
Expand Down Expand Up @@ -112,10 +89,11 @@ func HandleUninstallPlugin(logger *log.Logger, manager *Manager) error {
}

logger.Info("Plugin uninstalled and unloaded successfully", "name", selectedPlugin)
logger.Info("Plugin installed successfully")
return nil
}

func HandleListInstalledPlugins(logger *log.Logger) error {
func HandleListInstalledPlugins(logger *logger.RateLimitedLogger) error {
plugins, err := ListInstalledPlugins(logger)
if err != nil {
return fmt.Errorf("failed to list installed plugins: %w", err)
Expand All @@ -133,7 +111,7 @@ func HandleListInstalledPlugins(logger *log.Logger) error {
return nil
}

func HandleRunPlugin(logger *log.Logger, manager *Manager) error {
func HandleRunPlugin(logger *logger.RateLimitedLogger, manager *Manager) error {
discoveredPlugins := manager.GetDiscoveredPlugins()
logger.Debug("Discovered plugins", "count", len(discoveredPlugins))

Expand All @@ -160,26 +138,35 @@ func HandleRunPlugin(logger *log.Logger, manager *Manager) error {

logger.Debug("Selected plugin", "name", selectedPlugin)

// Load the plugin
err = manager.LoadPlugin(selectedPlugin, logger)
if err != nil {
logger.Error("Failed to load plugin", "name", selectedPlugin, "error", err)
return fmt.Errorf("failed to load plugin %s: %w", selectedPlugin, err)
// Load the plugin if it's not already loaded
if !manager.IsPluginLoaded(selectedPlugin) {
err = manager.LoadPlugin(selectedPlugin)
if err != nil {
logger.Error("Failed to load plugin", "name", selectedPlugin, "error", err)
return fmt.Errorf("failed to load plugin %s: %w", selectedPlugin, err)
}
}

// Get the plugin menu
logger.Debug("Getting menu for selected plugin", "plugin", selectedPlugin)
menuResp, err := manager.GetPluginMenu(selectedPlugin)
if err != nil {
logger.Error("Error getting plugin menu", "error", err)
return fmt.Errorf("error getting plugin menu: %w", err)
}

logger.Debug("Received menu response", "dataSize", len(menuResp.MenuData))

var menuOptions []MenuOption
err = json.Unmarshal(menuResp.MenuData, &menuOptions)
if err != nil {
logger.Error("Error unmarshalling menu data", "error", err)
return fmt.Errorf("error unmarshalling menu data: %w", err)
}

logger.Debug("Unmarshalled menu options", "optionsCount", len(menuOptions))

// Present menu to user
var selectedCommand string
err = huh.NewSelect[string]().
Title("Choose an action").
Expand All @@ -198,6 +185,8 @@ func HandleRunPlugin(logger *log.Logger, manager *Manager) error {
return fmt.Errorf("error running menu: %w", err)
}

logger.Debug("User selected command", "command", selectedCommand)

// Execute the selected command
result, err := manager.ExecuteCommand(selectedPlugin, selectedCommand, nil)
if err != nil {
Expand All @@ -209,7 +198,7 @@ func HandleRunPlugin(logger *log.Logger, manager *Manager) error {
return nil
}

func handleGitspaceCatalogInstall(logger *log.Logger) (string, error) {
func handleGitspaceCatalogInstall(logger *logger.RateLimitedLogger) (string, error) {
owner := "ssotops"
repo := "gitspace-catalog"
catalog, err := fetchGitspaceCatalog(owner, repo)
Expand Down
Loading

0 comments on commit 86b66a9

Please sign in to comment.