CLI still has a bug when running "op create" programmatically

mifeinstein
mifeinstein
Community Member

I am running "op create" programmatically and I get an error saying

[ERROR] 2023/12/06 11:47:57 invalid JSON in piped input

This was reported before here, and marked as fixed, but I am using version 2.23.0 and I can still experience this.

Here's my Dart code:

const itemName = 'retrievable generated password';
final passwordCreationResult = processManager.runSync([
'op',
'item',
'create',
'--title="$itemName"',
'--category=password',
'--generate-password',
]);
print(passwordCreationResult.stdout.toString());
print(passwordCreationResult.stderr.toString());


1Password Version: Not Provided
Extension Version: Not Provided
OS Version: Not Provided
Browser: Not Provided

Comments

  • retos
    retos
    Community Member

    I can confirm on version 2.24.0 on OSX

    when I call on my command line

    op item create --category=login --vault=adrastea \
      --generate-password=64,letters,digits --title=postgres/nfcbox-preview
    

    => item will be created successfully

    when I call the same command programmatically from within python I get

      File "/Users/REDACTED/.pyenv/versions/3.10.6/lib/python3.10/site-packages/exec_utils/exec_utils.py", line 49, in wait
        raise ExecStrictError("error while executing %s,\nstdout: %s\nstderr: %s\n" % (
    exec_utils.exec_strict_error.ExecStrictError: error while executing ['op', 'item', 'create', '--category=login', '--vault=adrastea', '--generate-password=64,letters,digits', '--title=postgres/nfcbox-preview'],
    stdout: 
    stderr: [ERROR] 2023/12/16 13:59:02 invalid JSON in piped input
    

    I assume you find no PTY and assume STDIN mode.

  • retos
    retos
    Community Member

    Ah this can even easier be reproduced:

    this works:

    op item create --category=login
    

    this does NOT work:

    echo 'op item create --category=login' | bash 
    
  • dansimau
    dansimau
    Community Member

    I am experiencing this as well. It happens when my bash script is accepting input from stdin.

    Workaround is to close stdin, e.g.:

    #!/bin/bash
    0<&-    # Close stdin when done with it
    op item create --category=login </dev/null
    

    or more simply:

    #!/bin/bash
    op item create --category=login </dev/null
    
  • cdeliens
    cdeliens
    Community Member

    Thanks @dansimau for the workaround!

  • eM_Jay_Be
    eM_Jay_Be
    Community Member

    I am also experiencing a similar issue with a project. It's not quite the same, mine involves the QProcess class in a Qt/QML app. All other commands I use work fine, including delete. Tried adding closeWriteChannel() as it seemed similar to above workarounds but to no avail. Haven't tested with edit yet.

  • 1passwordMember
    1passwordMember
    Community Member

    I've encountered a similar issue while attempting to programmatically create new entries using the 'op item create' command in the 1Password CLI version 2.27.0. I'm coding the application in Java. Facing similar challenges as others, depending on how I invoke the 'op item create' command, it either times out or returns an error message stating 'invalid JSON in piped input'. I've tested on both Windows and Linux, spending an entire day troubleshooting. Eventually, I found a solution to properly use the 'op item create' command in Java.

    I had to redirect the input to null output using ProcessBuilder:

    ProcessBuilder processBuilder = new ProcessBuilder("op", "item", "create", "--category", "login", "--dry-run", "--format", "json");
    Map<String, String> env = processBuilder.environment();
    env.put("OP_SERVICE_ACCOUNT_TOKEN", "xxxx");
    processBuilder.redirectInput(ProcessBuilder.Redirect.from(new File(System.getProperty("os.name").startsWith("Windows") ? "NUL" : "/dev/null")));
    Process proc = processBuilder.start();
    

    This resolved the issue for me.

This discussion has been closed.