I am trying to write a small Go wrapper application around the 1Password CLI executable op like so:
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 so:
#!/usr/bin/env python
import subprocess
subprocess.run(["op", "item", "list"])
...I get the output just fine.
Interestingly enough though, when I call the Python script (named op.py) from the Go app, it works fine (modified Go app shown below):
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 being printed by the Go app and not by the Python script because if I remove the fmt.Printf(...), nothing gets printed.
So to summarize:
- Go ->
op: does not work - Python (
./op.py) ->op: works fine - Go -> Python (
./op.py) ->op: works fine
I had the same problem accessing
opfrom apyenvvirtual environment. I figured that the problem is that the pyenv'spythonexecutable is owned by the user (me). Changing the ownership to root:root of the python interpreter and the directory that it lives in actually helped. Not sure what is going on behind the scenes.Here are the steps (I use the
--copiesto create a virtual environment, so it is not using symlinks - as symlinks would point back to root owned files):Bottomline: Change the ownership of your executable (and directory it lives in) that spawns the
opsubprocess toroot:rootAlso please see this thread on 1Password that look like the same issue:
https://1password.community/discussion/135768/calling-the-cli-from-a-go-program