Skip to content

Commit

Permalink
shell/fish: impl SpawnRunner
Browse files Browse the repository at this point in the history
  • Loading branch information
joshi4 committed Sep 17, 2024
1 parent 607b187 commit 928c1df
Showing 1 changed file with 45 additions and 1 deletion.
46 changes: 45 additions & 1 deletion shell/fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var fishTemplate, fishHistoryTemplate, fishRunTemplate *template.Template

func init() {
fishTemplate = template.Must(template.New("fish").Parse(fishBaseScript + fishRecordSetup))
fishRunTemplate = template.Must(template.New("fishRun").Parse(fishBaseScript + fishRunRunbookScript))
}

// Spawn starts a fish shell.
Expand Down Expand Up @@ -114,8 +115,51 @@ func (f *fish) SpawnHistoryExpander(ctx context.Context) (*exec.Cmd, error) {
return nil, nil
}

const fishRunRunbookScript = `
if not functions -q __savvy_run_pre_exec__
set_color red
echo " Your shell is not configured to use Savvy. Please run the following commands: "
set_color normal
echo
set_color red
echo "> echo 'savvy init fish | source' >> ~/.config/fish/config.fish"
echo "> source ~/.config/fish/config.fish"
set_color normal
exit 1
end
echo
echo "Type 'exit' or press 'ctrl+d' to stop running."
echo
`

func (f *fish) SpawnRunbookRunner(ctx context.Context, runbook *client.Runbook) (*exec.Cmd, error) {
return nil, nil
tmpDir := os.TempDir()
fishVendorConfDir := filepath.Join(tmpDir, "fish", "vendor_conf.d")
if err := os.MkdirAll(fishVendorConfDir, 0755); err != nil {
return nil, err
}
fishrc, err := os.CreateTemp(fishVendorConfDir, "savvy-fishrc-*.fish")
if err != nil {
return nil, err
}
defer fishrc.Close()

if err := fishRunTemplate.Execute(fishrc, f); err != nil {
return nil, err
}

dataDirs := os.Getenv("XDG_DATA_DIRS")
if dataDirs == "" {
dataDirs = fishVendorConfDir
} else {
dataDirs = strings.Join([]string{dataDirs, fishVendorConfDir}, ":")
}
cmd := exec.CommandContext(ctx, f.shellCmd)
cmd.Env = append(os.Environ(), "SAVVY_CONTEXT=run", fmt.Sprintf("XDG_DATA_DIRS=%s", dataDirs))
cmd.Env = append(cmd.Env, runbookRunMetadata(runbook, f)...)
cmd.WaitDelay = 500 * time.Millisecond
return cmd, nil
}

func (f *fish) DefaultStartingArrayIndex() int {
Expand Down

0 comments on commit 928c1df

Please sign in to comment.