-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Add functionality to operate on Environments (#324)
Co-authored-by: vr-varad <varadgupta21#gmail.com> Co-authored-by: Rajdip Bhattacharya <[email protected]>
- Loading branch information
Showing
28 changed files
with
1,300 additions
and
851 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import BaseCommand from './base.command' | ||
import { CreateEnvironment } from './environment/create.environment' | ||
import { DeleteEnvironment } from './environment/delete.environment' | ||
import { GetEnvironment } from './environment/get.environment' | ||
import { ListEnvironment } from './environment/list.environment' | ||
import { UpdateEnvironment } from './environment/update.environment' | ||
|
||
export default class EnvironmentCommand extends BaseCommand { | ||
getName(): string { | ||
return 'environment' | ||
} | ||
|
||
getDescription(): string { | ||
return 'Manage your environments in keyshade.' | ||
} | ||
|
||
getSubCommands(): BaseCommand[] { | ||
return [ | ||
new CreateEnvironment(), | ||
new DeleteEnvironment(), | ||
new GetEnvironment(), | ||
new ListEnvironment(), | ||
new UpdateEnvironment() | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import BaseCommand from '../base.command' | ||
import { text } from '@clack/prompts' | ||
import { | ||
type CommandActionData, | ||
type CommandArgument, | ||
type CommandOption | ||
} from 'src/types/command/command.types' | ||
import { EnvironmentController } from '@keyshade/api-client' | ||
import { Logger } from '@/util/logger' | ||
export class CreateEnvironment extends BaseCommand { | ||
getName(): string { | ||
return 'create' | ||
} | ||
|
||
getDescription(): string { | ||
return 'Create a new environment' | ||
} | ||
|
||
getOptions(): CommandOption[] { | ||
return [ | ||
{ | ||
short: '-n', | ||
long: '--name <string>', | ||
description: 'Name of the Environment' | ||
}, | ||
{ | ||
short: '-d', | ||
long: '--description <string>', | ||
description: 'Description about the Environment' | ||
} | ||
] | ||
} | ||
|
||
getArguments(): CommandArgument[] { | ||
return [ | ||
{ | ||
name: '<Project ID>', | ||
description: | ||
'ID of the project under which you want to add the environment' | ||
} | ||
] | ||
} | ||
|
||
async action({ options, args }: CommandActionData): Promise<void> { | ||
const [projectId] = args | ||
const { name, description } = await this.parseInput(options) | ||
|
||
if (!projectId) { | ||
Logger.error('Project ID is required') | ||
return | ||
} | ||
|
||
const apiKey = this.apiKey | ||
|
||
const environmentData = { | ||
name, | ||
description, | ||
projectId | ||
} | ||
|
||
const headers = { | ||
'x-keyshade-token': apiKey | ||
} | ||
|
||
const environmentController = new EnvironmentController(this.baseUrl) | ||
Logger.info('Creating Environment...') | ||
|
||
const { | ||
data: environment, | ||
error, | ||
success | ||
} = await environmentController.createEnvironment(environmentData, headers) | ||
|
||
if (success) { | ||
Logger.info(`Environment created:${environment.name} (${environment.id})`) | ||
} else { | ||
Logger.error(`Failed to create environment: ${error.message}`) | ||
} | ||
} | ||
|
||
private async parseInput(options: CommandActionData['options']): Promise<{ | ||
name: string | ||
description?: string | ||
}> { | ||
let { name } = options | ||
const { description } = options | ||
|
||
if (!name) { | ||
name = await text({ | ||
message: 'Enter the name of the Environment', | ||
placeholder: 'env' | ||
}) | ||
} | ||
|
||
return { name, description } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import BaseCommand from '../base.command' | ||
import { EnvironmentController } from '@keyshade/api-client' | ||
import { | ||
type CommandActionData, | ||
type CommandArgument | ||
} from 'src/types/command/command.types' | ||
import { Logger } from '@/util/logger' | ||
|
||
export class DeleteEnvironment extends BaseCommand { | ||
getName(): string { | ||
return 'delete' | ||
} | ||
|
||
getDescription(): string { | ||
return 'Delete an environment' | ||
} | ||
|
||
getArguments(): CommandArgument[] { | ||
return [ | ||
{ | ||
name: '<Environment ID>', | ||
description: 'ID of the environment which you want to delete.' | ||
} | ||
] | ||
} | ||
|
||
async action({ args }: CommandActionData): Promise<void> { | ||
const [environmentId] = args | ||
|
||
if (!environmentId) { | ||
Logger.error('Environment ID is required') | ||
return | ||
} | ||
|
||
const apiKey = this.apiKey | ||
|
||
const headers = { | ||
'x-keyshade-token': apiKey | ||
} | ||
|
||
const environmentController = new EnvironmentController(this.baseUrl) | ||
Logger.info('Deleting Environment...') | ||
|
||
const { success, error } = await environmentController.deleteEnvironment( | ||
{ id: environmentId }, | ||
headers | ||
) | ||
|
||
if (success) { | ||
Logger.info('Environment deleted successfully') | ||
} else { | ||
Logger.error(error.message) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Logger } from '@/util/logger' | ||
import BaseCommand from '../base.command' | ||
import { EnvironmentController } from '@keyshade/api-client' | ||
import { | ||
type CommandActionData, | ||
type CommandArgument | ||
} from 'src/types/command/command.types' | ||
|
||
export class GetEnvironment extends BaseCommand { | ||
getName(): string { | ||
return 'get' | ||
} | ||
|
||
getDescription(): string { | ||
return 'Get an environment' | ||
} | ||
|
||
getArguments(): CommandArgument[] { | ||
return [ | ||
{ | ||
name: '<Environment ID>', | ||
description: 'ID of the environment which you want to fetch.' | ||
} | ||
] | ||
} | ||
|
||
async action({ args }: CommandActionData): Promise<void> { | ||
const [environmentId] = args | ||
|
||
if (!environmentId) { | ||
Logger.error('Environment ID is required') | ||
return | ||
} | ||
|
||
const apiKey = this.apiKey | ||
|
||
const headers = { | ||
'x-keyshade-token': apiKey | ||
} | ||
|
||
const environmentController = new EnvironmentController(this.baseUrl) | ||
Logger.info('Fetching Environment...') | ||
|
||
const { | ||
success, | ||
error, | ||
data: environment | ||
} = await environmentController.getEnvironmentById( | ||
{ id: environmentId }, | ||
headers | ||
) | ||
|
||
if (success) { | ||
Logger.info('Environment fetched successfully:') | ||
Logger.info( | ||
`Environment ID: ${environment.id}, Name: ${environment.name}, Description: ${environment.description}` | ||
) | ||
} else { | ||
Logger.error(error.message) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import BaseCommand from '../base.command' | ||
import { EnvironmentController } from '@keyshade/api-client' | ||
import { | ||
type CommandActionData, | ||
type CommandArgument | ||
} from 'src/types/command/command.types' | ||
import { Logger } from '@/util/logger' | ||
|
||
export class ListEnvironment extends BaseCommand { | ||
getName(): string { | ||
return 'list' | ||
} | ||
|
||
getDescription(): string { | ||
return 'List all environments under a project' | ||
} | ||
|
||
getArguments(): CommandArgument[] { | ||
return [ | ||
{ | ||
name: '<Project ID>', | ||
description: 'ID of the project whose environments you want.' | ||
} | ||
] | ||
} | ||
|
||
async action({ args }: CommandActionData): Promise<void> { | ||
const [projectId] = args | ||
|
||
if (!projectId) { | ||
Logger.error('Project ID is required') | ||
return | ||
} | ||
|
||
const apiKey = this.apiKey | ||
|
||
const headers = { | ||
'x-keyshade-token': apiKey | ||
} | ||
|
||
if (!apiKey) { | ||
Logger.error('Base URL and API Key must be set as environment variables') | ||
return | ||
} | ||
|
||
const environmentController = new EnvironmentController(this.baseUrl) | ||
Logger.info('Fetching all environments...') | ||
|
||
const { | ||
success, | ||
data: environments, | ||
error | ||
} = await environmentController.getAllEnvironmentsOfProject( | ||
{ projectId }, | ||
headers | ||
) | ||
|
||
if (success) { | ||
Logger.info('Fetched environments:') | ||
environments.items.forEach((environment) => { | ||
Logger.info( | ||
`- ID: ${environment.id}, Name: ${environment.name}, Description: ${environment.description}` | ||
) | ||
}) | ||
} else { | ||
Logger.error(`Failed to fetch environments: ${error.message}`) | ||
} | ||
} | ||
} |
Oops, something went wrong.