Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 3ebaa66

Browse files
authored
Merge pull request #378 from secrethub/fix/migration-tool-op-config-lookup
Fix op config dir lookup
2 parents 5060d8f + 69a754c commit 3ebaa66

1 file changed

Lines changed: 42 additions & 4 deletions

File tree

internals/onepassword/onepassword.go

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,52 @@ func EnsureSignedIn() error {
100100
return fmt.Errorf("OP_SESSION environment variable not found, run `eval $(op signin)` to set one")
101101
}
102102

103+
func opConfigDirPath() (string, error) {
104+
xdgConfigHome, _ := os.LookupEnv("XDG_CONFIG_HOME")
105+
home, _ := homedir.Dir()
106+
107+
// Inspect possible config directories in reverse order of priority.
108+
// This code has been taken from the op cli's source code.
109+
configDirPaths := []string{}
110+
if home != "" {
111+
// Legacy home
112+
configDirPaths = append(configDirPaths, filepath.Join(home, ".op"))
113+
}
114+
if xdgConfigHome != "" {
115+
// Legacy xdg
116+
configDirPaths = append(configDirPaths, filepath.Join(xdgConfigHome, ".op"))
117+
}
118+
if home != "" {
119+
// New home
120+
configDirPaths = append(configDirPaths, filepath.Join(home, ".config", "op"))
121+
}
122+
if xdgConfigHome != "" {
123+
// New xdg
124+
configDirPaths = append(configDirPaths, filepath.Join(xdgConfigHome, "op"))
125+
}
126+
127+
for _, configDir := range configDirPaths {
128+
fileInfo, err := os.Stat(configDir)
129+
if err == nil && fileInfo.IsDir() {
130+
return configDir, nil
131+
}
132+
}
133+
134+
// If we reach this point then none of those directories exist (op is executed
135+
// for the first time). Default to the last entry in the list.
136+
if len(configDirPaths) > 0 {
137+
return configDirPaths[len(configDirPaths)-1], nil
138+
}
139+
140+
return "", fmt.Errorf("unable to determine location of config directory")
141+
}
142+
103143
func GetSignInAddress() (string, error) {
104-
home, err := homedir.Dir()
144+
path, err := opConfigDirPath()
105145
if err != nil {
106146
return "", err
107147
}
108-
109-
path := filepath.Join(home, ".op", "config")
110-
bytes, err := ioutil.ReadFile(path)
148+
bytes, err := ioutil.ReadFile(filepath.Join(path, "config"))
111149
if err != nil {
112150
return "", fmt.Errorf("could not read 1password config file at %s", path)
113151
}

0 commit comments

Comments
 (0)