Protect what matters – even after you're gone. Make a plan for your digital legacy today.
Forum Discussion
thecatfix
1 month agoDedicated Contributor
Amazing Command Line Workflow
This is my last post due to this janky editor. I had this in a nice format to read but due to the errors and time wasted its plain text
I spend most of my time in the terminal and struggle wit...
1P_Blake
Community Manager
1 month agoHey thecatfix! 👋
Thanks so much for taking the time to share this, and sorry you ran into trouble with the editor! It's definitely frustrating when you've got something all nicely formatted and it just doesn't cooperate.
I did my best to clean up the formatting on my end so it's easier to read, but I'll still pass that feedback along to our Community provider so we can look into what went wrong.
thecatfix
1 month agoDedicated Contributor
DUDE NO! YOU NEED TO CHANGE THAT
It is way to confusing.
#!/usr/bin/env bash
set -euo pipefail
# Parse arguments
COPY_MODE=false
QUERY=""
while [[ $# -gt 0 ]]; do
case $1 in
-c|--copy)
COPY_MODE=true
shift
;;
-h|--help)
echo "Usage: opsearch [OPTIONS] [QUERY]"
echo ""
echo "Options:"
echo " -c, --copy Copy selected field value to clipboard"
echo " -h, --help Show this help message"
echo ""
echo "Examples:"
echo " opsearch # Interactive search"
echo " opsearch github # Search for 'github' in titles"
echo " opsearch --copy # Copy mode (interactive)"
echo " opsearch --copy aws # Search 'aws' and copy result"
exit 0
;;
*)
QUERY="$1"
shift
;;
esac
done
# Function to copy to clipboard based on OS
copy_to_clipboard() {
local value="$1"
if command -v pbcopy &> /dev/null; then
# macOS
echo -n "$value" | pbcopy
echo "Copied to clipboard (macOS)"
elif command -v xclip &> /dev/null; then
# Linux with xclip
echo -n "$value" | xclip -selection clipboard
echo "Copied to clipboard (xclip)"
elif command -v wl-copy &> /dev/null; then
# Linux with wl-copy (Wayland)
echo -n "$value" | wl-copy
echo "Copied to clipboard (wl-copy)"
else
echo "Warning: No clipboard tool found (pbcopy, xclip, or wl-copy)" >&2
echo "Value: $value"
return 1
fi
}
# Select item via fzf (with optional query filter)
if [[ -n "$QUERY" ]]; then
# Search mode: filter items by query
item_id=$(op item list --format json | jq -r --arg q "$QUERY" '.[] | select(.title | ascii_downcase | contains($q | ascii_downcase)) | "\(.id)\t\(.title)\t[\(.vault.name)]\tcreated: \(.created_at[:10])\tmodified: \(.updated_at[:10])"' |
column -t -s $'\t' | fzf --header="Filter: $QUERY" | awk '{print $1}')
else
# Interactive mode: show all items
item_id=$(op item list --format json | jq -r '.[] | "\(.id)\t\(.title)\t[\(.vault.name)]\tcreated: \(.created_at[:10])\tmodified: \(.updated_at[:10])"' |
column -t -s $'\t' | fzf | awk '{print $1}')
fi
if [[ -z "$item_id" ]]; then
echo "No item selected." >&2
exit 1
fi
# Fetch full item JSON once
item_json=$(op item get "$item_id" --format json)
vault=$(echo "$item_json" | jq -r '.vault.name')
item=$(echo "$item_json" | jq -r '.title')
# Display all fields
echo ""
echo "=== $item [vault: $vault] ==="
echo ""
echo "$item_json" | jq -r '.fields[] | "\(.label): \(.value // .reference // "(empty)")"'
# In copy mode, skip the field selection and use fzf for field
echo ""
if [[ "$COPY_MODE" == true ]]; then
echo "=== Select field to copy ==="
else
echo "=== Select a field for reference commands ==="
fi
# Pick a field via fzf
field=$(echo "$item_json" | jq -r '.fields[].label' | fzf)
if [[ -z "$field" ]]; then
echo "No field selected." >&2
exit 1
fi
# Get the field value for copying
field_value=$(echo "$item_json" | jq -r --arg f "$field" '.fields[] | select(.label == $f) | .value // .reference // empty')
# Copy mode: copy and exit
if [[ "$COPY_MODE" == true ]]; then
if [[ -n "$field_value" && "$field_value" != "null" ]]; then
echo ""
copy_to_clipboard "$field_value"
echo "Field: $field"
else
echo "Error: Field '$field' has no value to copy" >&2
exit 1
fi
exit 0
fi
# Reference mode: show commands
ref="op://$vault/$item/$field"
echo ""
echo "Reference: $ref"
echo ""
echo "Commands:"
echo " op read \"$ref\""
echo " op run --env MYVAR=\"$ref\" -- <command>"
echo " op inject -i <template> -o <output> # use: {{ $ref }}"
echo ""
echo "Copy mode: opsearch --copy"
- 1P_Blake1 month ago
Community Manager
Fixed 🙂