Skip to content

Commit

Permalink
chore: updated functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanjassal committed Sep 2, 2024
1 parent 8092393 commit 630e648
Showing 1 changed file with 42 additions and 40 deletions.
82 changes: 42 additions & 40 deletions src/secrets/CommandEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 630e648

Please sign in to comment.