Skip to content

Instantly share code, notes, and snippets.

@bxb100
Last active February 28, 2025 13:25
Show Gist options
  • Save bxb100/9382f3022190b4f45dac3be39de9615b to your computer and use it in GitHub Desktop.
Save bxb100/9382f3022190b4f45dac3be39de9615b to your computer and use it in GitHub Desktop.

Revisions

  1. bxb100 revised this gist Feb 28, 2025. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion deno-test-1password-sdk-cli.ts
    Original file line number Diff line number Diff line change
    @@ -61,4 +61,7 @@ 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")
  2. bxb100 created this gist Feb 27, 2025.
    64 changes: 64 additions & 0 deletions deno-test-1password-sdk-cli.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    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")