Skip to content

Commit

Permalink
change headless sso to work properly (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotCamblor authored Jul 13, 2023
1 parent 679dc61 commit 0cfc431
Show file tree
Hide file tree
Showing 26 changed files with 992 additions and 983 deletions.
2 changes: 1 addition & 1 deletion src/commands/authCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default abstract class AuthCommand extends Base {
const { flags } = await this.parse(AuthCommand)
const organizations = await fetchOrganizations(this.authToken)
if (flags.headless && !flags.org) {
return this.writer.showResults(organizations.map((org) => org.name))
throw new Error('In headless mode, org flag is required')
}
const selectedOrg = await this.retrieveOrganization(organizations)
this.authToken = await this.selectOrganization(selectedOrg)
Expand Down
31 changes: 21 additions & 10 deletions src/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,21 +229,32 @@ export default abstract class Base extends Command {
return parsedToken.exp < Math.floor(Date.now()/1000)
}

async requireProject(): Promise<void> {
async requireProject(projectFlag?: string, headless?: boolean): Promise<void> {
if (this.projectKey !== '') {
return
}
const projects = await fetchProjects(this.authToken)
const project = await promptForProject(projects)
this.projectKey = project.key
const { shouldSave } = await inquirer.prompt([{
name: 'shouldSave',
message: 'Do you want to use this project for all future commands?',
type: 'confirm'
}])
if (shouldSave) {
await this.updateUserConfig({ project: this.projectKey })
if (headless && !projectFlag) {
throw new Error('In headless mode, project flag is required.')
} else if (projectFlag) {
const project = projects.find((proj) => proj.key === projectFlag)
if (!project) {
throw new Error(`Project not found for key ${projectFlag}`)
}
this.projectKey = projectFlag
} else {
const project = await promptForProject(projects)
this.projectKey = project.key
const { shouldSave } = await inquirer.prompt([{
name: 'shouldSave',
message: 'Do you want to use this project for all future commands?',
type: 'confirm'
}])
if (shouldSave) {
await this.updateUserConfig({ project: this.projectKey })
}
}

}

hasToken(): boolean {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/environments/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export default class DetailedEnvironments extends Base {
public async run(): Promise<void> {
const { flags } = await this.parse(DetailedEnvironments)
const keys = flags['keys']?.split(',')

await this.requireProject()
const { headless, project } = flags
await this.requireProject(project, headless)

if (flags.headless && !keys) {
throw (new Error('In headless mode, the keys flag is required'))
Expand Down
4 changes: 3 additions & 1 deletion src/commands/environments/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export default class ListEnvironments extends Base {
authRequired = true

public async run(): Promise<void> {
await this.requireProject()
const { flags } = await this.parse(ListEnvironments)
const { project, headless } = flags
await this.requireProject(project, headless)
const environments = await fetchEnvironments(this.authToken, this.projectKey)
const environmentKeys = environments.map((environment) => environment.key)
this.writer.showResults(environmentKeys)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/environments/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export default class UpdateEnvironment extends UpdateCommandWithCommonProperties
public async run(): Promise<void> {
const { args, flags } = await this.parse(UpdateEnvironment)
const { key } = args
const { headless } = flags
const { headless, project } = flags

await this.requireProject()
await this.requireProject(project, headless)
let envKey = key
if (headless && !envKey) {
this.writer.showError('The key argument is required')
Expand Down
6 changes: 3 additions & 3 deletions src/commands/features/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export default class CreateFeature extends CreateCommand {

public async run(): Promise<void> {
const { flags } = await this.parse(CreateFeature)
const { headless, key, name, description, variables, variations, sdkVisibility } = flags
await this.requireProject()
const { headless, key, name, description, variables, variations, sdkVisibility, project: projectFlag } = flags
await this.requireProject(projectFlag, headless)

if (headless && (!key || !name)) {
this.writer.showError('The key and name flags are required')
return
}
}

if (!flags.interactive) {
let params: Record<string, string> = flags
Expand Down
7 changes: 3 additions & 4 deletions src/commands/features/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ export default class DeleteFeatures extends Base {
}

public async run(): Promise<void> {
await this.requireProject()

const { args } = await this.parse(DeleteFeatures)

const { args, flags } = await this.parse(DeleteFeatures)
const { headless, project } = flags
await this.requireProject(project, headless)
let featureKey
if (!args.feature) {
const { feature } = await inquirer.prompt<FeaturePromptResult>([featurePrompt], {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/features/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export default class DetailedFeatures extends Base {
public async run(): Promise<void> {
const { flags } = await this.parse(DetailedFeatures)
const keys = flags['keys']?.split(',')

await this.requireProject()
const { project, headless } = flags
await this.requireProject(project, headless)

let features = await fetchFeatures(this.authToken, this.projectKey)
if (keys) {
Expand Down
4 changes: 3 additions & 1 deletion src/commands/features/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export default class ListFeatures extends Base {
authRequired = true

public async run(): Promise<void> {
await this.requireProject()
const { flags } = await this.parse(ListFeatures)
const { project, headless } = flags
await this.requireProject(project, headless)
const features = await fetchFeatures(this.authToken, this.projectKey)
const featureKeys = features.map((feature) => feature.key)
this.writer.showResults(featureKeys)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/features/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ export default class UpdateFeature extends UpdateCommandWithCommonProperties {

public async run(): Promise<void> {
const { flags, args } = await this.parse(UpdateFeature)
const { headless, key, name, description, variables, variations, sdkVisibility } = flags
await this.requireProject()
const { headless, key, name, description, variables, variations, sdkVisibility, project } = flags
await this.requireProject(project, headless)

const featureKey = args.key
let feature
Expand Down
3 changes: 2 additions & 1 deletion src/commands/generate/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export default class GenerateTypes extends Base {

public async run(): Promise<void> {
const { flags } = await this.parse(GenerateTypes)
await this.requireProject()
const { project, headless } = flags
await this.requireProject(project, headless)

const variables = await fetchAllVariables(this.authToken, this.projectKey)
const typesString = this.getTypesString(variables, flags['react'])
Expand Down
3 changes: 2 additions & 1 deletion src/commands/keys/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export default class GetEnvironmentKey extends Base {

public async run(): Promise<void> {
const { flags } = await this.parse(GetEnvironmentKey)
await this.requireProject()
const { project, headless } = flags
await this.requireProject(project, headless)

if (flags.headless && !flags.env) {
throw (new Error('In headless mode, the env flag is required'))
Expand Down
5 changes: 1 addition & 4 deletions src/commands/login/sso.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'reflect-metadata'

import { storeAccessToken } from '../../auth/config'
import SSOAuth from '../../api/ssoAuth'
import AuthCommand from '../authCommand'

Expand All @@ -12,8 +11,6 @@ export default class LoginSSO extends AuthCommand {
public async run(): Promise<void> {
const ssoAuth = new SSOAuth(this.writer)
this.authToken = await ssoAuth.getAccessToken()
storeAccessToken(this.authToken, this.authPath)

await this.setOrganizationAndProject()
}
}
}
4 changes: 2 additions & 2 deletions src/commands/targeting/disable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export default class DisableTargeting extends Base {

public async run(): Promise<void> {
const { args, flags } = await this.parse(DisableTargeting)

await this.requireProject()
const { project, headless } = flags
await this.requireProject(project, headless)

const {
feature,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/targeting/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ export default class EnableTargeting extends Base {

public async run(): Promise<void> {
const { args, flags } = await this.parse(EnableTargeting)

await this.requireProject()
const { project, headless } = flags
await this.requireProject(project, headless)

const {
feature,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/targeting/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export default class DetailedTargeting extends Base {

public async run(): Promise<void> {
const { args, flags } = await this.parse(DetailedTargeting)

await this.requireProject()
const { headless, project } = flags
await this.requireProject(project, headless)

const params: Params = {
featureKey: args.feature,
Expand Down
12 changes: 6 additions & 6 deletions src/commands/targeting/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export default class UpdateTargeting extends UpdateCommand {

public async run(): Promise<void> {
const { args, flags } = await this.parse(UpdateTargeting)
const { headless } = flags
await this.requireProject()
const { project, headless } = flags
await this.requireProject(project, headless)

if (flags.headless && (!args.feature || !args.environment)) {
this.writer.showError('Feature and environment arguments are required')
Expand Down Expand Up @@ -113,11 +113,11 @@ export default class UpdateTargeting extends UpdateCommand {
this.authToken, this.projectKey, featureKey, envKey
)
const targetingListPrompt = new TargetingListOptions(
featureTargetingRules.targets,
featureTargetingRules.targets,
audiences,
this.writer,
this.authToken,
this.projectKey,
this.writer,
this.authToken,
this.projectKey,
featureKey
)
targetingListPrompt.variations = variations
Expand Down
4 changes: 2 additions & 2 deletions src/commands/variables/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export default class CreateVariable extends CreateCommand {
prompts = createVariablePrompts

public async run(): Promise<void> {
await this.requireProject()
const { flags } = await this.parse(CreateVariable)
const { key, name, type, feature, headless } = flags
const { key, name, type, feature, headless, project } = flags
await this.requireProject(project, headless)

if (headless && (!key || !name || !type || !feature)) {
this.writer.showError('The key, name, feature, and type flags are required')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/variables/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export default class DetailedVariables extends Base {
public async run(): Promise<void> {
const { flags } = await this.parse(DetailedVariables)
const keys = flags['keys']?.split(',')

await this.requireProject()
const { project, headless } = flags
await this.requireProject(project, headless)

let variables = await fetchVariables(this.authToken, this.projectKey)
if (keys) {
Expand Down
5 changes: 3 additions & 2 deletions src/commands/variables/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import Base from '../base'
export default class ListVariables extends Base {
static hidden = false
authRequired = true

public async run(): Promise<void> {
await this.requireProject()
const { flags } = await this.parse(ListVariables)
const { headless, project } = flags
await this.requireProject(project, headless)
const variables = await fetchVariables(this.authToken, this.projectKey)
const variableKeys = variables.map((variable) => variable.key)
this.writer.showResults(variableKeys)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/variables/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export default class UpdateVariable extends UpdateCommandWithCommonProperties {
public async run(): Promise<void> {
const { args, flags } = await this.parse(UpdateVariable)
const { key } = args
const { headless } = flags
const { headless, project } = flags
flags._feature = flags.feature

await this.requireProject()
await this.requireProject(project, headless)
let variableKey = key
if (headless && !variableKey) {
this.writer.showError('The key argument is required')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/variations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export default class CreateVariation extends CreateCommand {
prompts = [namePrompt, keyPrompt]

public async run(): Promise<void> {
await this.requireProject()

const { args, flags } = await this.parse(CreateVariation)
const { headless, key, name, variables } = flags
const { headless, key, name, variables, project } = flags
await this.requireProject(project, headless)

let featureKey
let feature_id
Expand Down
4 changes: 2 additions & 2 deletions src/commands/variations/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export default class GetVariations extends Base {
}

public async run(): Promise<void> {
await this.requireProject()
const { args, flags } = await this.parse(GetVariations)
const { headless } = flags
const { headless, project } = flags
await this.requireProject(project, headless)
let featureKey
if (headless && !args.feature) {
this.writer.showError('In headless mode, feature is required')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/variations/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export default class ListVariations extends Base {
}

public async run(): Promise<void> {
await this.requireProject()
const { args, flags } = await this.parse(ListVariations)
const { headless } = flags
const { headless, project } = flags
await this.requireProject(project, headless)
let featureKey
if (headless && !args.feature) {
this.writer.showError('In headless mode, feature is required')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/variations/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export default class UpdateVariation extends UpdateCommandWithCommonProperties {
}

public async run(): Promise<void> {
await this.requireProject()

const { args, flags } = await this.parse(UpdateVariation)
const { variables, name, key, headless } = flags
const { variables, name, key, headless, project } = flags
await this.requireProject(project, headless)
let featureKey
if (!args.feature) {
const { feature } = await inquirer.prompt<FeaturePromptResult>([featurePrompt], {
Expand Down
Loading

0 comments on commit 0cfc431

Please sign in to comment.