Skip to content

Commit

Permalink
feat(cli): Add functionality to operate on Environments (#324)
Browse files Browse the repository at this point in the history
Co-authored-by: vr-varad <varadgupta21#gmail.com>
Co-authored-by: Rajdip Bhattacharya <agentR47@gmail.com>
  • Loading branch information
vr-varad and rajdip-b authored Jul 31, 2024
1 parent e28748f commit a26f7c1
Show file tree
Hide file tree
Showing 28 changed files with 1,300 additions and 851 deletions.
5 changes: 4 additions & 1 deletion apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "CLI for keyshade",
"main": "index.js",
"private": false,
"type": "commonjs",
"scripts": {
"build": "swc src --out-dir dist",
Expand All @@ -23,13 +24,15 @@
"figlet": "^1.7.0",
"fs": "0.0.1-security",
"nodemon": "^3.1.4",
"socket.io-client": "^4.7.5"
"socket.io-client": "^4.7.5",
"@keyshade/api-client": "workspace:../../packages/api-client"
},
"devDependencies": {
"@swc/cli": "^0.4.0",
"@swc/core": "^1.6.13",
"@types/cli-table": "^0.3.4",
"@types/figlet": "^1.5.8",
"@types/eccrypto": "^1.1.6",
"@types/node": "^20.14.10",
"eslint-config-standard-with-typescript": "^43.0.1"
}
Expand Down
26 changes: 26 additions & 0 deletions apps/cli/src/commands/environment.command.ts
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()
]
}
}
97 changes: 97 additions & 0 deletions apps/cli/src/commands/environment/create.environment.ts
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 }
}
}
55 changes: 55 additions & 0 deletions apps/cli/src/commands/environment/delete.environment.ts
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)
}
}
}
62 changes: 62 additions & 0 deletions apps/cli/src/commands/environment/get.environment.ts
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)
}
}
}
69 changes: 69 additions & 0 deletions apps/cli/src/commands/environment/list.environment.ts
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}`)
}
}
}
Loading

0 comments on commit a26f7c1

Please sign in to comment.