Skip to content

Commit e1631e3

Browse files
committed
Accept TRANSLOADIT_AUTH_* env vars in CLI
1 parent 27ead72 commit e1631e3

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

src/cli/commands/BaseCommand.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dotenv/config'
44
import { Transloadit as TransloaditClient } from '../../Transloadit.ts'
55
import type { IOutputCtl } from '../OutputCtl.ts'
66
import OutputCtl, { LOG_LEVEL_DEFAULT, LOG_LEVEL_NAMES, parseLogLevel } from '../OutputCtl.ts'
7+
import { getEnvCredentials } from '../helpers.ts'
78

89
export abstract class BaseCommand extends Command {
910
logLevelOption = Option.String('-l,--log-level', {
@@ -31,7 +32,8 @@ export abstract class BaseCommand extends Command {
3132
}
3233

3334
protected setupClient(): boolean {
34-
if (!process.env.TRANSLOADIT_KEY || !process.env.TRANSLOADIT_SECRET) {
35+
const creds = getEnvCredentials()
36+
if (!creds) {
3537
this.output.error(
3638
'Please provide API authentication in the environment variables TRANSLOADIT_KEY and TRANSLOADIT_SECRET',
3739
)
@@ -40,11 +42,7 @@ export abstract class BaseCommand extends Command {
4042

4143
const endpoint = this.endpoint || process.env.TRANSLOADIT_ENDPOINT
4244

43-
this.client = new TransloaditClient({
44-
authKey: process.env.TRANSLOADIT_KEY,
45-
authSecret: process.env.TRANSLOADIT_SECRET,
46-
...(endpoint && { endpoint }),
47-
})
45+
this.client = new TransloaditClient({ ...creds, ...(endpoint && { endpoint }) })
4846
return true
4947
}
5048

src/cli/commands/auth.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '../../alphalib/types/template.ts'
99
import type { OptionalAuthParams } from '../../apiTypes.ts'
1010
import { Transloadit } from '../../Transloadit.ts'
11+
import { getEnvCredentials } from '../helpers.ts'
1112
import { UnauthenticatedCommand } from './BaseCommand.ts'
1213

1314
type UrlParamPrimitive = string | number | boolean
@@ -80,16 +81,7 @@ async function readStdin(): Promise<string> {
8081
return data
8182
}
8283

83-
function getCredentials(): { authKey: string; authSecret: string } | null {
84-
const authKey = process.env.TRANSLOADIT_KEY || process.env.TRANSLOADIT_AUTH_KEY
85-
const authSecret = process.env.TRANSLOADIT_SECRET || process.env.TRANSLOADIT_AUTH_SECRET
86-
87-
if (!authKey || !authSecret) {
88-
return null
89-
}
90-
91-
return { authKey, authSecret }
92-
}
84+
const getCredentials = getEnvCredentials
9385

9486
// Result type for signature operations
9587
type SigResult = { ok: true; output: string } | { ok: false; error: string }

src/cli/helpers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import type { Readable } from 'node:stream'
33
import type { APIError } from './types.ts'
44
import { isAPIError } from './types.ts'
55

6+
export function getEnvCredentials(): { authKey: string; authSecret: string } | null {
7+
const authKey = process.env.TRANSLOADIT_KEY ?? process.env.TRANSLOADIT_AUTH_KEY
8+
const authSecret = process.env.TRANSLOADIT_SECRET ?? process.env.TRANSLOADIT_AUTH_SECRET
9+
10+
if (!authKey || !authSecret) return null
11+
12+
return { authKey, authSecret }
13+
}
14+
615
export function createReadStream(file: string): Readable {
716
if (file === '-') return process.stdin
817
return fs.createReadStream(file)

0 commit comments

Comments
 (0)