Skip to content

Commit

Permalink
added create and update commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdip-b committed Sep 21, 2024
1 parent feb37bf commit 83a2f83
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 9 deletions.
4 changes: 2 additions & 2 deletions apps/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"name": "cli",
"version": "1.0.0",
"description": "CLI for keyshade",
"main": "index.ts",
"main": "dist/src/index.js",
"private": false,
"type": "module",
"scripts": {
"build": "tsc && tsc-alias",
"start": "node dist/src/index.js",
"dev": "pnpm build && node dist/index.js",
"dev": "pnpm build && node dist/src/index.js",
"lint": "eslint \"src/**/*.ts\" --fix"
},
"keywords": [],
Expand Down
19 changes: 12 additions & 7 deletions apps/cli/src/commands/base.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
import { fetchProfileConfig } from '@/util/configuration'
import { Logger } from '@/util/logger'
import { getDefaultProfile } from '@/util/profile'
import type { Command } from 'commander'
import { Option, type Command } from 'commander'
import ControllerInstance from '@/util/controller-instance'

/**
Expand Down Expand Up @@ -55,13 +55,18 @@ export default abstract class BaseCommand {
}
})

this.getOptions().forEach((option) =>
command.option(
this.getOptions().forEach((option) => {
const newOption: Option = new Option(
`${option.short}, ${option.long}`,
option.description,
option.defaultValue
)
)
option.description
).default(option.defaultValue)

option.choices &&
option.choices.length > 0 &&
newOption.choices(option.choices)

command.addOption(newOption)
})
this.getArguments().forEach((argument) =>
command.argument(argument.name, argument.description)
)
Expand Down
92 changes: 92 additions & 0 deletions apps/cli/src/commands/project/create.project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import type {
CommandActionData,
CommandArgument,
CommandOption
} from '@/types/command/command.types'
import BaseCommand from '../base.command'
import { text } from '@clack/prompts'
import ControllerInstance from '@/util/controller-instance'
import { Logger } from '@/util/logger'

export default class CreateProject extends BaseCommand {
getName(): string {
Expand All @@ -8,4 +16,88 @@ export default class CreateProject extends BaseCommand {
getDescription(): string {
return 'Creates a project'
}

getArguments(): CommandArgument[] {
return [
{
name: '<Workspace Slug>',
description:
'Slug of the workspace under which you want to create the project'
}
]
}

getOptions(): CommandOption[] {
return [
{
short: '-n',
long: '--name <string>',
description: 'Name of the project'
},
{
short: '-d',
long: '--description <string>',
description: 'Description of the project. Defaults to project name'
},
{
short: '-k',
long: '--store-private-key',
description: 'Store the private key in the project. Defaults to true',
defaultValue: true
},
{
short: '-a',
long: '--access-level <string>',
description: 'Access level of the project. Defaults to PRIVATE.',
defaultValue: 'PRIVATE',
choices: ['GLOBAL', 'PRIVATE', 'INTERNAL']
}
]
}

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

const parsedData = await this.parseOptions(options)

const { data, error, success } =
await ControllerInstance.getInstance().projectController.createProject(
{
workspaceSlug,
...parsedData
},
this.headers
)

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

async parseOptions(options: CommandActionData['options']): Promise<{
name: string
description?: string
storePrivateKey: boolean
accessLevel: string
}> {
let { name, description } = options
const { storePrivateKey, accessLevel } = options

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

if (!description) {
description = name
}

return { name, description, storePrivateKey, accessLevel }
}
}
65 changes: 65 additions & 0 deletions apps/cli/src/commands/project/update.project.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import type {
CommandActionData,
CommandArgument,
CommandOption
} from '@/types/command/command.types'
import BaseCommand from '../base.command'
import ControllerInstance from '@/util/controller-instance'
import { Logger } from '@/util/logger'

export default class UpdateProject extends BaseCommand {
getName(): string {
Expand All @@ -8,4 +15,62 @@ export default class UpdateProject extends BaseCommand {
getDescription(): string {
return 'Updates a project'
}

getArguments(): CommandArgument[] {
return [
{
name: '<Project Slug>',
description: 'Slug of the project that you want to update'
}
]
}

getOptions(): CommandOption[] {
return [
{
short: '-n',
long: '--name <string>',
description: 'Name of the project'
},
{
short: '-d',
long: '--description <string>',
description: 'Description of the project. Defaults to project name'
},
{
short: '-k',
long: '--store-private-key',
description: 'Store the private key in the project. Defaults to true',
defaultValue: true
},
{
short: '-a',
long: '--access-level <string>',
description: 'Access level of the project. Defaults to PRIVATE.',
defaultValue: 'PRIVATE',
choices: ['GLOBAL', 'PRIVATE', 'INTERNAL']
}
]
}

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

const { data, error, success } =
await ControllerInstance.getInstance().projectController.updateProject(
{
projectSlug,
...options
},
this.headers
)

if (success) {
Logger.info(`Project ${data.name} (${data.slug}) updated successfully!`)
Logger.info(`Created at ${data.createdAt}`)
Logger.info(`Updated at ${data.updatedAt}`)
} else {
Logger.error(`Failed to update project: ${error.message}`)
}
}
}
1 change: 1 addition & 0 deletions apps/cli/src/types/command/command.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface CommandOption {
long: string
description: string
defaultValue?: string | boolean
choices?: string[]
}

export interface CommandArgument {
Expand Down

0 comments on commit 83a2f83

Please sign in to comment.