Skip to content

Commit

Permalink
feat(defaults): add default value feature
Browse files Browse the repository at this point in the history
- when creating a new drive, the values entered the first time are preserved
- when you create a new drive with the same provider, the default values are
  sused to fill in the fields (such as base path, client ID, client secret, etc)
  • Loading branch information
gamemaker1 committed May 26, 2021
1 parent ef9aad2 commit dcc7e10
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/commands/new-drive.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const setupDrive = async (
field.prompt!,
field.description,
field.type,
Config.get(`defaults.${providerId}.${field.path}`),
)

Logger.debug(
Expand All @@ -101,6 +102,9 @@ const setupDrive = async (
// Store it in the configuration file
Config.set(`drives.${driveName}.${field.path}`, fieldValue)

// Also store it as the default value
Config.set(`defaults.${providerId}.${field.path}`, fieldValue)

Logger.debug(
`command.new-drive.setupDrives: set value ${Config.get(
`drives.${driveName}.${field.path}`,
Expand Down Expand Up @@ -140,12 +144,14 @@ const setupDrive = async (
'Enter the client ID:',
providerDetails.authDetails.instructions,
'string',
Config.get(`defaults.${providerId}.authMeta.clientId`),
)
const { fieldValue: clientSecret } =
await Prompts.getFieldValueFromUser(
'Enter the client secret:',
undefined,
'string',
Config.get(`defaults.${providerId}.authMeta.clientSecret`),
)

Logger.debug(
Expand All @@ -163,6 +169,13 @@ const setupDrive = async (
providerDetails.authDetails.redirectUri,
)

// Also store them as defaults if defaults do not exist
Config.set(`defaults.${providerId}.authMeta.clientId`, clientId)
Config.set(
`defaults.${providerId}.authMeta.clientSecret`,
clientSecret,
)

Logger.debug(
`command.new-drive.setupDrives: requesting user authorization`,
)
Expand Down
4 changes: 3 additions & 1 deletion src/ui/prompts.ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ export const getFieldValueFromUser = (
ps: string,
description: string | undefined,
type: 'string' | 'number',
defaultValue: string | undefined = undefined,
): Promise<{ fieldValue: string }> => {
Logger.debug(
`ui.prompts.getFieldValueFromUser: requesting user to enter field value: prompt: ${ps}; description: ${description}; type: ${type}`,
`ui.prompts.getFieldValueFromUser: requesting user to enter field value: prompt: ${ps}; description: ${description}; type: ${type}; defaultValue: ${defaultValue}`,
)

let promptType: string
Expand All @@ -94,6 +95,7 @@ export const getFieldValueFromUser = (
type: promptType,
name: 'fieldValue',
message: Chalk.keyword('orange')(ps),
initial: defaultValue,
})
}

Expand Down

0 comments on commit dcc7e10

Please sign in to comment.