Skip to content

Commit

Permalink
Override system command not found
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffaluff committed Oct 30, 2024
1 parent f3a1846 commit 7d3fb11
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ chars = "\ue002"
key = "]"
mods = "Control"

[shell]
[terminal.shell]
args = ["{{ '-NoLogo' if ansible_system == 'Win32NT' else '--login' }}"]
program = "{{ user_shell }}"
{% if ansible_system == "Darwin" %}
Expand Down
42 changes: 27 additions & 15 deletions ansible_collections/scruffaluff/bootware/roles/bash/files/bashrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@
# For more information, visit
# https://www.gnu.org/software/bash/manual/html_node/Bash-Startup-Files.html.

# Convenience variables.
#
# Do not use long form flags for uname. They are not supported on MacOS. Command
# "(brew --prefix)" will give the incorrect path when sourced on Apple silicon
# and running under an Rosetta 2 emulated terminal.
# Public convenience interactive functions.

# Override system implementation of command not found.
#
# Flags:
# -d: Check if path is a directory.
# -s: Show operating system kernel name.
if [[ -d '/opt/homebrew' ]]; then
_brew_prefix='/opt/homebrew'
else
_brew_prefix='/usr/local'
fi
_os="$(uname -s)"
_tty="$([[ "$-" =~ 'i' ]] && echo 'true' || echo '')"
# Some system implementations will perform a long lookup to see if a package
# provides the command.
command_not_found_handle() {
echo "Error: command '${1}' not found" >&2
}

# Public convenience script functions.

# Prepend existing directories that are not in the system path.
#
Expand Down Expand Up @@ -49,6 +44,23 @@ source_files() {
done
}

# Private convenience variables.
#
# Do not use long form flags for uname. They are not supported on MacOS. Command
# "(brew --prefix)" will give the incorrect path when sourced on Apple silicon
# and running under an Rosetta 2 emulated terminal.
#
# Flags:
# -d: Check if path is a directory.
# -s: Show operating system kernel name.
if [[ -d '/opt/homebrew' ]]; then
_brew_prefix='/opt/homebrew'
else
_brew_prefix='/usr/local'
fi
_os="$(uname -s)"
_tty="$([[ "$-" =~ 'i' ]] && echo 'true' || echo '')"

# Shell settings.

# Add alias for remove by force.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,30 +94,6 @@ function _ssh_session
end
end

# Source shell files if they exist.
#
# Flags:
# -f: Check if file exists and is a regular file.
function _source_files
for inode in $argv
if test -f $inode
source $inode
end
end
end

# Source Bash files if they exist.
#
# Flags:
# -f: Check if file exists and is a regular file.
function _source_bash_files
for inode in $argv
if test -f $inode
bass source $inode
end
end
end

# Public convenience interactive functions.

# Open Fish history file with default editor.
Expand All @@ -130,6 +106,14 @@ function edit-history
end
end

# Override system implementation of command not found.
#
# Some system implementations will perform a long lookup to see if a package
# provides the command.
function fish_command_not_found
echo "Error: command '$argv[1]' not found" >&2
end

# Public convenience script functions.

# Prepend existing directories that are not in the system path.
Expand All @@ -148,6 +132,30 @@ function prepend_paths
end
end

# Source Bash files if they exist.
#
# Flags:
# -f: Check if file exists and is a regular file.
function source_bash_files
for inode in $argv
if test -f $inode
bass source $inode
end
end
end

# Source shell files if they exist.
#
# Flags:
# -f: Check if file exists and is a regular file.
function source_files
for inode in $argv
if test -f $inode
source $inode
end
end
end

# Private convenience variables.
#
# Do not use long form flags for uname. They are not supported on MacOS. Command
Expand Down Expand Up @@ -522,6 +530,6 @@ end
# Flags:
# -q: Only check for exit status by supressing output.
if type -q bass
_source_bash_files "$HOME/.env" "$HOME/.secrets"
source_bash_files "$HOME/.env" "$HOME/.secrets"
end
_source_files "$HOME/.env.fish" "$HOME/.secrets.fish"
source_files "$HOME/.env.fish" "$HOME/.secrets.fish"
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ node_applications:
- lean-language-server
- live-server
- playwright
- pnpm
- prettier
- speed-test
- speedscope
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
become_user: "{{ user_id }}"
# Command always marks application as changed.
changed_when: true
loop: "{{ node_applications }}"
loop: "{{ ['pnpm'] + node_applications }}"
when: >-
(ansible_pkg_mgr == "apk" or ansible_system == "FreeBSD") and user_id !=
"root"
Expand All @@ -141,7 +141,7 @@
become_user: "{{ user_id }}"
# Command always marks application as changed.
changed_when: true
loop: "{{ node_applications }}"
loop: "{{ ['pnpm'] + node_applications }}"
when: >-
ansible_system not in ["FreeBSD", "Win32NT"] and user_id != "root" and
system_libc != "musl"
Expand All @@ -156,6 +156,56 @@
loop: "{{ node_applications }}"
when: ansible_system == "Win32NT"

- name: Create user shell completion directories for Unix
ansible.builtin.file:
group: "{{ group_id }}"
mode: "755"
owner: "{{ user_id }}"
path: "{{ user_home }}/{{ item }}"
state: directory
become: true
loop:
- .local/share/bash-completion/completions
- .config/fish/completions
when: ansible_system != "Win32NT" and user_id != "root"

- name: Generate PNPM user shell completions for Alpine and FreeBSD
ansible.builtin.shell:
cmd: |
source {{ user_home }}/.bashrc
pnpm completion {{ item.shell }} > {{ user_home }}/{{ item.path }}
executable: "{{ bash_executable }}"
become: true
become_user: "{{ user_id }}"
changed_when: true
loop:
- path: .local/share/bash-completion/completions/pnpm
shell: bash
- path: .config/fish/completions/pnpm.fish
shell: fish
when: >-
(ansible_pkg_mgr == "apk" or ansible_system == "FreeBSD") and user_id !=
"root"
- name: Generate PNPM user shell completions for Unix
ansible.builtin.shell:
cmd: |
source {{ user_home }}/.bashrc
nvm use {{ node_versions[-1] }}
pnpm completion {{ item.shell }} > {{ user_home }}/{{ item.path }}
executable: "{{ bash_executable }}"
become: true
become_user: "{{ user_id }}"
changed_when: true
loop:
- path: .local/share/bash-completion/completions/pnpm
shell: bash
- path: .config/fish/completions/pnpm.fish
shell: fish
when: >-
ansible_system not in ["FreeBSD", "Win32NT"] and user_id != "root" and
system_libc != "musl"
- name: Create Fish functions directory for Unix
ansible.builtin.file:
group: "{{ group_id }}"
Expand Down

0 comments on commit 7d3fb11

Please sign in to comment.