Skip to content

Commit

Permalink
feat: allow credentials via command line flags (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
jroehl authored May 22, 2023
1 parent be04789 commit 34e943b
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/lib/base-class.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Args, Command, Flags, ux } from '@oclif/core';
import { ArgOutput, FlagInput, FlagOutput } from '@oclif/core/lib/interfaces/parser';
import GoogleSheet, { GoogleSheetCli } from './google-sheet';

export const spreadsheetId = Flags.string({
Expand All @@ -14,6 +15,7 @@ export const worksheetTitle = Flags.string({
required: true,
env: 'WORKSHEET_TITLE',
});

export const valueInputOption = Flags.string({
char: 'v',
description: 'The style of the input ("RAW" or "USER_ENTERED")',
Expand All @@ -31,18 +33,33 @@ export const data = Args.string({
env: 'DATA',
});

interface CommonFlags {
rawOutput: boolean;
clientEmail: string | undefined;
privateKey: string | undefined;
help: void;
}

export default abstract class extends Command {
private rawLogs: boolean = false;
public gsheet!: GoogleSheet;

static flags = {
static flags: FlagInput<CommonFlags> = {
help: Flags.help({ char: 'h' }),
rawOutput: Flags.boolean({
char: 'r',
description: 'Get the raw output as a JSON string',
default: false,
required: false,
}),
clientEmail: Flags.string({
char: 'c',
env: 'GSHEET_CLIENT_EMAIL',
}),
privateKey: Flags.string({
char: 'p',
env: 'GSHEET_PRIVATE_KEY',
}),
};

async start(message: string) {
Expand All @@ -67,18 +84,15 @@ export default abstract class extends Command {

async init() {
// do some initialization
const { flags } = await this.parse(<any>this.constructor);
this.rawLogs = flags && (flags as any).rawOutput;

const {
GSHEET_CLIENT_EMAIL = await ux.prompt('What is your client email?', { type: 'hide' }),
GSHEET_PRIVATE_KEY = await ux.prompt('What is your private key?', { type: 'hide' }),
} = process.env;
const { flags } = await this.parse<CommonFlags, FlagOutput, ArgOutput>(<any>this.constructor);
this.rawLogs = !!flags?.rawOutput;
const clientEmail = flags?.clientEmail ?? (await ux.prompt('What is your client email?', { type: 'hide' }));
const privateKey = flags?.privateKey ?? (await ux.prompt('What is your private key?', { type: 'hide' }));

const gsheet = new GoogleSheet();
await gsheet.authorize({
client_email: GSHEET_CLIENT_EMAIL,
private_key: GSHEET_PRIVATE_KEY,
client_email: clientEmail,
private_key: privateKey,
});

this.gsheet = gsheet;
Expand Down

0 comments on commit 34e943b

Please sign in to comment.