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

feat(cli): Add functionality to operate on Variables #514

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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()
]
}
}
119 changes: 119 additions & 0 deletions apps/cli/src/commands/variable/create.variable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
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 entryObj: { value: string; environmentSlug: string } = {
value: '',
environmentSlug: ''
}
entry.split(' ').forEach((pair) => {
const [key, value] = pair.split('=')
entryObj[key] = value
})
return entryObj
})
anudeeps352 marked this conversation as resolved.
Show resolved Hide resolved

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 variable = data
if (variable.length > 0) {
data.forEach((variable: any) => {
Logger.info(`- ${variable.name} (${variable.value})`)
})
} else {
Logger.info('No variable found')
rajdip-b marked this conversation as resolved.
Show resolved Hide resolved
}
} 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