From 630e648c76ca394cf57cb93611dd9c29f1f22f46 Mon Sep 17 00:00:00 2001 From: Aryan Jassal Date: Mon, 2 Sep 2024 16:43:20 +1000 Subject: [PATCH] chore: updated functionality --- src/secrets/CommandEdit.ts | 82 +++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/src/secrets/CommandEdit.ts b/src/secrets/CommandEdit.ts index 1da7ddb5..13c416ef 100644 --- a/src/secrets/CommandEdit.ts +++ b/src/secrets/CommandEdit.ts @@ -54,61 +54,63 @@ class CommandEdit extends CommandPolykey { }, logger: this.logger.getChild(PolykeyClient.name), }); - const response = await binUtils.retryAuthentication( - async (auth) => { + const [secretExists, secretContent] = + await binUtils.retryAuthentication(async (auth) => { + let secretExists: boolean = true; + let response; try { - await pkClient.rpcClient.methods.vaultsSecretsStat({ + response = await pkClient.rpcClient.methods.vaultsSecretsGet({ metadata: auth, nameOrId: secretPath[0], secretName: secretPath[1], }); } catch (e) { - if (e.cause.name === 'ErrorSecretsSecretUndefined') { - await pkClient.rpcClient.methods.vaultsSecretsNew({ - metadata: auth, - nameOrId: secretPath[0], - secretName: secretPath[1], - secretContent: '', - }); - } else { - throw e; - } + if (e.cause.name === 'ErrorSecretsSecretUndefined') + secretExists = false; + else throw e; } - return await pkClient.rpcClient.methods.vaultsSecretsGet({ - metadata: auth, - nameOrId: secretPath[0], - secretName: secretPath[1], - }); - }, - - meta, - ); - const secretContent = response.secretContent; + return [secretExists, response?.secretContent]; + }, meta); const tmpDir = await fs.promises.mkdtemp( path.join(os.tmpdir(), 'polykey-'), ); const tmpFile = path.join(tmpDir, 'pksecret'); - await this.fs.promises.writeFile(tmpFile, secretContent); + if (secretExists) + await this.fs.promises.writeFile(tmpFile, secretContent); execSync(`$EDITOR \"${tmpFile}\"`, { stdio: 'inherit' }); - let content: Buffer; + let content: string; try { - content = await this.fs.promises.readFile(tmpFile); + let tmpContent = await this.fs.promises.readFile(tmpFile); + content = tmpContent.toString('binary'); + if (!secretExists) { + await binUtils.retryAuthentication(async (auth) => { + await pkClient.rpcClient.methods.vaultsSecretsNew({ + metadata: auth, + nameOrId: secretPath[0], + secretName: secretPath[1], + secretContent: content, + }); + await pkClient.rpcClient.methods.vaultsSecretsEdit({ + metadata: auth, + nameOrId: secretPath[0], + secretName: secretPath[1], + secretContent: content, + }); + }); + } } catch (e) { - throw new errors.ErrorPolykeyCLIFileRead(e.message, { - data: { - errno: e.errno, - syscall: e.syscall, - code: e.code, - path: e.path, - }, - cause: e, - }); + if (secretExists) { + throw new errors.ErrorPolykeyCLIFileRead(e.message, { + data: { + errno: e.errno, + syscall: e.syscall, + code: e.code, + path: e.path, + }, + cause: e, + }); + } } - await pkClient.rpcClient.methods.vaultsSecretsEdit({ - nameOrId: secretPath[0], - secretName: secretPath[1], - secretContent: content.toString('binary'), - }); await this.fs.promises.rm(tmpDir, { recursive: true, force: true }); // Windows // TODO: complete windows impl