Skip to content

Commit 97c9531

Browse files
committed
Factor out common functionality
Extract common functionality present in both implementations for interacting with the shared lib
1 parent 86c33b7 commit 97c9531

File tree

3 files changed

+140
-219
lines changed

3 files changed

+140
-219
lines changed

internal/shared_lib_core.go

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package internal
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"log"
8+
"os"
9+
"path"
10+
"runtime"
11+
)
12+
13+
// Request/Response mirror your Unix file (kept identical)
14+
type Request struct {
15+
Kind string `json:"kind"`
16+
AccountName string `json:"account_name"`
17+
Payload []byte `json:"payload"`
18+
}
19+
20+
type Response struct {
21+
Success bool `json:"success"`
22+
Payload []byte `json:"payload"`
23+
}
24+
25+
func (r Response) Error() string { return string(r.Payload) }
26+
27+
// find1PasswordLibPath returns the path to the 1Password shared library
28+
// (libop_sdk_ipc_client.dylib/.so/.dll) depending on OS.
29+
func find1PasswordLibPath() (string, error) {
30+
var locations []string
31+
32+
home, err := os.UserHomeDir()
33+
if err != nil {
34+
return "", err
35+
}
36+
37+
switch runtime.GOOS {
38+
case "darwin":
39+
locations = []string{
40+
"/Applications/1Password.app/Contents/Frameworks/libop_sdk_ipc_client.dylib",
41+
path.Join(home, "Applications/1Password.app/Contents/Frameworks/libop_sdk_ipc_client.dylib"),
42+
}
43+
44+
case "linux":
45+
locations = []string{
46+
"/usr/bin/1password/libop_sdk_ipc_client.so",
47+
"/opt/1Password/libop_sdk_ipc_client.so",
48+
"/snap/bin/1password/libop_sdk_ipc_client.so",
49+
}
50+
51+
case "windows":
52+
locations = []string{
53+
path.Join(home, `AppData\Local\1Password\op_sdk_ipc_client.dll`),
54+
`C:\Program Files\1Password\app\8\op_sdk_ipc_client.dll`,
55+
`C:\Program Files (x86)\1Password\app\8\op_sdk_ipc_client.dll`,
56+
path.Join(home, `AppData\Local\1Password\app\8\op_sdk_ipc_client.dll`),
57+
}
58+
59+
default:
60+
return "", fmt.Errorf("unsupported OS: %s", runtime.GOOS)
61+
}
62+
for _, libPath := range locations {
63+
if _, err := os.Stat(libPath); err == nil {
64+
return libPath, nil
65+
}
66+
}
67+
68+
return "", fmt.Errorf("1Password desktop application not found")
69+
}
70+
71+
func GetSharedLibCore(accountName string) (*CoreWrapper, error) {
72+
if coreLib == nil {
73+
libPath, err := find1PasswordLibPath()
74+
if err != nil {
75+
return nil, err
76+
}
77+
coreLib, err = loadCore(libPath)
78+
if err != nil {
79+
return nil, err
80+
}
81+
coreLib.accountName = accountName
82+
}
83+
84+
coreWrapper := CoreWrapper{InnerCore: coreLib}
85+
86+
return &coreWrapper, nil
87+
}
88+
89+
// InitClient creates a client instance in the current core module and returns its unique ID.
90+
func (slc *SharedLibCore) InitClient(ctx context.Context, config []byte) ([]byte, error) {
91+
const kind = "init_client"
92+
request := Request{
93+
Kind: kind,
94+
AccountName: slc.accountName,
95+
Payload: config,
96+
}
97+
98+
requestMarshaled, err := json.Marshal(request)
99+
if err != nil {
100+
return nil, err
101+
}
102+
res, err := slc.callSharedLibrary(requestMarshaled)
103+
if err != nil {
104+
return nil, err
105+
}
106+
107+
return res, nil
108+
}
109+
110+
// Invoke performs an SDK operation.
111+
func (slc *SharedLibCore) Invoke(ctx context.Context, invokeConfig []byte) ([]byte, error) {
112+
const kind = "invoke"
113+
request := Request{
114+
Kind: kind,
115+
AccountName: slc.accountName,
116+
Payload: invokeConfig,
117+
}
118+
119+
requestMarshaled, err := json.Marshal(request)
120+
if err != nil {
121+
return nil, err
122+
}
123+
124+
res, err := slc.callSharedLibrary(requestMarshaled)
125+
if err != nil {
126+
return nil, err
127+
}
128+
129+
return res, nil
130+
}
131+
132+
// ReleaseClient releases memory in the core associated with the given client ID.
133+
func (slc *SharedLibCore) ReleaseClient(clientID []byte) {
134+
_, err := slc.callSharedLibrary(clientID)
135+
if err != nil {
136+
log.Println("failed to release client")
137+
}
138+
}

internal/shared_lib_core_unix.go

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@
33
package internal
44

