Initial commit.

This commit is contained in:
2025-10-11 17:06:14 +00:00
commit ec521c3c91
45 changed files with 9798 additions and 0 deletions

140
internal/context/context.go Normal file
View File

@@ -0,0 +1,140 @@
package context
import (
"fmt"
"path/filepath"
"strings"
"github.com/wild-cloud/wild-central/daemon/internal/storage"
)
// Manager handles current instance context tracking
type Manager struct {
dataDir string
}
// NewManager creates a new context manager
func NewManager(dataDir string) *Manager {
return &Manager{
dataDir: dataDir,
}
}
// GetContextFilePath returns the path to the context file
func (m *Manager) GetContextFilePath() string {
return filepath.Join(m.dataDir, "current-context")
}
// GetCurrentContext retrieves the name of the current instance context
func (m *Manager) GetCurrentContext() (string, error) {
contextFile := m.GetContextFilePath()
if !storage.FileExists(contextFile) {
return "", fmt.Errorf("no current context set")
}
content, err := storage.ReadFile(contextFile)
if err != nil {
return "", fmt.Errorf("reading context file: %w", err)
}
contextName := strings.TrimSpace(string(content))
if contextName == "" {
return "", fmt.Errorf("context file is empty")
}
return contextName, nil
}
// SetCurrentContext sets the current instance context
func (m *Manager) SetCurrentContext(instanceName string) error {
if instanceName == "" {
return fmt.Errorf("instance name cannot be empty")
}
// Verify instance exists
instancePath := filepath.Join(m.dataDir, "instances", instanceName)
if !storage.FileExists(instancePath) {
return fmt.Errorf("instance %s does not exist", instanceName)
}
contextFile := m.GetContextFilePath()
// Ensure data directory exists
if err := storage.EnsureDir(m.dataDir, 0755); err != nil {
return err
}
// Acquire lock before writing
lockPath := contextFile + ".lock"
return storage.WithLock(lockPath, func() error {
return storage.WriteFile(contextFile, []byte(instanceName), 0644)
})
}
// ClearCurrentContext removes the current context
func (m *Manager) ClearCurrentContext() error {
contextFile := m.GetContextFilePath()
if !storage.FileExists(contextFile) {
// Already cleared
return nil
}
// Acquire lock before deleting
lockPath := contextFile + ".lock"
return storage.WithLock(lockPath, func() error {
return storage.WriteFile(contextFile, []byte(""), 0644)
})
}
// HasCurrentContext checks if a current context is set
func (m *Manager) HasCurrentContext() bool {
_, err := m.GetCurrentContext()
return err == nil
}
// ValidateContext checks if the current context is valid (instance exists)
func (m *Manager) ValidateContext() error {
contextName, err := m.GetCurrentContext()
if err != nil {
return err
}
instancePath := filepath.Join(m.dataDir, "instances", contextName)
if !storage.FileExists(instancePath) {
return fmt.Errorf("current context %s points to non-existent instance", contextName)
}
return nil
}
// GetCurrentInstancePath returns the path to the current instance directory
func (m *Manager) GetCurrentInstancePath() (string, error) {
contextName, err := m.GetCurrentContext()
if err != nil {
return "", err
}
return filepath.Join(m.dataDir, "instances", contextName), nil
}
// GetCurrentInstanceConfigPath returns the path to the current instance's config file
func (m *Manager) GetCurrentInstanceConfigPath() (string, error) {
instancePath, err := m.GetCurrentInstancePath()
if err != nil {
return "", err
}
return filepath.Join(instancePath, "config.yaml"), nil
}
// GetCurrentInstanceSecretsPath returns the path to the current instance's secrets file
func (m *Manager) GetCurrentInstanceSecretsPath() (string, error) {
instancePath, err := m.GetCurrentInstancePath()
if err != nil {
return "", err
}
return filepath.Join(instancePath, "secrets.yaml"), nil
}

View File

@@ -0,0 +1,100 @@
package context
import (
"os"
"path/filepath"
"testing"
)
func TestManager_GetSetCurrentContext(t *testing.T) {
tmpDir := t.TempDir()
m := NewManager(tmpDir)
// Create test instances
instancesDir := filepath.Join(tmpDir, "instances")
instances := []string{"cloud1", "cloud2"}
for _, name := range instances {
instancePath := filepath.Join(instancesDir, name)
err := os.MkdirAll(instancePath, 0755)
if err != nil {
t.Fatalf("Failed to create instance dir: %v", err)
}
}
// Initially should have no context
_, err := m.GetCurrentContext()
if err == nil {
t.Fatalf("Should have no context initially")
}
// Set context
err = m.SetCurrentContext("cloud1")
if err != nil {
t.Fatalf("SetCurrentContext failed: %v", err)
}
// Get context
ctx, err := m.GetCurrentContext()
if err != nil {
t.Fatalf("GetCurrentContext failed: %v", err)
}
if ctx != "cloud1" {
t.Errorf("Wrong context: got %q, want %q", ctx, "cloud1")
}
// Change context
err = m.SetCurrentContext("cloud2")
if err != nil {
t.Fatalf("SetCurrentContext failed: %v", err)
}
ctx, err = m.GetCurrentContext()
if err != nil {
t.Fatalf("GetCurrentContext failed: %v", err)
}
if ctx != "cloud2" {
t.Errorf("Wrong context: got %q, want %q", ctx, "cloud2")
}
}
func TestManager_SetCurrentContext_ValidationError(t *testing.T) {
tmpDir := t.TempDir()
m := NewManager(tmpDir)
// Trying to set context to non-existent instance should fail
err := m.SetCurrentContext("non-existent")
if err == nil {
t.Fatalf("SetCurrentContext should fail for non-existent instance")
}
}
func TestManager_ClearCurrentContext(t *testing.T) {
tmpDir := t.TempDir()
m := NewManager(tmpDir)
// Create test instance
instancesDir := filepath.Join(tmpDir, "instances")
instancePath := filepath.Join(instancesDir, "test-cloud")
err := os.MkdirAll(instancePath, 0755)
if err != nil {
t.Fatalf("Failed to create instance dir: %v", err)
}
// Set context
err = m.SetCurrentContext("test-cloud")
if err != nil {
t.Fatalf("SetCurrentContext failed: %v", err)
}
// Clear context
err = m.ClearCurrentContext()
if err != nil {
t.Fatalf("ClearCurrentContext failed: %v", err)
}
// Context should be gone
_, err = m.GetCurrentContext()
if err == nil {
t.Fatalf("Context should be cleared")
}
}