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

refactor(cli): Refactored profile commands into readable blocks #331

Merged
merged 1 commit into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion apps/cli/src/commands/base.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '../types/command/command.types'
import { fetchProfileConfig } from '../util/configuration'
import Logger from '../util/logger'
import { getDefaultProfile } from '../util/profile'

/**
* The base class for all commands. All commands should extend this class.
Expand Down Expand Up @@ -143,7 +144,7 @@ export default abstract class BaseCommand {
this.baseUrl = globalOptions.baseUrl
}
} else {
const defaultProfileName = profiles.default
const defaultProfileName = getDefaultProfile(profiles)
const defaultProfile = profiles[defaultProfileName]

if (defaultProfile) {
Expand Down
41 changes: 25 additions & 16 deletions apps/cli/src/commands/profile/create.profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,32 @@ export default class CreateProfile extends BaseCommand {
}

async action({ options }: CommandActionData): Promise<void> {
intro('Creating a new profile')

const { name, apiKey, baseUrl, setDefault } = await this.parseInput(options)

this.profiles = await fetchProfileConfig()
await this.checkOverwriteExistingProfile(name)

const s = spinner()
s.start('Saving changes...')

this.setProfileConfigData(name, apiKey, baseUrl, setDefault)
await writeProfileConfig(this.profiles)

s.stop()
outro(`Profile ${name} created successfully`)
}

private async parseInput(options: CommandActionData['options']): Promise<{
name: string
apiKey: string
baseUrl: string
setDefault: boolean
}> {
let { name, apiKey } = options
const { baseUrl, setDefault } = options

intro('Creating a new profile')

if (!name) {
name = await text({
message: 'Enter the name of the profile',
Expand All @@ -69,19 +90,7 @@ export default class CreateProfile extends BaseCommand {
})
}

this.profiles = await fetchProfileConfig()
await this.checkOverwriteExistingProfile(name)

const s = spinner()
s.start('Saving changes...')

this.setProfileData(name, apiKey, baseUrl, setDefault)

await writeProfileConfig(this.profiles)

s.stop()

outro(`Profile ${name} created successfully`)
return { name, apiKey, baseUrl, setDefault }
}

private async checkOverwriteExistingProfile(name: string): Promise<boolean> {
Expand All @@ -97,7 +106,7 @@ export default class CreateProfile extends BaseCommand {
}
}

private setProfileData(
private setProfileConfigData(
name: string,
apiKey: string,
baseUrl: string,
Expand Down
34 changes: 12 additions & 22 deletions apps/cli/src/commands/profile/delete.profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
writeProfileConfig
} from '../../util/configuration'
import { ProfileConfig } from 'src/types/index.types'
import { checkIsDefaultProfile, checkProfileExists } from 'src/util/profile'

export default class DeleteProfile extends BaseCommand {
profiles: ProfileConfig
Expand All @@ -32,13 +33,20 @@ export default class DeleteProfile extends BaseCommand {
s.start('Deleting the profile')

this.profiles = await fetchProfileConfig()
checkProfileExists(this.profiles, profile, s)
await this.makeConfirmation(profile, s)

if (this.profileNotFound(profile)) {
s.stop(`Profile ${profile} not found`)
return
this.profiles[profile] = undefined
if (checkIsDefaultProfile(this.profiles, profile)) {
delete this.profiles.default
}
await writeProfileConfig(this.profiles)

s.stop(`Profile ${profile} deleted`)
}

if (this.isDefaultProfile(profile)) {
private async makeConfirmation(profile: string, s: any) {
if (checkIsDefaultProfile(this.profiles, profile)) {
const choice = await confirm({
message: 'Are you sure you want to delete the default profile?'
})
Expand All @@ -48,23 +56,5 @@ export default class DeleteProfile extends BaseCommand {
return
}
}

this.profiles[profile] = undefined

if (this.isDefaultProfile(profile)) {
delete this.profiles.default
}

await writeProfileConfig(this.profiles)

s.stop(`Profile ${profile} deleted`)
}

private profileNotFound(profile: string): boolean {
return !this.profiles[profile]
}

private isDefaultProfile(profile: string): boolean {
return this.profiles.default === profile
}
}
40 changes: 26 additions & 14 deletions apps/cli/src/commands/profile/list.profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import {
import BaseCommand from '../base.command'
import { fetchProfileConfig } from '../../util/configuration'
import Logger from '../../util/logger'
import { ProfileConfig } from '../../types/index.types'
import { getDefaultProfile } from '../../util/profile'

export default class ListProfile extends BaseCommand {
private profiles: ProfileConfig

getName(): string {
return 'list'
}
Expand All @@ -29,22 +33,30 @@ export default class ListProfile extends BaseCommand {
async action({ options }: CommandActionData): Promise<void> {
const { verbose } = options

const profiles = await fetchProfileConfig()
const defaultProfile = profiles.default
delete profiles.default
this.profiles = await fetchProfileConfig()
const defaultProfile = getDefaultProfile(this.profiles)
delete this.profiles.default

Logger.log('Profiles:')
Object.keys(profiles).forEach((profile) => {
if (defaultProfile === profile) {
Logger.log(`- ${profile} (default)`)
} else {
Logger.log(`- ${profile}`)
}
Object.keys(this.profiles).forEach((profile) =>
this.printProfile(profile, defaultProfile, verbose)
)
}

if (verbose) {
Logger.log(` - API Key: ${profiles[profile].apiKey}`)
Logger.log(` - Base URL: ${profiles[profile].baseUrl}`)
}
})
private printProfile(
profile: string,
defaultProfile: string,
verbose: boolean
): void {
if (defaultProfile === profile) {
Logger.log(`- ${profile} (default)`)
} else {
Logger.log(`- ${profile}`)
}

if (verbose) {
Logger.log(` - API Key: ${this.profiles[profile].apiKey}`)
Logger.log(` - Base URL: ${this.profiles[profile].baseUrl}`)
}
}
}
23 changes: 14 additions & 9 deletions apps/cli/src/commands/profile/update.profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
writeProfileConfig
} from '../../util/configuration'
import { spinner } from '@clack/prompts'
import { checkProfileExists, checkIsDefaultProfile } from '../../util/profile'

export default class UpdateProfile extends BaseCommand {
private profiles: ProfileConfig
Expand Down Expand Up @@ -55,12 +56,20 @@ export default class UpdateProfile extends BaseCommand {
const s = spinner()
s.start('Updating the profile')

if (!this.profiles[profile]) {
s.stop(`Profile ${profile} not found`)
return
}
checkProfileExists(this.profiles, profile, s)
this.updateProfileData(profile, name, apiKey, baseUrl)
await writeProfileConfig(this.profiles)

s.stop(`Profile ${profile} updated`)
}

const isDefaultProfile = this.profiles.default === profile
private updateProfileData(
profile: string,
name: string,
apiKey: string,
baseUrl: string
): void {
const isDefaultProfile = checkIsDefaultProfile(this.profiles, profile)

if (apiKey) {
this.profiles[profile].apiKey = apiKey
Expand All @@ -77,9 +86,5 @@ export default class UpdateProfile extends BaseCommand {
this.profiles.default = name
}
}

await writeProfileConfig(this.profiles)

s.stop(`Profile ${profile} updated`)
}
}
6 changes: 2 additions & 4 deletions apps/cli/src/commands/profile/use.profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
fetchProfileConfig,
writeProfileConfig
} from '../../util/configuration'
import { checkProfileExists } from '../../util/profile'

export default class UseProfile extends BaseCommand {
getName(): string {
Expand All @@ -30,10 +31,7 @@ export default class UseProfile extends BaseCommand {

const profiles = await fetchProfileConfig()

if (!profiles[profile]) {
s.stop(`Profile ${profile} not found`)
return
}
checkProfileExists(profiles, profile, s)

// Set the active profile
profiles.default = profile
Expand Down
25 changes: 25 additions & 0 deletions apps/cli/src/util/profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ProfileConfig } from '../types/index.types'

export const checkProfileExists = (
profiles: ProfileConfig,
profile: string,
s?: any
): void => {
if (!profiles[profile]) {
if (s) {
s.stop(`Profile ${profile} not found`)
return
} else throw new Error(`Profile ${profile} not found`)
}
}

export const checkIsDefaultProfile = (
profiles: ProfileConfig,
profile: string
): boolean => {
return profiles.default === profile
}

export const getDefaultProfile = (profiles: ProfileConfig): string => {
return profiles.default
}