Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 13 additions & 59 deletions cmd/config/setup.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package config

import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"

"github.com/jcleira/workspace/cmd"
"github.com/jcleira/workspace/pkg/ui/commands"
"github.com/jcleira/workspace/pkg/ui/setup"
"github.com/jcleira/workspace/pkg/workspace"
)

Expand All @@ -28,70 +26,26 @@ func init() {
}

func runInteractiveSetup() {
reader := bufio.NewReader(os.Stdin)
homeDir := os.Getenv("HOME")

fmt.Println()
fmt.Println(commands.TitleStyle.Render("Workspace Configuration Setup"))
fmt.Println()
commands.PrintInfo("Configure the directories where workspace will store its data.")
fmt.Println()

fmt.Printf("Workspaces directory (where workspace projects will be created):\n")
fmt.Printf("Default: %s\n", commands.InfoStyle.Render(filepath.Join(homeDir, "workspaces")))
fmt.Print("Enter path: ")
workspacesDir, err := reader.ReadString('\n')
if err != nil || strings.TrimSpace(workspacesDir) == "" {
workspacesDir = filepath.Join(homeDir, "workspaces")
} else {
workspacesDir = strings.TrimSpace(workspacesDir)
}

fmt.Println()
fmt.Printf("Repositories directory (where git repositories will be cloned):\n")
fmt.Printf("Default: %s\n", commands.InfoStyle.Render(filepath.Join(homeDir, "repos")))
fmt.Print("Enter path: ")
reposDir, err := reader.ReadString('\n')
if err != nil || strings.TrimSpace(reposDir) == "" {
reposDir = filepath.Join(homeDir, "repos")
} else {
reposDir = strings.TrimSpace(reposDir)
}

fmt.Println()
fmt.Printf("Claude directory (shared .claude context directory):\n")
fmt.Printf("Default: %s\n", commands.InfoStyle.Render(filepath.Join(homeDir, ".claude")))
fmt.Print("Enter path: ")
claudeDir, err := reader.ReadString('\n')
if err != nil || strings.TrimSpace(claudeDir) == "" {
claudeDir = filepath.Join(homeDir, ".claude")
} else {
claudeDir = strings.TrimSpace(claudeDir)
}

fmt.Println()
commands.PrintInfo("Saving configuration...")

if err := cmd.ConfigManager.SetWorkspacesDir(workspacesDir); err != nil {
commands.PrintError(fmt.Sprintf("Failed to set workspaces directory: %v", err))
os.Exit(1)
}
cfg := cmd.ConfigManager.GetConfig()

if err := cmd.ConfigManager.SetReposDir(reposDir); err != nil {
commands.PrintError(fmt.Sprintf("Failed to set repos directory: %v", err))
result, err := setup.RunSetupWizardWithDefaults(
cmd.ConfigManager,
cfg.ReposDir,
cfg.WorkspacesDir,
cfg.ClaudeDir,
)
if err != nil {
commands.PrintError(fmt.Sprintf("Setup wizard failed: %v", err))
os.Exit(1)
}

if err := cmd.ConfigManager.SetClaudeDir(claudeDir); err != nil {
commands.PrintError(fmt.Sprintf("Failed to set claude directory: %v", err))
os.Exit(1)
if !result.Completed {
return
}

cfg := cmd.ConfigManager.GetConfig()
cfg = cmd.ConfigManager.GetConfig()
cmd.WorkspaceManager = workspace.NewManager(cfg.WorkspacesDir, cfg.ReposDir, cfg.ClaudeDir)

fmt.Println()
commands.PrintSuccess("Configuration saved successfully!")
fmt.Println()
showConfig()
}
80 changes: 57 additions & 23 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"

"github.com/jcleira/workspace/pkg/config"
"github.com/jcleira/workspace/pkg/shell"
"github.com/jcleira/workspace/pkg/ui/commands"
"github.com/jcleira/workspace/pkg/ui/dashboard"
"github.com/jcleira/workspace/pkg/ui/setup"
"github.com/jcleira/workspace/pkg/workspace"
)

Expand Down Expand Up @@ -48,6 +50,16 @@ func InitializeConfig() error {
return fmt.Errorf("failed to initialize configuration: %w", err)
}

if !ConfigManager.IsInitialized() {
result, err := setup.RunSetupWizard(ConfigManager)
if err != nil {
return fmt.Errorf("setup wizard failed: %w", err)
}
if !result.Completed {
os.Exit(0)
}
}

cfg := ConfigManager.GetConfig()
WorkspaceManager = workspace.NewManager(cfg.WorkspacesDir, cfg.ReposDir, cfg.ClaudeDir)

Expand All @@ -60,33 +72,55 @@ func init() {
}

func runInteractiveWorkspaceSelector() {
workspaces, err := WorkspaceManager.GetWorkspaces()
if err != nil {
commands.PrintError(fmt.Sprintf("Failed to get workspaces: %v", err))
return
}
for {
workspaces, err := WorkspaceManager.GetWorkspaces()
if err != nil {
commands.PrintError(fmt.Sprintf("Failed to get workspaces: %v", err))
return
}

if len(workspaces) == 0 {
commands.PrintWarning("No workspaces found.")
fmt.Println("Create one with: workspace create <name>")
return
}
if len(workspaces) == 0 {
commands.PrintWarning("No workspaces found.")
fmt.Println("Create one with: workspace create <name>")
return
}

selectedPath, err := dashboard.RunDashboard(WorkspaceManager, ConfigManager)
if err != nil {
commands.PrintError(fmt.Sprintf("Dashboard error: %v", err))
return
}
result, err := dashboard.RunDashboard(WorkspaceManager, ConfigManager)
if err != nil {
commands.PrintError(fmt.Sprintf("Dashboard error: %v", err))
return
}

if result.OpenSetup {
cfg := ConfigManager.GetConfig()
setupResult, err := setup.RunSetupWizardWithDefaults(
ConfigManager,
cfg.ReposDir,
cfg.WorkspacesDir,
cfg.ClaudeDir,
)
if err != nil {
commands.PrintError(fmt.Sprintf("Setup wizard failed: %v", err))
return
}
if setupResult.Completed {
cfg = ConfigManager.GetConfig()
WorkspaceManager = workspace.NewManager(cfg.WorkspacesDir, cfg.ReposDir, cfg.ClaudeDir)
}
continue
}

if selectedPath != "" {
if OutputPathOnly {
fmt.Println(selectedPath)
} else {
ws := workspace.Workspace{Path: selectedPath}
shell.NavigateToWorkspace(ws)
if result.SelectedPath != "" {
if OutputPathOnly {
fmt.Println(result.SelectedPath)
} else {
ws := workspace.Workspace{Path: result.SelectedPath}
shell.NavigateToWorkspace(ws)
}
} else if OutputPathOnly {
fmt.Println("quit")
}
} else if OutputPathOnly {
fmt.Println("quit")
return
}
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect
github.com/charmbracelet/x/ansi v0.9.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.21.0 h1:9TdC97SdRVg/1aaXNVWfFH3nnLAwOXr8Fn6u6mfQdFs=
Expand Down
47 changes: 47 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Config struct {
ReposDir string `json:"repos_dir"`
ClaudeDir string `json:"claude_dir"`
IgnoredBranches []string `json:"ignored_branches,omitempty"`
Initialized bool `json:"initialized"`
}

// ConfigManager handles configuration operations
Expand Down Expand Up @@ -204,3 +205,49 @@ func (cm *ConfigManager) ClearIgnoredBranches() error {
cm.config.IgnoredBranches = []string{}
return cm.saveConfig()
}

// IsInitialized checks if the workspace has been initialized
func (cm *ConfigManager) IsInitialized() bool {
if cm.config.Initialized {
return true
}
if _, err := os.Stat(cm.config.ReposDir); err == nil {
entries, err := os.ReadDir(cm.config.ReposDir)
if err == nil && len(entries) > 0 {
return true
}
}
return false
}

// SetInitialized sets the initialized flag and saves the config
func (cm *ConfigManager) SetInitialized(initialized bool) error {
cm.config.Initialized = initialized
return cm.saveConfig()
}

// UpdateConfig updates multiple config values and saves
func (cm *ConfigManager) UpdateConfig(workspacesDir, reposDir, claudeDir string) error {
if workspacesDir != "" {
absDir, err := filepath.Abs(workspacesDir)
if err != nil {
return fmt.Errorf("failed to get absolute path for workspaces dir: %w", err)
}
cm.config.WorkspacesDir = absDir
}
if reposDir != "" {
absDir, err := filepath.Abs(reposDir)
if err != nil {
return fmt.Errorf("failed to get absolute path for repos dir: %w", err)
}
cm.config.ReposDir = absDir
}
if claudeDir != "" {
absDir, err := filepath.Abs(claudeDir)
if err != nil {
return fmt.Errorf("failed to get absolute path for claude dir: %w", err)
}
cm.config.ClaudeDir = absDir
}
return cm.saveConfig()
}
29 changes: 24 additions & 5 deletions pkg/ui/dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type DashboardModel struct {

selectedPath string
quitting bool
openSetup bool
}

// NewDashboard creates a new dashboard model.
Expand Down Expand Up @@ -183,6 +184,10 @@ func (m DashboardModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.quitting = true
return m, tea.Quit

case key.Matches(msg, m.keys.Settings):
m.openSetup = true
return m, tea.Quit

case key.Matches(msg, m.keys.Help):
m.showHelp = true
return m, nil
Expand Down Expand Up @@ -560,8 +565,8 @@ func (m DashboardModel) renderFooter() string {
helpKeyStyle.Render("Enter") + helpDescStyle.Render(" select"),
helpKeyStyle.Render("f") + helpDescStyle.Render(" fetch"),
helpKeyStyle.Render("p") + helpDescStyle.Render(" pull"),
helpKeyStyle.Render("G") + helpDescStyle.Render(" staged diff"),
helpKeyStyle.Render("d") + helpDescStyle.Render(" delete"),
helpKeyStyle.Render("S") + helpDescStyle.Render(" settings"),
helpKeyStyle.Render("?") + helpDescStyle.Render(" help"),
helpKeyStyle.Render("q") + helpDescStyle.Render(" quit"),
}
Expand All @@ -583,16 +588,30 @@ func (m DashboardModel) SelectedPath() string {
return m.selectedPath
}

// RunDashboard runs the dashboard and returns the selected workspace path.
func RunDashboard(wm *workspace.Manager, cm *config.ConfigManager) (string, error) {
// OpenSetup returns true if the user requested to open the setup wizard.
func (m DashboardModel) OpenSetup() bool {
return m.openSetup
}

// DashboardResult contains the result of running the dashboard.
type DashboardResult struct {
SelectedPath string
OpenSetup bool
}

// RunDashboard runs the dashboard and returns the result.
func RunDashboard(wm *workspace.Manager, cm *config.ConfigManager) (DashboardResult, error) {
m := NewDashboard(wm, cm)
p := tea.NewProgram(m, tea.WithAltScreen())

finalModel, err := p.Run()
if err != nil {
return "", err
return DashboardResult{}, err
}

dm := finalModel.(DashboardModel)
return dm.SelectedPath(), nil
return DashboardResult{
SelectedPath: dm.SelectedPath(),
OpenSetup: dm.OpenSetup(),
}, nil
}
7 changes: 6 additions & 1 deletion pkg/ui/dashboard/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type KeyMap struct {
Refresh key.Binding
Diff key.Binding
DiffStaged key.Binding
Settings key.Binding
Help key.Binding
Quit key.Binding
Filter key.Binding
Expand Down Expand Up @@ -74,6 +75,10 @@ func DefaultKeyMap() KeyMap {
key.WithKeys("G"),
key.WithHelp("G", "diff staged"),
),
Settings: key.NewBinding(
key.WithKeys("S"),
key.WithHelp("S", "settings"),
),
Help: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "help"),
Expand Down Expand Up @@ -109,6 +114,6 @@ func (k KeyMap) FullHelp() [][]key.Binding {
{k.Select, k.Fetch, k.Pull},
{k.Diff, k.DiffStaged, k.Refresh},
{k.Delete, k.Create},
{k.Filter, k.Help, k.Quit},
{k.Settings, k.Filter, k.Help, k.Quit},
}
}
Loading