-
-
Notifications
You must be signed in to change notification settings - Fork 124
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 Variables (#514)
Co-authored-by: codiumai-pr-agent-free[bot] <138128286+codiumai-pr-agent-free[bot]@users.noreply.github.com>
- Loading branch information
1 parent
ee58f07
commit 32d93e6
Showing
8 changed files
with
450 additions
and
1 deletion.
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
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() | ||
] | ||
} | ||
} |
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,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 | ||
} | ||
} | ||
} |
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,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}`) | ||
} | ||
} | ||
} |
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,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}`) | ||
} | ||
} | ||
} |
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,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}`) | ||
} | ||
} | ||
} |
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,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 | ||
} | ||
} | ||
} |
Oops, something went wrong.