Skip to content

Commit 33c72f0

Browse files
feat(dsc): add skipExistingInit
resolves #7560 Co-authored-by: JanDeDobbeleer <2492783+JanDeDobbeleer@users.noreply.github.com>
1 parent 771c62f commit 33c72f0

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

src/shell/dsc.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ func DSC() *dsc.Resource[*Shell] {
2828
}
2929

3030
type Shell struct {
31-
Command string `json:"command,omitempty" jsonschema:"title=Command,description=The oh-my-posh init command to run"`
32-
Name string `json:"name,omitempty" jsonschema:"title=Shell name,description=The name of the shell"`
31+
Command string `json:"command,omitempty" jsonschema:"title=Command,description=The oh-my-posh init command to run"`
32+
Name string `json:"name,omitempty" jsonschema:"title=Shell name,description=The name of the shell"`
33+
SkipExistingInit bool `json:"skipExistingInit,omitempty" jsonschema:"title=Skip existing init,description=Treat any existing oh-my-posh init line as compliant instead of rewriting it"` //nolint:lll
3334
}
3435

3536
func (s *Shell) Equal(shell *Shell) bool {
@@ -149,6 +150,11 @@ func (s *Shell) updateShellConfig(content string) (string, bool) {
149150
initLineStr := lines[initLinePos]
150151
shellCommand := s.shellCommand()
151152

153+
if s.SkipExistingInit {
154+
log.Debug("existing oh-my-posh init line found, skipping update")
155+
return content, false
156+
}
157+
152158
// validate if we have the same command
153159
if strings.Contains(initLineStr, shellCommand) {
154160
log.Debug("oh-my-posh already correctly configured")

src/shell/dsc_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,28 @@ func TestUpdateShellConfig(t *testing.T) {
265265
expectedOutput: "if command -v oh-my-posh >/dev/null 2>&1; then\n \t eval \"$(oh-my-posh init bash --config theme.json)\"\nfi\n",
266266
expectedUpdate: true,
267267
},
268+
{
269+
name: "preserve existing initialization when configured",
270+
shell: &Shell{
271+
Name: "pwsh",
272+
Command: "oh-my-posh init pwsh",
273+
SkipExistingInit: true,
274+
},
275+
content: "oh-my-posh init pwsh --config \"$HOME/.config/oh-my-posh/custom.omp.json\" | Invoke-Expression\n",
276+
expectedOutput: "oh-my-posh init pwsh --config \"$HOME/.config/oh-my-posh/custom.omp.json\" | Invoke-Expression\n",
277+
expectedUpdate: false,
278+
},
279+
{
280+
name: "add initialization when skip existing is enabled but no init line exists",
281+
shell: &Shell{
282+
Name: "pwsh",
283+
Command: "oh-my-posh init pwsh",
284+
SkipExistingInit: true,
285+
},
286+
content: "$env:PATH += ';C:\\Program Files'\n",
287+
expectedOutput: "$env:PATH += ';C:\\Program Files'\noh-my-posh init pwsh | Invoke-Expression\n",
288+
expectedUpdate: true,
289+
},
268290
}
269291

270292
for _, tt := range tests {

website/docs/dsc.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ resources:
244244
states:
245245
- name: pwsh
246246
command: oh-my-posh init pwsh --config ~/mytheme.omp.json
247+
skipExistingInit: true
247248
```
248249
249250
Apply with:
@@ -391,6 +392,13 @@ Manages shell initialization.
391392

392393
**Properties:**
393394

395+
- `states` (array): List of shell initialization states
396+
- `name` (string): Shell name
397+
- `command` (string): `oh-my-posh init` command to apply
398+
- `skipExistingInit` (boolean): When `true`, any existing `oh-my-posh init` line is treated as compliant and left unchanged
399+
400+
**Properties:**
401+
394402
- `states` (array): List of shell configurations
395403
- `name` (string): Shell name (`bash`, `zsh`, `pwsh`, `fish`, etc.)
396404
- `command` (string): Oh My Posh initialization command

0 commit comments

Comments
 (0)