Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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="<Your token from shielded.dev>"

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 "<Your token from shielded.dev>" --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_<your user API token>"

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.
18 changes: 15 additions & 3 deletions dist/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,25 @@ 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(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);
}
Expand All @@ -44,6 +55,7 @@ if (!options.token) {
try {
shield = yield s.updateShield({
color: options.color,
shieldKey: options.key,
title: options.title,
text: options.text,
});
Expand Down
8 changes: 4 additions & 4 deletions dist/pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 2 additions & 0 deletions dist/sdk.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions dist/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
24 changes: 19 additions & 5 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Comment thread
Copilot marked this conversation as resolved.
process.exit(1);
}
Expand All @@ -50,6 +63,7 @@ if (!options.token) {
try {
shield = await s.updateShield({
color: options.color,
shieldKey: options.key,
title: options.title,
text: options.text,
});
Expand All @@ -65,4 +79,4 @@ if (!options.token) {
console.log(shield.ShieldURL);
})().catch((e) => {
throw e;
});
});
8 changes: 7 additions & 1 deletion src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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: {
Expand All @@ -72,4 +78,4 @@ export class ShieldedAPI {
return await result.json() as ShieldResponse;
}

}
}