Last active
February 28, 2025 13:25
-
-
Save bxb100/9382f3022190b4f45dac3be39de9615b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createClient } from 'npm:@1password/sdk'; | |
const decoder = new TextDecoder(); | |
const runCli = async (args: string[] | string) => { | |
if (typeof args === 'string') { | |
args = [args] | |
} | |
const command = new Deno.Command("op", { | |
args: [...args], | |
}); | |
const { code, stdout, stderr } = await command.output(); | |
if (code != 0) { | |
return decoder.decode(stderr).trim(); | |
} else { | |
return decoder.decode(stdout).trim(); | |
} | |
}; | |
const getSdkVersion = async () => { | |
const command = new Deno.Command("deno", { | |
args: ["info", "@1password/sdk", "--json"] | |
}) | |
const {code, stdout, stderr} = await command.output(); | |
if (code !== 0) { | |
return decoder.decode(stderr).trim(); | |
} | |
const json = JSON.parse(decoder.decode(stdout)); | |
return json.roots[0].split("@")[2]; | |
} | |
console.log(`CLI version`, await runCli("--version")); | |
console.log(`SDK version`, await getSdkVersion()); | |
// Creates an authenticated client. | |
const client = await createClient({ | |
auth: Deno.env.get("OP_SERVICE_ACCOUNT_TOKEN")!, | |
// Set the following to your own integration name and version. | |
integrationName: "My 1Password Integration", | |
integrationVersion: "v1.0.0", | |
}); | |
const loadSecret = async (secret: string) => { | |
const cliRes = await runCli(["read", secret]); | |
let sdkRes; | |
try { | |
sdkRes = await client.secrets.resolve(secret); | |
} catch (e) { | |
sdkRes = (e as Error).toString(); | |
} | |
console.group(); | |
console.info(`${secret} %c${sdkRes} %c${cliRes}`, "color:green", "color:red") | |
console.groupEnd(); | |
} | |
await loadSecret("op://dev/GitHub Action Test Bak/section/text") | |
await loadSecret("op://dev/GitHub Action Test Bak/add more/text") | |
await loadSecret("op://dev/GitHub Action Test Bak/add more/cs") | |
await loadSecret("op://dev/GitHub Action Test Bak/text") | |
await loadSecret("op://dev/GitHub Action Test Bak/cs") | |
await loadSecret("op://dev/GitHub Action Test Bak/duplicate/not_exist") | |
await loadSecret("op://dev/GitHub Action Test Bak/duplicate/text") | |
await loadSecret("op://dev/GitHub Action Test Bak/duplicate/url") | |
await loadSecret("op://dev/GitHub Action Test Bak/wgsjixhj5u7git3chjqvxmvzie/text") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment