diff --git a/README.md b/README.md index 9225e0b..a04179c 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,32 @@ npm install -g shielded-cli ## Usage ```bash -# You can also use the --token argument +# Update one shield with its own API token. export SHIELDED_TOKEN="" shielded --title "Last Build" --text "$(date)" --color "00AA33" ``` + +For a one-off command, pass the token directly with `--token`. An explicit +`--token` always wins over environment variables. + +```bash +shielded --token "" --title "Last Build" --text "$(date)" --color "00AA33" +``` + +### Update shields by key + +A user API token can update or create any of your shields. Give the shield a +stable key with `--key`, then keep the user token in +`SHIELDED_USER_TOKEN`: + +```bash +export SHIELDED_USER_TOKEN="sdu_" + +shielded --key "production-status" --title "Last Build" --text "$(date)" --color "00AA33" +``` + +When you use `--key`, the CLI prefers `SHIELDED_USER_TOKEN` and falls +back to `SHIELDED_TOKEN` if it is unset. Without `--key`, it uses only +`SHIELDED_TOKEN`, so your existing per-shield-token commands keep working as +before. An explicit `--token` takes precedence in either workflow. diff --git a/dist/bin.js b/dist/bin.js index 290b36e..7b0261d 100755 --- a/dist/bin.js +++ b/dist/bin.js @@ -21,14 +21,25 @@ program .option('-c, --color ', 'Badge color') .option('-T, --title ', 'Badge title') .option('-x, --text <text>', 'Badge text') + .option('-k, --key <key>', 'Shield key') + .option('-t, --token <token>', 'API token') .version(pkg_json_1.version); program.parse(); const options = program.opts(); -if (!options.token && process.env.SHIELDED_TOKEN) { - options.token = process.env.SHIELDED_TOKEN; +if (!options.token) { + const tokenEnvironmentVariables = options.key + ? ['SHIELDED_USER_TOKEN', 'SHIELDED_TOKEN'] + : ['SHIELDED_TOKEN']; + for (const variable of tokenEnvironmentVariables) { + if (process.env[variable]) { + options.token = process.env[variable]; + break; + } + } } if (!options.token) { - process.stderr.write('Missing token. Please set SHIELDED_TOKEN environment variable or use --token option.'); + const tokenVariables = options.key ? 'SHIELDED_USER_TOKEN or SHIELDED_TOKEN' : 'SHIELDED_TOKEN'; + process.stderr.write(`Missing token. Please set the ${tokenVariables} environment variable or use --token option.`); process.stderr.write(program.helpInformation()); process.exit(1); } @@ -44,6 +55,7 @@ if (!options.token) { try { shield = yield s.updateShield({ color: options.color, + shieldKey: options.key, title: options.title, text: options.text, }); diff --git a/dist/pkg.json b/dist/pkg.json index a5ae5f6..46193da 100644 --- a/dist/pkg.json +++ b/dist/pkg.json @@ -16,18 +16,18 @@ "license": "MIT", "homepage": "https://github.com/ShieldedDotDev/shielded-cli-js", "devDependencies": { - "@types/node": "^20.8.0", + "@types/node": "^26.1.0", "@types/node-fetch": "^2.6.2", "@typescript-eslint/eslint-plugin": "^5.41.0", "@typescript-eslint/parser": "^5.41.0", "eslint": "^8.26.0", - "typescript": "^5.4.3" + "typescript": "^5.9.3" }, "bin": { "shielded": "./dist/bin.js" }, "dependencies": { - "commander": "^9.4.1", - "node-fetch": "^2.6.7" + "commander": "^12.0.0", + "node-fetch": "^2.6.13" } } diff --git a/dist/sdk.d.ts b/dist/sdk.d.ts index eaa2e70..2fae074 100644 --- a/dist/sdk.d.ts +++ b/dist/sdk.d.ts @@ -5,9 +5,11 @@ export interface ShieldOptions { title?: string; text?: string; color?: string; + shieldKey?: string; } export interface ShieldResponse { ShieldURL: string; + ShieldKey?: string; } export declare class StatusError extends Error { status: number; diff --git a/dist/sdk.js b/dist/sdk.js index 2edf9f7..25240cf 100644 --- a/dist/sdk.js +++ b/dist/sdk.js @@ -48,6 +48,9 @@ class ShieldedAPI { if (options.color) { params.append('color', options.color); } + if (options.shieldKey) { + params.append('shield_key', options.shieldKey); + } const result = yield (0, node_fetch_1.default)(options.endpoint, { method: 'POST', headers: { diff --git a/src/bin.ts b/src/bin.ts index ff98fbe..263f21d 100755 --- a/src/bin.ts +++ b/src/bin.ts @@ -12,26 +12,39 @@ program .option('-c, --color <color>', 'Badge color') .option('-T, --title <title>', 'Badge title') .option('-x, --text <text>', 'Badge text') + .option('-k, --key <key>', 'Shield key') + .option('-t, --token <token>', 'API token') .version(version); program.parse(); type Options = { endpoint: string | undefined; - token: string; + token: string | undefined; color: string | undefined; title: string | undefined; text: string | undefined; + key: string | undefined; }; const options = program.opts<Options>(); -if (!options.token && process.env.SHIELDED_TOKEN) { - options.token = process.env.SHIELDED_TOKEN; +if (!options.token) { + const tokenEnvironmentVariables = options.key + ? ['SHIELDED_USER_TOKEN', 'SHIELDED_TOKEN'] + : ['SHIELDED_TOKEN']; + + for (const variable of tokenEnvironmentVariables) { + if (process.env[variable]) { + options.token = process.env[variable]; + break; + } + } } if (!options.token) { - process.stderr.write('Missing token. Please set SHIELDED_TOKEN environment variable or use --token option.'); + const tokenVariables = options.key ? 'SHIELDED_USER_TOKEN or SHIELDED_TOKEN' : 'SHIELDED_TOKEN'; + process.stderr.write(`Missing token. Please set the ${tokenVariables} environment variable or use --token option.\n\n`); process.stderr.write(program.helpInformation()); process.exit(1); } @@ -50,6 +63,7 @@ if (!options.token) { try { shield = await s.updateShield({ color: options.color, + shieldKey: options.key, title: options.title, text: options.text, }); @@ -65,4 +79,4 @@ if (!options.token) { console.log(shield.ShieldURL); })().catch((e) => { throw e; -}); \ No newline at end of file +}); diff --git a/src/sdk.ts b/src/sdk.ts index 7de1541..b77278f 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -6,10 +6,12 @@ export interface ShieldOptions { title?: string; text?: string; color?: string; + shieldKey?: string; } export interface ShieldResponse { ShieldURL: string; + ShieldKey?: string; } export class StatusError extends Error { @@ -56,6 +58,10 @@ export class ShieldedAPI { params.append('color', options.color); } + if (options.shieldKey) { + params.append('shield_key', options.shieldKey); + } + const result = await fetch(options.endpoint, { method: 'POST', headers: { @@ -72,4 +78,4 @@ export class ShieldedAPI { return await result.json() as ShieldResponse; } -} \ No newline at end of file +}