55
import (
6-
"context"
76
"encoding/json"
87
"errors"
98
"fmt"
10-
"log"
11-
"os"
12-
"path"
13-
"runtime"
149
"unsafe"
1510
)
1611

@@ -85,60 +80,6 @@ type SharedLibCore struct {
8580

8681
var coreLib *SharedLibCore
8782

88-
// find1PasswordLibPath returns the path to the 1Password shared library
89-
// (libop_sdk_ipc_client.dylib/.so/.dll) depending on OS.
90-
func find1PasswordLibPath() (string, error) {
91-
var locations []string
92-
93-
home, err := os.UserHomeDir()
94-
if err != nil {
95-
return "", err
96-
}
97-
98-
switch runtime.GOOS {
99-
case "darwin":
100-
locations = []string{
101-
"/Applications/1Password.app/Contents/Frameworks/libop_sdk_ipc_client.dylib",
102-
path.Join(home, "Applications/1Password.app/Contents/Frameworks/libop_sdk_ipc_client.dylib"),
103-
}
104-
105-
case "linux":
106-
locations = []string{
107-
"/usr/bin/1password/libop_sdk_ipc_client.so",
108-
"/opt/1Password/libop_sdk_ipc_client.so",
109-
"/snap/bin/1password/libop_sdk_ipc_client.so",
110-
}
111-
112-
default:
113-
return "", fmt.Errorf("unsupported OS: %s", runtime.GOOS)
114-
}
115-
for _, libPath := range locations {
116-
if _, err := os.Stat(libPath); err == nil {
117-
return libPath, nil
118-
}
119-
}
120-
121-
return "", fmt.Errorf("1Password desktop application not found")
122-
}
123-
124-
func GetSharedLibCore(accountName string) (*CoreWrapper, error) {
125-
if coreLib == nil {
126-
path, err := find1PasswordLibPath()
127-
if err != nil {
128-
return nil, err
129-
}
130-
coreLib, err = loadCore(path)
131-
if err != nil {
132-
return nil, err
133-
}
134-
coreLib.accountName = accountName
135-
}
136-
137-
coreWrapper := CoreWrapper{InnerCore: coreLib}
138-
139-
return &coreWrapper, nil
140-
}
141-
14283
func loadCore(path string) (*SharedLibCore, error) {
14384
cPath := C.CString(path)
14485
defer C.free(unsafe.Pointer(cPath))
@@ -173,56 +114,6 @@ func loadCore(path string) (*SharedLibCore, error) {
173114
}, nil
174115
}
175116

176-
// InitClient creates a client instance in the current core module and returns its unique ID.
177-
func (slc *SharedLibCore) InitClient(ctx context.Context, config []byte) ([]byte, error) {
178-
const kind = "init_client"
179-
request := Request{
180-
Kind: kind,
181-
AccountName: slc.accountName,
182-
Payload: config,
183-
}
184-
185-
requestMarshaled, err := json.Marshal(request)
186-
if err != nil {
187-
return nil, err
188-
}
189-
res, err := slc.callSharedLibrary(requestMarshaled)
190-
if err != nil {
191-
return nil, err
192-
}
193-
194-
return res, nil
195-
}
196-
197-
func (slc *SharedLibCore) Invoke(ctx context.Context, invokeConfig []byte) ([]byte, error) {
198-
const kind = "invoke"
199-
request := Request{
200-
Kind: kind,
201-
AccountName: slc.accountName,
202-
Payload: invokeConfig,
203-
}
204-
205-
requestMarshaled, err := json.Marshal(request)
206-
if err != nil {
207-
return nil, err
208-
}
209-
210-
res, err := slc.callSharedLibrary(requestMarshaled)
211-
if err != nil {
212-
return nil, err
213-
}
214-
215-
return res, nil
216-
}
217-
218-
// ReleaseClient releases memory in the core associated with the given client ID.
219-
func (slc *SharedLibCore) ReleaseClient(clientID []byte) {
220-
_, err := slc.callSharedLibrary(clientID)
221-
if err != nil {
222-
log.Println("failed to release client")
223-
}
224-
}
225-
226117
func (slc *SharedLibCore) callSharedLibrary(input []byte) ([]byte, error) {
227118
if len(input) == 0 {
228119
return nil, errors.New("internal: empty input")
@@ -261,18 +152,3 @@ func (slc *SharedLibCore) callSharedLibrary(input []byte) ([]byte, error) {
261152
return nil, response
262153
}
263154
}
264-
265-
type Request struct {
266-
Kind string `json:"kind"`
267-
AccountName string `json:"account_name"`
268-
Payload []byte `json:"payload"`
269-
}
270-
271-
type Response struct {
272-
Success bool `json:"success"`
273-
Payload []byte `json:"payload"`
274-
}
275-
276-
func (r Response) Error() string {
277-
return string(r.Payload)
278-
}

0 commit comments

Comments
 (0)