Skip to content

Commit

Permalink
fix: only allowed to set supported config (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
2nthony authored Jul 14, 2022
1 parent 2fe8a82 commit 60dc22a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
32 changes: 13 additions & 19 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import {
printConfig,
writeUserConfig,
ghqConfigFileName,
supportedConfigKeys,
defaultConfig,
} from '../config'
import { Config, OptionalConfig, PluginApi } from '../types'
import { PluginApi } from '../types'

export const config: PluginApi = {
extend(api) {
api.cli
const commandCli = api.cli
.command('config', 'Manage the ghq configuration file')
.option('--set <value>', 'Set a variable')
.example('ghq config --set.root ~/my-path')
.option('--get <value>', 'Get a variable')
.example('ghq config --get.root')
.option('--get', 'Get a variable')
.option('-l, --list', 'List all')
.action(async (options) => {
if (!(await existsUserConfig())) {
Expand All @@ -29,28 +29,22 @@ export const config: PluginApi = {
}

if (options.set) {
const { key, value } = firstArgPairs(options.set)
await writeUserConfig(key, value)
await printConfig(key)
await writeUserConfig(options.set)
await printConfig(options.set)
return
}

if (options.get) {
const { key } = firstArgPairs(options.get)
await printConfig(key)
await printConfig(options.get)
return
}

api.cli.outputHelp()
})
},
}

function firstArgPairs<K extends keyof Config>(optionalConfig: OptionalConfig) {
const [[key, value] = []] = Object.entries(optionalConfig)

return {
key,
value,
} as { key: K; value: Config[K] }
supportedConfigKeys.forEach((k) => {
commandCli.example(`ghq config --set.${k} [${typeof defaultConfig[k]}]`)
commandCli.example(`ghq config --get.${k}`)
})
},
}
29 changes: 16 additions & 13 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Config, OptionalConfig } from './types'
import { homedir } from 'os'
import { exists, read, writeJson } from './fs'
import { expandTildePath, join } from './path'
import { pick } from './helpers/pick'

export const ghqConfigFileName = '.ghqrc'
export const userConfigFilePath = join(homedir(), ghqConfigFileName)
Expand All @@ -11,6 +12,10 @@ export const defaultConfig: Config = {
shallow: false,
}

export const supportedConfigKeys = Object.keys(
defaultConfig,
) as (keyof Config)[]

export async function resolveConfig() {
const config = await readConfig()

Expand Down Expand Up @@ -40,30 +45,28 @@ export async function readUserConfig(): Promise<OptionalConfig> {
}
}

export async function writeUserConfig<K extends keyof Config>(
key: K,
value: Config[K],
) {
export async function writeUserConfig(options: OptionalConfig) {
try {
options = pick(options, supportedConfigKeys)

const config = await readConfig()
await writeJson(userConfigFilePath, {
...config,
[key]: value,
...options,
})
} catch (e) {
console.error(e)
}
}

export async function printConfig(key?: keyof Config) {
export async function printConfig<K extends keyof Config>(
expectConfig?: OptionalConfig,
) {
const config = await readConfig()

if (key) {
console.info(config[key] ?? '')
return
}

for (const [k, v] of Object.entries(config)) {
console.info(`${k}=${v}`)
for (const k of Object.keys(expectConfig ?? config)) {
if (supportedConfigKeys.includes(k as K)) {
console.info(`${k}=${config[k as K]}`)
}
}
}
9 changes: 9 additions & 0 deletions src/helpers/pick.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function pick<T extends object, K extends keyof T>(
obj: T,
keys: K[],
): Pick<T, K> {
return keys.reduce((val, k) => {
val[k] = obj[k]
return val
}, {} as T)
}

0 comments on commit 60dc22a

Please sign in to comment.