Skip to content

Commit

Permalink
give feedback if already initialized (bluesky-social#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms authored Apr 28, 2022
1 parent ada695c commit ed348e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
10 changes: 9 additions & 1 deletion cli/src/commands/setup/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ export default cmd({
let { username, server, register, delegatorClient } = args

console.log(`Repo path: ${REPO_PATH}`)

const exists = await config.cfgExists(REPO_PATH)
if (exists) {
console.log('Repo already exists.')
console.log('To overwrite, run `destroy`.')
console.log('If unregistered, run `register`.')
return
}

if (!username || !server) {
console.log(`This utility will initialize your sky repo.`)
console.log(`Press ^C at any time to quit.`)
Expand Down Expand Up @@ -63,7 +72,6 @@ export default cmd({
}

console.log('Generating repo...')

await config.writeCfg(REPO_PATH, username, server, delegatorClient)
const client = await loadClient(REPO_PATH)

Expand Down
37 changes: 12 additions & 25 deletions cli/src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,38 +87,25 @@ const readFile = async (
filename: string,
encoding?: BufferEncoding,
): Promise<string | Buffer> => {
try {
const value = await fsp.readFile(path.join(repoPath, filename), encoding)
if (!value) throw new Error(`${filename} file not found`)
return value
} catch (e) {
console.error(`Failed to read ${filename} file`)
console.error(e)
process.exit(1)
}
const value = await fsp.readFile(path.join(repoPath, filename), encoding)
if (!value) throw new Error(`${filename} file not found`)
return value
}

const readAccountFile = async (
repoPath: string,
filename: string,
): Promise<AccountJson> => {
const str = (await readFile(repoPath, filename, 'utf-8')) as string
try {
const obj = JSON.parse(str)
const { username, server, delegator } = obj
if (!username || typeof username !== 'string')
throw new Error('"username" is invalid')
if (!server || typeof server !== 'string')
throw new Error('"server" is invalid')
if (typeof delegator !== 'boolean')
throw new Error('"delegator" is invalid')
const serverCleaned = cleanHost(server)
return { username, server: serverCleaned, delegator }
} catch (e) {
console.error(`Failed to load ${filename} file`)
console.error(e)
process.exit(1)
}
const obj = JSON.parse(str)
const { username, server, delegator } = obj
if (!username || typeof username !== 'string')
throw new Error('"username" is invalid')
if (!server || typeof server !== 'string')
throw new Error('"server" is invalid')
if (typeof delegator !== 'boolean') throw new Error('"delegator" is invalid')
const serverCleaned = cleanHost(server)
return { username, server: serverCleaned, delegator }
}

export const readRoot = async (repoPath: string): Promise<CID | null> => {
Expand Down

0 comments on commit ed348e5

Please sign in to comment.