Skip to content

Commit 1c58dbd

Browse files
authored
Merge pull request #55 from muxinc/dj/agent-mode
feat: add --agent flag for machine-readable output with modified User-Agent
2 parents b22c1fd + 114971d commit 1c58dbd

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

src/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,39 @@ import { signingKeysCommand } from './commands/signing-keys/index.ts';
2323
import { transcriptionVocabulariesCommand } from './commands/transcription-vocabularies/index.ts';
2424
import { uploadsCommand } from './commands/uploads/index.ts';
2525
import { videoViewsCommand } from './commands/video-views/index.ts';
26+
import { setAgentMode } from './lib/context.ts';
2627

2728
const VERSION = pkg.version;
2829

30+
/**
31+
* Preprocess argv to handle --agent flag before Cliffy parses it.
32+
* Strips --agent from args, enables agent mode, and injects --json.
33+
*/
34+
function preprocessArgs(argv: string[]): string[] {
35+
const agentIndex = argv.indexOf('--agent');
36+
if (agentIndex === -1) {
37+
return argv;
38+
}
39+
40+
setAgentMode(true);
41+
const args = argv.filter((_, i) => i !== agentIndex);
42+
43+
if (!args.includes('--json')) {
44+
args.push('--json');
45+
}
46+
47+
return args;
48+
}
49+
2950
// Main CLI command
3051
const cli = new Command()
3152
.name('mux')
3253
.version(VERSION)
3354
.description('Official Mux CLI for interacting with Mux APIs')
55+
.globalOption(
56+
'--agent',
57+
'Agent mode: uses JSON output and identifies as an agent in User-Agent header.',
58+
)
3459
.action(function () {
3560
this.showHelp();
3661
})
@@ -61,7 +86,7 @@ const cli = new Command()
6186
// Run the CLI
6287
if (import.meta.main) {
6388
try {
64-
await cli.parse(Bun.argv.slice(2));
89+
await cli.parse(preprocessArgs(Bun.argv.slice(2)));
6590
} catch (error) {
6691
if (error instanceof Error) {
6792
console.error(`Error: ${error.message}`);

src/lib/context.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let agentMode = false;
2+
3+
export function setAgentMode(value: boolean) {
4+
agentMode = value;
5+
}
6+
7+
export function isAgentMode(): boolean {
8+
return agentMode;
9+
}

src/lib/mux.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import Mux from '@mux/mux-node';
22
import pkg from '../../package.json';
33
import { getDefaultEnvironment } from './config.ts';
4+
import { isAgentMode } from './context.ts';
45

5-
const USER_AGENT = `Mux CLI/${pkg.version}`;
6+
function getUserAgent(): string {
7+
return isAgentMode()
8+
? `Mux CLI (agent)/${pkg.version}`
9+
: `Mux CLI/${pkg.version}`;
10+
}
611

712
/**
813
* Create an authenticated Mux client using stored credentials
@@ -17,7 +22,7 @@ export async function createAuthenticatedMuxClient(): Promise<Mux> {
1722
return new Mux({
1823
tokenId: env.environment.tokenId,
1924
tokenSecret: env.environment.tokenSecret,
20-
defaultHeaders: { 'User-Agent': USER_AGENT },
25+
defaultHeaders: { 'User-Agent': getUserAgent() },
2126
});
2227
}
2328

@@ -34,7 +39,7 @@ export async function validateCredentials(
3439
const mux = new Mux({
3540
tokenId,
3641
tokenSecret,
37-
defaultHeaders: { 'User-Agent': USER_AGENT },
42+
defaultHeaders: { 'User-Agent': getUserAgent() },
3843
});
3944

4045
// Make a simple API call to verify credentials

0 commit comments

Comments
 (0)