Skip to content

Commit

Permalink
setup/fish: vendor code in fish is loaded before user configured
Browse files Browse the repository at this point in the history
The usual way I've been setting up and checking record/run setup won't
work here.

Fish checks were loaded as vendor code and setup is part of the user's
config.

Therefore, in fish these checks will always fail since they are run
before user config.

So, we move our checks into user code.
  • Loading branch information
joshi4 committed Sep 17, 2024
1 parent 8acca97 commit c6b8567
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 83 deletions.
51 changes: 26 additions & 25 deletions cmd/setup/savvy.fish
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ function __savvy_record_prompt --description "Modify prompt for Savvy recording"
if test "$SAVVY_CONTEXT" = "record"
and not string match -q '*recording*' "$fish_prompt"
echo -n $original_prompt
echo -n (set_color green)"recording"(set_color normal)" 😎 "
echo -n (set_color green)"recording"(set_color normal)" 😎>"
echo -n " "
else
echo -n $original_prompt
end
Expand Down Expand Up @@ -82,8 +83,8 @@ function __savvy_run_prompt --description "Modify prompt for Savvy run"

echo -n $original_prompt
if test "$SAVVY_CONTEXT" = "run"
echo -n (set_color green)"savvy run" (set_color normal)
echo -n "$SAVVY_RUN_CURR"
echo -n (set_color --bold)"[ctrl-n: get next step]"(set_color normal)
echo -n (set_color green)"(savvy run" (set_color normal) "$SAVVY_RUN_CURR) "
end

if test "$SAVVY_CONTEXT" = "run"
Expand Down Expand Up @@ -132,21 +133,12 @@ function __savvy_record_pre_exec__ --on-event fish_preexec
return
end

# $argv[2] is the full command line in Fish
set -l cmd $argv[1]

# Clear step_id
set -g step_id ""

if test "$SAVVY_CONTEXT" = "record"
# Get the current prompt
#set -l prompt (fish_prompt)

# Remove color codes and other formatting from the prompt
#set -l clean_prompt (string replace -ra '\e\[[^m]*m' "" -- $prompt)

# Send command to savvy and get step_id
#savvy send --prompt="$clean_prompt" $cmd
set -g step_id (
env SAVVY_SOCKET_PATH=$SAVVY_INPUT_FILE \
savvy send $cmd
Expand All @@ -155,26 +147,27 @@ function __savvy_record_pre_exec__ --on-event fish_preexec
end


function __savvy_run_completion__ --on-event fish_prompt
if not test "$SAVVY_CONTEXT" = "run"
function check_savvy_record_setup
if not test "$SAVVY_CONTEXT" = "record"
return
end

set -l run_cmd (savvy internal current)
set -l cmd (commandline -o)

if test -z "$cmd"
# Completions for empty command line
commandline -i $run_cmd
return
if not functions -q __savvy_record_pre_exec__
set_color red
echo "Your recording 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" set_color normal
set_color red echo "> source ~/.config/fish/config.fish" set_color normal
echo
exit 1
end
end

check_savvy_record_setup


# Trigger completion on empty command line
# Refer to shell/fish.go for triggering completion
function __savvy_run_completion_old__ --description "Complete the current command in a runbook"
function __savvy_run_completion__
if not test "$SAVVY_CONTEXT" = "run"
return
end
Expand All @@ -184,8 +177,16 @@ function __savvy_run_completion_old__ --description "Complete the current comman

if test -z "$cmd"
# Completions for empty command line
commandline -i $run_cmd
commandline --replace $run_cmd
return
end
end

function trigger_run_autocomplete --on-event fish_complete_command
if not test "$SAVVY_CONTEXT" = "run"
return
end
bind \cn '__savvy_run_completion__'
end

trigger_run_autocomplete
62 changes: 4 additions & 58 deletions shell/fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,43 +36,12 @@ switch "$OSTYPE"
end
set -g SAVVY_INPUT_FILE {{.SocketPath}}
## Fish doesn't use the same startup files as Bash, but we can approximate the behavior
#if status is-login
# if test -f "/etc/fish/config.fish"
# source "/etc/fish/config.fish"
# end
# if test -f "$HOME/.config/fish/config.fish"
# source "$HOME/.config/fish/config.fish"
# end
#else
# if test -f "/etc/fish/conf.d/login.fish"
# source "/etc/fish/conf.d/login.fish"
# end
# if test -f "$HOME/.config/fish/conf.d/login.fish"
# source "$HOME/.config/fish/conf.d/login.fish"
# end
#end
`
const fishRecordSetup = `
if not functions -q __savvy_record_pre_exec__
set_color red
echo -n "Your recording 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" set_color normal
set_color red echo "> source ~/.config/fish/config.fish" set_color normal
exit 1
end
echo
echo "Type 'exit' or press 'ctrl+d' to stop recording."
`

var fishTemplate, fishHistoryTemplate, fishRunTemplate *template.Template
var fishTemplate *template.Template

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

// Spawn starts a fish shell.
Expand Down Expand Up @@ -100,7 +69,7 @@ func (f *fish) Spawn(ctx context.Context) (*exec.Cmd, error) {
if dataDirs == "" {
dataDirs = tmpDir
} else {
dataDirs = strings.Join([]string{dataDirs, fishVendorConfDir}, ":")
dataDirs = strings.Join([]string{dataDirs, tmpDir}, ":")
}
cmd := exec.CommandContext(ctx, f.shellCmd)
cmd.Env = append(os.Environ(), "SAVVY_CONTEXT=record", fmt.Sprintf("XDG_DATA_DIRS=%s", dataDirs))
Expand All @@ -116,29 +85,6 @@ func (f *fish) SpawnHistoryExpander(ctx context.Context) (*exec.Cmd, error) {
return nil, nil
}

const fishRunRunbookScript = `
if not functions -q __savvy_run_pre_exec__
or not functions -q __savvy_run_completion__
set_color red
echo -n " Your shell is not configured to use Savvy. Please run the following commands: "
set_color normal
set_color red
echo
echo "> echo 'savvy init fish | source' >> ~/.config/fish/config.fish"
echo "> source ~/.config/fish/config.fish"
set_color normal
exit 1
end
echo "HERE!!!"
bind \cn '__savvy_run_completion__ "__savvy_run_completion__"'
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) {
tmpDir, err := os.MkdirTemp("", "savvy-fish-*")
if err != nil {
Expand All @@ -154,7 +100,7 @@ func (f *fish) SpawnRunbookRunner(ctx context.Context, runbook *client.Runbook)
}
defer fishrc.Close()

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

Expand Down

0 comments on commit c6b8567

Please sign in to comment.