Skip to content

Commit 4cba9ff

Browse files
committed
feat: add switch '-' option to cycle between prev profile
1 parent 5476df9 commit 4cba9ff

4 files changed

Lines changed: 46 additions & 14 deletions

File tree

README.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ git-context --version
9191
First-time setup:
9292

9393
```bash
94+
# Initialize git-context
9495
git-context init
9596
```
9697

@@ -99,6 +100,7 @@ This creates `~/.config/git-context/config.yaml`.
99100
### Add Your First Profile
100101

101102
```bash
103+
# Add a new profile named 'work'
102104
git-context add work
103105
```
104106

@@ -112,6 +114,7 @@ You'll be prompted for:
112114
#### 3. List All Profiles
113115

114116
```bash
117+
# List available profiles
115118
git-context list
116119
```
117120

@@ -130,7 +133,11 @@ university andre@university.edu
130133
#### 4. Switch Between Profiles
131134

132135
```bash
136+
# Switch to 'personal' profile
133137
git-context switch personal
138+
139+
# Switch back to the previous profile
140+
git-context switch -
134141
```
135142

136143
**Output:**
@@ -146,34 +153,37 @@ git-context switch personal
146153
#### 5. Show Current Profile
147154

148155
```bash
156+
# Display the active profile
149157
git-context current
150158
```
151159

152160
#### 6. Show Profile Details
153161

154162
```bash
163+
# Show details of 'work' profile
155164
git-context show work
156165
```
157166

158167
#### 7. Remove a Profile
159168

160169
```bash
170+
# Remove 'university' profile
161171
git-context remove university
162172
```
163173

164174
### All Available Commands
165175

166-
| Command | Description |
167-
| --------------------------- | ------------------------ |
168-
| `git-context init` | Initialize configuration |
169-
| `git-context add <name>` | Create a new profile |
170-
| `git-context switch <name>` | Switch to a profile |
171-
| `git-context list` | List all profiles |
172-
| `git-context current` | Show active profile |
173-
| `git-context show <name>` | Show profile details |
174-
| `git-context remove <name>` | Delete a profile |
175-
| `git-context --help` | Show help |
176-
| `git-context --version` | Show version |
176+
| Command | Description |
177+
| ----------------------------- | ------------------------ |
178+
| `git-context init` | Initialize configuration |
179+
| `git-context add <name>` | Create a new profile |
180+
| `git-context switch <name\|->`| Switch to a profile |
181+
| `git-context list` | List all profiles |
182+
| `git-context current` | Show active profile |
183+
| `git-context show <name>` | Show profile details |
184+
| `git-context remove <name>` | Delete a profile |
185+
| `git-context --help` | Show help |
186+
| `git-context --version` | Show version |
177187

178188
## Configuration
179189

cmd/switch.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
)
1212

1313
var switchCmd = &cobra.Command{
14-
Use: "switch [profile-name]",
14+
Use: "switch [profile-name|-]",
1515
Short: "Switch to a different profile",
16-
Long: `Switch the active git configuration to a different profile.`,
16+
Long: `Switch the active git configuration to a different profile. Use '-' to return to the previous profile.`,
1717
Args: cobra.ExactArgs(1),
1818
RunE: runSwitch,
1919
}
2020

2121
func runSwitch(cmd *cobra.Command, args []string) error {
22-
profileName := args[0]
22+
requestedName := args[0]
2323

2424
paths, err := config.NewPaths()
2525
if err != nil {
@@ -35,6 +35,17 @@ func runSwitch(cmd *cobra.Command, args []string) error {
3535
return errors.Wrap(err, "failed to load config")
3636
}
3737

38+
profileName := requestedName
39+
if requestedName == "-" {
40+
if cfg.Previous == "" {
41+
ui.PrintWarning("No previous profile to switch to")
42+
43+
return errors.WithStack(errors.New("no previous profile to switch to"))
44+
}
45+
46+
profileName = cfg.Previous
47+
}
48+
3849
// Check if profile exists
3950
profile, err := cfg.GetProfile(profileName)
4051
if err != nil {
@@ -72,6 +83,10 @@ func runSwitch(cmd *cobra.Command, args []string) error {
7283
}
7384

7485
// Update current profile
86+
if cfg.Current != "" && cfg.Current != profileName {
87+
cfg.Previous = cfg.Current
88+
}
89+
7590
cfg.Current = profileName
7691
if err := cfg.SaveConfig(paths.ConfigFile); err != nil {
7792
ui.PrintError(fmt.Sprintf("Failed to save config: %v", err))

internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ type Config struct {
5959
Global map[string]any `yaml:"global"`
6060
Profiles map[string]*Profile `yaml:"profiles"`
6161
Current string `yaml:"-"` // Not saved, determined at runtime
62+
Previous string `yaml:"previous,omitempty"`
6263
}
6364

6465
// NewConfig creates a new empty config.
@@ -67,6 +68,7 @@ func NewConfig() *Config {
6768
Global: make(map[string]any),
6869
Profiles: make(map[string]*Profile),
6970
Current: "",
71+
Previous: "",
7072
}
7173
}
7274

internal/config/config_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ func TestSaveAndLoadConfig(t *testing.T) {
174174
}
175175

176176
// Save config
177+
cfg.Previous = "work"
177178
err := cfg.SaveConfig(configFile)
178179
if err != nil {
179180
t.Fatalf("SaveConfig failed: %v", err)
@@ -211,6 +212,10 @@ func TestSaveAndLoadConfig(t *testing.T) {
211212
if len(p.URL) != 1 {
212213
t.Errorf("Expected 1 URL rewrite, got %d", len(p.URL))
213214
}
215+
216+
if loadedCfg.Previous != "work" {
217+
t.Errorf("Expected previous profile 'work', got '%s'", loadedCfg.Previous)
218+
}
214219
}
215220

216221
func TestLoadConfigNonExistent(t *testing.T) {

0 commit comments

Comments
 (0)