Home  >  Article  >  Backend Development  >  Unable to run 'op' from Go application

Unable to run 'op' from Go application

王林
王林forward
2024-02-10 08:42:08667browse

无法从 Go 应用程序运行“op”

php Xiaobian Yuzai may encounter a common problem when developing applications using the Go language, that is, the "op" cannot be run from the Go application. This issue may cause the application to not function properly or to display an error message. There are many ways to solve this problem, which can be solved by checking whether the corresponding package is correctly imported in the code, or by reinstalling the relevant dependencies. Also, try other tools or methods to deal with the problem. I hope the introduction in this article can help developers who encounter this problem.

Question content

I'm trying to write a small go wrapper application around the 1password cli executable op like this:

package main

import (
    "fmt"
    "log"
    "os/exec"
)

func main() {
    op := exec.command("op", "item", "list")
    out, err := op.output()
    if e, ok := err.(*exec.exiterror); ok {
        log.fatal(e, ": ", string(e.stderr))
    }
    fmt.println(string(out))
}

However, I keep getting the following error:

2023/04/13 09:48:51 exit status 1: [error] 2023/04/13 09:48:51 error initializing client: connecting to desktop app: read: connection reset, make sure the cli is correctly installed and connect with 1password cli is enabled in the 1password app

But when I do the same thing from a python script like this:

#!/usr/bin/env python

import subprocess

subprocess.run(["op", "item", "list"])

...I get nice output.

Interestingly, when I call the python script (named op.py) from the go application, it works fine (the modified go application looks like this):

package main

import (
    "fmt"
    "log"
    "os/exec"
)

func main() {
    op := exec.Command("/usr/bin/python3.11", "./op.py")
    //op := exec.Command("op", "item", "list")
    out, err := op.Output()
    if e, ok := err.(*exec.ExitError); ok {
        log.Fatal(e, ": ", string(e.Stderr))
    }
    fmt.Println(string(out))
}

I can test that it is printed by the go application and not the python script because if I remove fmt.printf(...) nothing is printed.

in conclusion:

  • go -> op: does not work
  • python(./op.py)-> op: working fine
  • go -> python (./op.py) -> op: working fine

Solution

I had the same problem when accessing op from the pyenv virtual environment. I think the problem is that the pyenv 's python executable is owned by the user (me). Changing ownership to root:the root of the python interpreter and the directory it resides in actually helps. Not sure what's going on behind the scenes.

Here are the steps (I used --copies to create the virtual environment so it doesn't use symlinks - since symlinks would point to root-owned files):

$ python -m venv --copies .venv
$ .venv/bin/python -c "import subprocess; subprocess.call(['op', 'signin'])"
[ERROR] 2023/05/16 16:01:18 connecting to desktop app: read: connection reset, make sure the CLI is correctly installed and Connect with 1Password CLI is enabled in the 1Password app
# chown -R root:root .venv/
$ .venv/bin/python -c "import subprocess; subprocess.call(['op', 'signin'])"
 Select account  [Use arrows to move, type to filter]

Bottom line: Change the ownership of the executable file that spawned the op child process (and the directory it resides in) to root:root

Also see this post on 1password which appears to be the same issue:

https://1password.community/discussion/135768/call-cli-from-a-go-program-

The above is the detailed content of Unable to run 'op' from Go application. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Adapter pattern in golangNext article:Adapter pattern in golang