Example : Create Secure Note from piped data

This example won't work on the initial build we sent out, but should work on the next one.

#!/bin/bash

CONTENTS=`cat -`

TITLE=$1

SECURENOTEITEM=$(jq -n --arg notesPlain "$CONTENTS" '{notesPlain: $notesPlain, sections:[], fields:[]}')

S=$(./op login agilebits) &&
BASE64ENCODEDITEM=$(echo $SECURENOTEITEM | base64) &&
./op create $S personal "secure note" "$TITLE" $BASE64ENCODEDITEM

Using that bash script, I'm able to do stuff like this:

$ cat sample.txt | ./createSecureNote.sh "Sample Text File"

After getting prompted for my master password, it'll then create a secure note with the text I've piped into it. There's probably a better way to get the piped data than I've got there, but this seems to do the trick.

Comments

  • DanielP
    DanielP
    1Password Alumni
    edited February 2017

    @rickfillion

    I haven't tried this in the CLI so the code might not work, but wouldn't it be better to just pass sample.txt (which, if I understood this correctly, is the file with the contents of the secure note, right?) as another argument to ./createSecureNote? So, like this:

    ./createSecureNote.sh "Title" sample.txt

    Inside the bash file, you could assign the contents of the text file with this:

    CONTENTS = $( cat $2 | base64)

    And then you should be able to pass this value to the ./op create command like this:

    /op create $S personal "secure note" "$TITLE" CONTENTS

  • @danielp it would certainly be easier. :)

    I chose to do it this way so that I could either pass file contents like I did, or if I wanted I could just pipe output from another process. So for example ls -al | ./createSecureNote.sh "Directory Listing Example". It's a little contrived, but this way I don't need to output to an intermediary file and I can just throw data at the script.

  • DanielP
    DanielP
    1Password Alumni

    Oh that makes perfect sense actually, I had not thought about this :)

  • jpgoldberg
    jpgoldberg
    1Password Alumni
    edited February 2017

    I'm tempted to replace the `cat -` with a cat.

    CONTENTS=<<'END'
      /\___/\
     |       |
    _  *   *  _
    -   /_\   -
        ---
    END
    
This discussion has been closed.