Skip to content

Instantly share code, notes, and snippets.

@scottrbaxter
Last active February 10, 2024 08:22
Show Gist options
  • Select an option

  • Save scottrbaxter/99ec088568f45e1e206bca02b85a2753 to your computer and use it in GitHub Desktop.

Select an option

Save scottrbaxter/99ec088568f45e1e206bca02b85a2753 to your computer and use it in GitHub Desktop.

Revisions

  1. scottrbaxter revised this gist May 18, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions op_session.sh
    Original file line number Diff line number Diff line change
    @@ -28,6 +28,7 @@ main(){
    signout )
    op signout
    unset OP_EXPIRATION OP_SESSION_"$OP_CLOUD_ACCOUNT"
    reset_timeout
    ;;
    update )
    op update;;
  2. scottrbaxter revised this gist May 18, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion op_session.sh
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,9 @@ main(){
    signin )
    op signin;;
    signout )
    op signout;;
    op signout
    unset OP_EXPIRATION OP_SESSION_"$OP_CLOUD_ACCOUNT"
    ;;
    update )
    op update;;
    * ) # active session required for everything else
  3. scottrbaxter revised this gist May 18, 2018. 1 changed file with 40 additions and 14 deletions.
    54 changes: 40 additions & 14 deletions op_session.sh
    Original file line number Diff line number Diff line change
    @@ -1,34 +1,60 @@
    #!/usr/bin/env bash
    # starts (or restarts) a 1password cli session and sets 30 minute countdown variable
    # use: OP_CLOUD_ACCOUNT="[your-account-name]" source /path/to/op_session.sh

    set -e
    # starts (or restarts) a 1password cli session, sets 30 minute countdown variable

    main(){
    # check if session is active
    if op get account &> /dev/null; then
    set_timeout
    else
    # use: OP_CLOUD_ACCOUNT="[your-account-name]" source /path/to/op_session.sh command
    # e.g.: OP_CLOUD_ACCOUNT="familyname" source ~/op_session.sh get account


    check_session(){
    # attempt sign in if session is not active
    if ! op get account &> /dev/null; then
    signin
    check_session
    fi
    # reset 30 min countdown
    export OP_EXPIRATION="$OP_EXPIRATION_TIME"
    }

    set_timeout(){
    main(){
    # directly pass inactive session args
    case "$*" in
    "" )
    op;;
    --help )
    op --help;;
    --version )
    op --version;;
    signin )
    op signin;;
    signout )
    op signout;;
    update )
    op update;;
    * ) # active session required for everything else
    check_session
    eval "op $*"
    reset_timeout
    ;;
    esac
    }

    reset_timeout(){
    # reset 30 min countdown
    OS_TYPE=$(uname -s)
    if [ "$OS_TYPE" = Darwin ]; then # MacOS
    OP_EXPIRATION_TIME=$(date -v +30M -u +%s)
    elif [ "$OS_TYPE" = Linux ]; then
    OP_EXPIRATION_TIME=$(date -d '+30 min' -u +%s)
    fi
    export OP_EXPIRATION="$OP_EXPIRATION_TIME"
    }

    signin(){
    token=$(op signin "$OP_CLOUD_ACCOUNT" | grep 'export' | awk -F\" '{print $2}')
    token=$(op signin "$OP_CLOUD_ACCOUNT" | \
    grep 'export' | \
    awk -F\" '{print $2}'
    )
    export OP_SESSION_"$OP_CLOUD_ACCOUNT"="$token"
    set_timeout
    }


    main
    main "$*"
  4. scottrbaxter revised this gist May 17, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion op_session.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env bash
    # starts (or restarts) a 1password cli session and sets 30 minute countdown variable
    # use: source ~/scripts/op_session.sh
    # use: OP_CLOUD_ACCOUNT="[your-account-name]" source /path/to/op_session.sh

    set -e

    @@ -16,6 +16,7 @@ main(){
    }

    set_timeout(){
    OS_TYPE=$(uname -s)
    if [ "$OS_TYPE" = Darwin ]; then # MacOS
    OP_EXPIRATION_TIME=$(date -v +30M -u +%s)
    elif [ "$OS_TYPE" = Linux ]; then
  5. scottrbaxter created this gist May 17, 2018.
    33 changes: 33 additions & 0 deletions op_session.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #!/usr/bin/env bash
    # starts (or restarts) a 1password cli session and sets 30 minute countdown variable
    # use: source ~/scripts/op_session.sh

    set -e

    main(){
    # check if session is active
    if op get account &> /dev/null; then
    set_timeout
    else
    signin
    fi
    # reset 30 min countdown
    export OP_EXPIRATION="$OP_EXPIRATION_TIME"
    }

    set_timeout(){
    if [ "$OS_TYPE" = Darwin ]; then # MacOS
    OP_EXPIRATION_TIME=$(date -v +30M -u +%s)
    elif [ "$OS_TYPE" = Linux ]; then
    OP_EXPIRATION_TIME=$(date -d '+30 min' -u +%s)
    fi
    }

    signin(){
    token=$(op signin "$OP_CLOUD_ACCOUNT" | grep 'export' | awk -F\" '{print $2}')
    export OP_SESSION_"$OP_CLOUD_ACCOUNT"="$token"
    set_timeout
    }


    main