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): Store metrics_enabled key in profile config #536

Merged
merged 2 commits into from
Nov 17, 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: 3 additions & 0 deletions apps/cli/src/commands/base.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default abstract class BaseCommand {
// Use these fields from the subclasses to make API requests if needed
protected apiKey: string | null = null
protected baseUrl: string | null = null
protected metricsEnabled: boolean | null = null

// Headers to be used by the API requests
protected headers: Record<string, string> | null = null
Expand Down Expand Up @@ -150,6 +151,7 @@ export default abstract class BaseCommand {

this.apiKey = profile.apiKey
this.baseUrl = profile.baseUrl
this.metricsEnabled = profile.metrics_enabled
} else {
throw new Error('Profile not found')
}
Expand All @@ -167,6 +169,7 @@ export default abstract class BaseCommand {
if (defaultProfile) {
this.apiKey = defaultProfile.apiKey
this.baseUrl = defaultProfile.baseUrl
this.metricsEnabled = defaultProfile.metrics_enabled
}
}

Expand Down
38 changes: 31 additions & 7 deletions apps/cli/src/commands/profile/create.profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,30 @@ export default class CreateProfile extends BaseCommand {
long: '--set-default',
description: 'Set the profile as the default profile',
defaultValue: false
},
{
short: '-m',
long: '--enable-metrics',
description:
'Should keyshade collect anonymous metrics for development?',
defaultValue: false
}
]
}

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

const { name, apiKey, baseUrl, setDefault } = await this.parseInput(options)
const { name, apiKey, baseUrl, setDefault, enableMetrics } =
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)
this.setProfileConfigData(name, apiKey, baseUrl, setDefault, enableMetrics)
await writeProfileConfig(this.profiles)

s.stop()
Expand All @@ -70,8 +78,9 @@ export default class CreateProfile extends BaseCommand {
apiKey?: string
baseUrl?: string
setDefault?: boolean
enableMetrics?: boolean
}> {
let { name, apiKey, baseUrl, setDefault } = options
let { name, apiKey, baseUrl, setDefault, enableMetrics } = options

if (!name) {
name = await text({
Expand All @@ -87,6 +96,12 @@ export default class CreateProfile extends BaseCommand {
})
}

if (!enableMetrics === undefined) {
enableMetrics = await confirm({
message: 'Should keyshade collect anonymous metrics for development?'
})
}

const inputSchema = z.object({
name: z
.string()
Expand All @@ -101,11 +116,18 @@ export default class CreateProfile extends BaseCommand {
'API key must start with "ks_" and contain only letters and numbers.'
),
baseUrl: z.string().url().or(z.string().length(0)).optional(),
setDefault: z.boolean().optional()
setDefault: z.boolean().optional(),
enableMetrics: z.boolean().optional()
})

// Validate the collected data
const parsedData = inputSchema.parse({ name, apiKey, baseUrl, setDefault })
const parsedData = inputSchema.parse({
name,
apiKey,
baseUrl,
setDefault,
enableMetrics
})

return parsedData
}
Expand All @@ -126,15 +148,17 @@ export default class CreateProfile extends BaseCommand {
name: string,
apiKey: string,
baseUrl: string,
setDefault: boolean
setDefault: boolean,
enableMetrics: boolean
): void {
if (setDefault) {
this.profiles.default = name
}

this.profiles[name] = {
apiKey,
baseUrl
baseUrl,
metrics_enabled: enableMetrics
}
}
}
5 changes: 3 additions & 2 deletions apps/cli/src/commands/profile/list.profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ export default class ListProfile extends BaseCommand {
profileList.push([
`${defaultProfile === profile ? `${profile} (default)` : profile}`,
`${profiles[profile].apiKey}`,
`${profiles[profile].baseUrl}`
`${profiles[profile].baseUrl}`,
`${profiles[profile].metrics_enabled ? 'Yes' : 'No'}`
])
})
table.push(
['Profile', 'API Key', 'Base URL'],
['Profile', 'API Key', 'Base URL', 'Metrics Enabled'],
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
...profileList
)
Expand Down
18 changes: 15 additions & 3 deletions apps/cli/src/commands/profile/update.profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export default class UpdateProfile extends BaseCommand {
short: '-b',
long: '--base-url <string>',
description: 'New base URL for the keyshade server'
},
{
short: '-m',
long: '--enable-metrics <boolean>',
description:
'Should keyshade collect anonymous metrics for development?'
}
]
}
Expand All @@ -48,7 +54,7 @@ export default class UpdateProfile extends BaseCommand {
this.profiles = await fetchProfileConfig()

const profile = args[0]
const { name, apiKey, baseUrl } = options
const { name, apiKey, baseUrl, enableMetrics } = options

const s = spinner()
s.start('Updating the profile')
Expand All @@ -58,7 +64,8 @@ export default class UpdateProfile extends BaseCommand {
profile,
name as string,
apiKey as string,
baseUrl as string
baseUrl as string,
enableMetrics as boolean
)
await writeProfileConfig(this.profiles)

Expand All @@ -69,7 +76,8 @@ export default class UpdateProfile extends BaseCommand {
profile: string,
name: string,
apiKey: string,
baseUrl: string
baseUrl: string,
enableMetrics: boolean
): void {
const isDefaultProfile = checkIsDefaultProfile(this.profiles, profile)

Expand All @@ -81,6 +89,10 @@ export default class UpdateProfile extends BaseCommand {
this.profiles[profile].baseUrl = baseUrl
}

if (enableMetrics !== undefined) {
this.profiles[profile].metrics_enabled = enableMetrics
}

if (name) {
this.profiles[name] = this.profiles[profile]
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
Expand Down
1 change: 1 addition & 0 deletions apps/cli/src/types/index.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ProfileConfig {
[name: string]: {
apiKey: string
baseUrl: string
metrics_enabled: boolean
}
}

Expand Down
Loading