Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow growthbook auth login to accept its arguments as flags #37

Merged
merged 4 commits into from
Aug 1, 2023
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,13 @@ Configure your API key with the GrowthBook SDK with your project

```
USAGE
$ growthbook auth login
$ growthbook auth login [-u <value>] [-p <value>] [--apiKey <value>]

FLAGS
-p, --profile=<value> Optional profile (for projects that use multiple GrowthBook instances) default: default)
-u, --apiBaseUrl=<value> Your GrowthBook instance base URL (e.g. http://localhost:3100, default:
https://api.growthbook.io)
--apiKey=<value> Your GrowthBook secret API key

DESCRIPTION
Configure your API key with the GrowthBook SDK with your project
Expand Down
69 changes: 49 additions & 20 deletions src/commands/auth/login.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,72 @@
import {Command, ux} from '@oclif/core'
import {Command, Flags, ux} from '@oclif/core'
import * as Fs from 'node:fs'
import * as toml from '@iarna/toml'
import {getGrowthBookConfigDirectory, getGrowthBookConfigFilePath} from '../../utils/file'
import {DEFAULT_GROWTHBOOK_BASE_URL, DEFAULT_GROWTHBOOK_PROFILE} from '../../utils/constants'
import {Icons} from '../../utils/cli'
import {Icons, baseGrowthBookCliFlags} from '../../utils/cli'
import {getGrowthBookProfileConfigAndThrowForCommand} from '../../utils/config'

export default class Login extends Command {
static description = 'Configure your API key with the GrowthBook SDK with your project'

static examples = []

static flags = {}
static flags = {
...baseGrowthBookCliFlags,
apiKey: Flags.string({
description: 'Your GrowthBook secret API key',
required: false,
}),
}

static args = {}

async run(): Promise<void> {
const apiKey = await ux.prompt('What is your GrowthBook secret API Key?', {
type: 'hide',
required: true,
})
if (!apiKey) {
this.error(`${Icons.xSymbol} You must provide a GrowthBook secret API key to continue`)
const {
args: {
apiKey,
},
flags: {
profile,
apiBaseUrl,
},
} = await this.parse(Login)
let profileUsed = profile

if (!profileUsed) {
profileUsed = await ux.prompt(`What is the name of this profile? You can leave this blank (default: ${DEFAULT_GROWTHBOOK_PROFILE})`, {
required: false,
})
if (!profileUsed) {
profileUsed = DEFAULT_GROWTHBOOK_PROFILE
}
}

let profile = await ux.prompt(`What is the name of this profile? You can leave this blank (default: ${DEFAULT_GROWTHBOOK_PROFILE})`, {
required: false,
})
if (!profile) {
profile = DEFAULT_GROWTHBOOK_PROFILE
const {apiKey: configApiKey, apiBaseUrl: configApiBaseUrl} = getGrowthBookProfileConfigAndThrowForCommand(profileUsed, this)
let baseUrlUsed = apiBaseUrl || configApiBaseUrl || DEFAULT_GROWTHBOOK_BASE_URL

let apiKeyUsed = apiKey || configApiKey

if (!apiKeyUsed) {
apiKeyUsed = await ux.prompt('What is your GrowthBook secret API Key?', {
type: 'hide',
required: true,
})
if (!apiKeyUsed) {
this.error(`${Icons.xSymbol} You must provide a GrowthBook secret API key to continue`)
}
}

let apiBaseUrl = await ux.prompt(`What is the API base URL of the GrowthBook instance? If using GrowthBook cloud, you can leave this blank (e.g. http://localhost:3100, default: ${DEFAULT_GROWTHBOOK_BASE_URL})`, {
required: false,
})
if (!apiBaseUrl) {
apiBaseUrl = DEFAULT_GROWTHBOOK_BASE_URL
if (!baseUrlUsed) {
baseUrlUsed = await ux.prompt(`What is the API base URL of the GrowthBook instance? If using GrowthBook cloud, you can leave this blank (e.g. http://localhost:3100, default: ${DEFAULT_GROWTHBOOK_BASE_URL})`, {
required: false,
})
if (!baseUrlUsed) {
baseUrlUsed = DEFAULT_GROWTHBOOK_BASE_URL
}
}

this.writeApiKeyToGrowthBookConfig(apiKey, profile, apiBaseUrl)
this.writeApiKeyToGrowthBookConfig(apiKeyUsed, profileUsed, baseUrlUsed)
}

/**
Expand Down