Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions lib/interactive/retrieve.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const processsecret = require('../processsecret');
const editSecrect = require('../editsecret');
const addKey = require('../addkey');
const deleteKey = require('../deletekey');
const { getCurrentContextNamespace } = require('../kube');

const beginDeletingKey = (chosenSecret) => {
inq.prompt([
Expand Down Expand Up @@ -263,23 +264,45 @@ const chooseNextActionWithGivenSecret = (chosenSecret) => {
});
};

const beginWithNamespace = ({
namespace = 'default',
const beginWithNamespace = async ({
namespace,
secretName,
}) => {
const kubectlOutput = shell.exec(`kubectl get secrets -n ${namespace} -o json`, { silent: true });
let selectedNamespace = namespace;
let selectedSecretName = secretName;

if (!selectedNamespace) {
selectedNamespace = getCurrentContextNamespace() || 'default';
}
console.log(`Namespace: ${selectedNamespace}`);

const options = namespace ? `-n ${namespace}` : '';
const kubectlOutput = shell.exec(`kubectl get secrets ${options} -o json`, { silent: true });
const secrets = processsecret.processSecrets(kubectlOutput);
if (secrets.result === 'success') {
inq.prompt([

if (secrets.result !== 'success') {
return;
}

if (!selectedSecretName) {
const secretListChoice = await inq.prompt([
{
type: 'rawlist',
name: 'secretName',
choices: Object.keys(secrets.secrets),
message: 'Choose a secret to work with:',
pageSize: 200,
},
]).then((secretListChoice) => {
chooseNextActionWithGivenSecret(secrets.secrets[secretListChoice.secretName]);
});
]);
selectedSecretName = secretListChoice.secretName;
}

if (!(selectedSecretName in secrets.secrets)) {
throw new Error(`Secret "${selectedSecretName}" not found in namespace "${selectedNamespace}"`);
}

if (selectedSecretName) {
chooseNextActionWithGivenSecret(secrets.secrets[selectedSecretName]);
}
};

Expand Down
3 changes: 3 additions & 0 deletions lib/kube.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const yaml = require('js-yaml');
const fs = require('fs');
const processSecret = require('./processsecret');

const getCurrentContextNamespace = () => shell.exec('kubectl config view --minify --output \'jsonpath={..namespace}\'', { silent: true }).stdout;

const getSingleSecret = ({
namespace,
name,
Expand Down Expand Up @@ -49,6 +51,7 @@ ${secretInYamlForm}`);
};

module.exports = {
getCurrentContextNamespace,
getSingleSecret,
applySecret,
convertSecretToYaml,
Expand Down
14 changes: 11 additions & 3 deletions scripts/kubesecret-get.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
const program = require('commander');
const interactiveget = require('../lib/interactive/retrieve');
const { red } = require('chalk');

program.name('kubesecret get');
program
.option('-n --namespace <namespace>', 'Namespace to filter by')
.arguments('[secret-name]')
.action((secretName) => {
console.log(`Working with namespace ${program.namespace}`);
interactiveget.beginWithNamespace({ namespace: program.namespace });
.action(async (secretName) => {
try {
await interactiveget.beginWithNamespace({
namespace: program.namespace,
secretName,
});
} catch (err) {
console.log(red(`${err.message}`));
process.exit(1);
}
});

program.parse(process.argv);