Skip to content

Commit

Permalink
feat(cli): Add functionality to operate on Variables (#514)
Browse files Browse the repository at this point in the history
Co-authored-by: codiumai-pr-agent-free[bot] <138128286+codiumai-pr-agent-free[bot]@users.noreply.github.com>
  • Loading branch information
anudeeps352 and codiumai-pr-agent-free[bot] authored Oct 29, 2024
1 parent ee58f07 commit 32d93e6
Show file tree
Hide file tree
Showing 8 changed files with 450 additions and 1 deletion.
28 changes: 28 additions & 0 deletions apps/cli/src/commands/variable.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import BaseCommand from '@/commands/base.command'
import CreateVariable from '@/commands/variable/create.variable'
import DeleteVariable from '@/commands/variable/delete.variable'
import ListVariable from '@/commands/variable/list.variable'
import FetchVariableRevisions from '@/commands/variable/revisions.variable'
import UpdateVariable from '@/commands/variable/update.variable'
import RollbackVariable from '@/commands/variable/rollback.variable'

export default class VariableCommand extends BaseCommand {
getName(): string {
return 'variable'
}

getDescription(): string {
return 'Manages the variable on keyshade'
}

getSubCommands(): BaseCommand[] {
return [
new CreateVariable(),
new DeleteVariable(),
new ListVariable(),
new FetchVariableRevisions(),
new UpdateVariable(),
new RollbackVariable()
]
}
}
115 changes: 115 additions & 0 deletions apps/cli/src/commands/variable/create.variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import BaseCommand from '@/commands/base.command'
import { text } from '@clack/prompts'
import ControllerInstance from '@/util/controller-instance'
import { Logger } from '@/util/logger'
import {
type CommandActionData,
type CommandArgument,
type CommandOption
} from '@/types/command/command.types'

export default class CreateVariable extends BaseCommand {
getName(): string {
return 'create'
}
getDescription(): string {
return 'Create a variable'
}
getArguments(): CommandArgument[] {
return [
{
name: '<Project Slug>',
description:
'Slug of the project under which you want to create variable'
}
]
}

getOptions(): CommandOption[] {
return [
{
short: '-n',
long: '--name <string>',
description: 'Name of the variable. Must be unique across the project'
},
{
short: '-d',
long: '--note <string>',
description: 'A note describing the usage of the variable.'
},
{
short: '-e',
long: '--entries [entries...]',
description:
'An array of key-value pair (value and environmentSlug) for the variable.'
}
]
}

async action({ args, options }: CommandActionData): Promise<void> {
const { name, note, entries } = await this.parseInput(options)
const [projectSlug] = args

if (!projectSlug) {
Logger.error('Project slug is required')
return
}

const { data, error, success } =
await ControllerInstance.getInstance().variableController.createVariable(
{
name,
note,
entries,
projectSlug
},
this.headers
)

if (success) {
Logger.info(`Variable ${data.name} (${data.slug}) created successfully!`)
Logger.info(`Created at ${data.createdAt}`)
Logger.info(`Updated at ${data.updatedAt}`)
} else {
Logger.error(`Failed to create variable: ${error.message}`)
}
}

private async parseInput(options: CommandActionData['options']): Promise<{
name: string
note?: string
entries: Array<{ value: string; environmentSlug: string }>
}> {
let { name, note } = options
const { entries } = options

if (!name) {
name = await text({
message: 'Enter the name of variable',
placeholder: 'My Variable'
})
}

if (!entries) {
throw new Error('Entries is required')
}

if (!note) {
note = name
}

const parsedEntries = entries.map((entry) => {
const [environmentSlug, value] = entry.split('=').map(s => s.trim())
if (!environmentSlug || !value) {
throw new Error(`Invalid entry format: ${entry}. Expected format: "environmentSlug=value"`)
}
return { environmentSlug, value }
})

return {
name,
note,
entries: parsedEntries
}
}
}
44 changes: 44 additions & 0 deletions apps/cli/src/commands/variable/delete.variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import type {
CommandActionData,
CommandArgument
} from '@/types/command/command.types'
import BaseCommand from '@/commands/base.command'
import { Logger } from '@/util/logger'
import ControllerInstance from '@/util/controller-instance'

export default class DeleteVariable extends BaseCommand {
getName(): string {
return 'delete'
}

getDescription(): string {
return 'Deletes a variable'
}

getArguments(): CommandArgument[] {
return [
{
name: '<Variable Slug>',
description: 'Slug of the variable that you want to delete.'
}
]
}

async action({ args }: CommandActionData): Promise<void> {
const [variableSlug] = args

const { error, success } =
await ControllerInstance.getInstance().variableController.deleteVariable(
{
variableSlug
},
this.headers
)

if (success) {
Logger.info(`Variable ${variableSlug} deleted successfully!`)
} else {
Logger.error(`Failed to delete variable: ${error.message}`)
}
}
}
50 changes: 50 additions & 0 deletions apps/cli/src/commands/variable/list.variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type {
CommandActionData,
CommandArgument
} from '@/types/command/command.types'
import BaseCommand from '@/commands/base.command'
import ControllerInstance from '@/util/controller-instance'
import { Logger } from '@/util/logger'

export default class ListVariable extends BaseCommand {
getName(): string {
return 'list'
}

getDescription(): string {
return 'List all variable under a project'
}

getArguments(): CommandArgument[] {
return [
{
name: '<Project Slug>',
description: 'Slug of the project whose variable you want.'
}
]
}

async action({ args }: CommandActionData): Promise<void> {
const [projectSlug] = args
const { data, error, success } =
await ControllerInstance.getInstance().variableController.getAllVariablesOfProject(
{
projectSlug
},
this.headers
)

if (success) {
const variables = data
if (variables.length > 0) {
data.forEach((variable: any) => {
Logger.info(`- ${variable.name} (${variable.value})`)
})
} else {
Logger.info('No variables found')
}
} else {
Logger.error(`Failed fetching variables: ${error.message}`)
}
}
}
63 changes: 63 additions & 0 deletions apps/cli/src/commands/variable/revisions.variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import type {
CommandActionData,
CommandArgument
} from '@/types/command/command.types'
import BaseCommand from '@/commands/base.command'
import ControllerInstance from '@/util/controller-instance'
import { Logger } from '@/util/logger'

export default class FetchVariableRevisions extends BaseCommand {
getName(): string {
return 'revisions'
}

getDescription(): string {
return 'Fetch all revisions of a variable'
}

getArguments(): CommandArgument[] {
return [
{
name: '<Variable Slug>',
description: 'Slug of the variable whose revisions you want.'
},
{
name: '<Environment Slug>',
description:
'Environment slug of the variable whose revisions you want.'
}
]
}

async action({ args }: CommandActionData): Promise<void> {
const [variableSlug, environmentSlug] = args

const { data, error, success } =
await ControllerInstance.getInstance().variableController.getRevisionsOfVariable(
{
variableSlug,
environmentSlug
},
this.headers
)

if (success) {
const revisions = data.items
if (revisions.length > 0) {
data.items.forEach((revision: any) => {
Logger.info(`Id ${revision.id}`)
Logger.info(`value ${revision.value}`)
Logger.info(`version ${revision.version}`)
Logger.info(`variableID ${revision.variableId}`)
Logger.info(`Created On ${revision.createdOn}`)
Logger.info(`Created By Id ${revision.createdById}`)
Logger.info(`environmentId ${revision.environmentId}`)
})
} else {
Logger.info('No revisions found')
}
} else {
Logger.error(`Failed fetching revisions: ${error.message}`)
}
}
}
78 changes: 78 additions & 0 deletions apps/cli/src/commands/variable/rollback.variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import type {
CommandActionData,
CommandArgument,
CommandOption
} from '@/types/command/command.types'
import BaseCommand from '@/commands/base.command'
import { Logger } from '@/util/logger'
import ControllerInstance from '@/util/controller-instance'

export default class RollbackVariable extends BaseCommand {
getName(): string {
return 'rollback'
}

getDescription(): string {
return 'Rollbacks a variable'
}

getArguments(): CommandArgument[] {
return [
{
name: '<Variable Slug>',
description: 'Slug of the variable that you want to rollback'
}
]
}

getOptions(): CommandOption[] {
return [
{
short: '-v',
long: '--version <string>',
description: 'Version of the variable to which you want to rollback'
},
{
short: '-e',
long: '--environmentSlug <string>',
description:
'Slug of the environment of the variable to which you want to rollback'
}
]
}

async action({ args, options }: CommandActionData): Promise<void> {
const [variableSlug] = args
const { environmentSlug, version } = await this.parseInput(options)
const { data, error, success } =
await ControllerInstance.getInstance().variableController.rollbackVariable(
{
environmentSlug,
version,
variableSlug
},
this.headers
)

if (success) {
Logger.info(`Variable ${data.name} (${data.slug}) updated successfully!`)
Logger.info(`Created at ${data.createdAt}`)
Logger.info(`Updated at ${data.updatedAt}`)
Logger.info(`Note: ${data.note}`)
} else {
Logger.error(`Failed to update variable: ${error.message}`)
}
}

private async parseInput(options: CommandActionData['options']): Promise<{
environmentSlug: string
version: string
}> {
const { environmentSlug, version } = options

return {
environmentSlug,
version
}
}
}
Loading

0 comments on commit 32d93e6

Please sign in to comment.