diff --git a/bashUtils/auto-completion.sh b/bashUtils/auto-completion.sh index da80f22..b73df89 100644 --- a/bashUtils/auto-completion.sh +++ b/bashUtils/auto-completion.sh @@ -2,25 +2,6 @@ autoload -U compinit compinit -function findJJSource { - # @source http://stackoverflow.com/a/246128/330439 - local SOURCE="$jjCpmmandRunPoint" - local DIR="" - while [[ -h "$SOURCE" ]]; do - DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" - SOURCE="$(readlink "$SOURCE")" - [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" - done - DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )" - while [[ ! -e "${DIR}/jj.filelocation" ]]; do - DIR=$(dirname "${DIR}") - done - echo "${DIR}" -} - - -export JJ_SOURCE_DIR=$(findJJSource) - if [[ -e "$JJ_SOURCE_DIR/~~tmp/shEnvs.sh" ]]; then source $JJ_SOURCE_DIR/~~tmp/shEnvs.sh fi @@ -29,7 +10,6 @@ fi typeset -A opt_args function _AuctoCompletion(){ - # echo $words COMMANDS_WITH_HELP=$($JJ_SOURCE_DIR/bashUtils/auto-completion.ts "1" $words ) COMMANDS=$($JJ_SOURCE_DIR/bashUtils/auto-completion.ts "0" $words ) diff --git a/bashUtils/auto-completion.ts b/bashUtils/auto-completion.ts index a35ab6f..9682a75 100755 --- a/bashUtils/auto-completion.ts +++ b/bashUtils/auto-completion.ts @@ -1,6 +1,7 @@ #!/usr/bin/env bun import { bashRunAndReturnSync } from "@helpers/bashUtils" +import { dynamicStringHandler } from "@helpers/dynamicStringHandler" import { getJJConfigByKeyChain } from "@helpers/jjConfigUtils" async function main() { @@ -33,9 +34,14 @@ async function main() { } const oldArgOrder = remainingArgsCount remainingArgsCount = 0 - const command = + let command = matchingCommand?.commandForRetrieveKeys + ` ${matchingCommand?.commandForRetrieveKeys?.includes("auto-completion") ? ` auto-completion ${process.argv.join(" ")}` : ""}` - const res = await bashRunAndReturnSync(command) + command = dynamicStringHandler(command, "", process.env, false) + // console.log(command); + const res = await bashRunAndReturnSync({ + command + }) + // console.log(res) const newCommands = res?.split("\n") || [] newCommands.push(String("")) lastArrayOfKeys = [] diff --git a/bashUtils/discover.ts b/bashUtils/discover.ts new file mode 100755 index 0000000..416bb12 --- /dev/null +++ b/bashUtils/discover.ts @@ -0,0 +1,12 @@ +#!/usr/bin/env bun + +import { getJJConfigByKeyChain } from "@helpers/jjConfigUtils" + +const firstArg = process.argv[2] + +if(firstArg == 'jj-environments-keys'){ + const environments = getJJConfigByKeyChain('environments') + console.log(Object.keys(environments).join("\n")) +} + +process.exit(0) \ No newline at end of file diff --git a/bun.lockb b/bun.lockb index 350ea08..62c3c1a 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/cli/help.ts b/cli/help.ts index 007d798..85ee61c 100644 --- a/cli/help.ts +++ b/cli/help.ts @@ -48,7 +48,9 @@ const cliHelp = async () => { if (process.env.JJ_CLI_ARG_2 == "auto-completion") { // const aaa = getJJConfigByKeyChain(`commands`) - console.log(`add this to .zshrc: "source ${process.env.JJ_SOURCE_DIR}/bashUtils/auto-completion.sh" `) + console.log(`add this to .zshrc: ===> `) + console.log(`export JJ_SOURCE_DIR="${process.env.JJ_SOURCE_DIR}"`) + console.log(`source ${process.env.JJ_SOURCE_DIR}/bashUtils/auto-completion.sh`) return } console.log(chalk.blue.italic.bold("Usage: jj ")) diff --git a/cli/secrets.ts b/cli/secrets.ts new file mode 100644 index 0000000..8d80d36 --- /dev/null +++ b/cli/secrets.ts @@ -0,0 +1,107 @@ +import { dynamicStringHandler } from "@helpers/dynamicStringHandler"; +import { getJJConfigByKeyChain } from "@helpers/jjConfigUtils" +import { makeJJlogger } from "@helpers/jjLogger"; +import _ from 'lodash'; +import fs from 'fs-extra'; +import path from "path"; +import { existsSync } from 'fs'; +import { jjEncrypt } from "@helpers/cryptoUtils"; +import { saveFileIfItChanged } from "@helpers/saveFileIfItChanged"; + +const logger = makeJJlogger({ + filename: 'jj/cli/secrets.ts' +}) + + +const getSecretFolder = () => { + let secretsFolder = getJJConfigByKeyChain(`secretsFolder`) + secretsFolder = dynamicStringHandler(secretsFolder, "", process.env, false) + return secretsFolder +} + + +const getEncryptionKeyForEnv = (envKey) => { + const secretsFolder = getSecretFolder() + if (!fs.existsSync(`${secretsFolder}/~~${envKey}`)) { + logger.error(`Encryption key for ${envKey} env not fond and nt exist `) + process.exit(1) + } + const encryptionKey = fs.readFileSync(`${secretsFolder}/~~${envKey}`, 'utf8') + return encryptionKey +} + + +const setSecretFile = async ({ + absFilePath, + secretKey, + secretValue +}) => { + + if (!fs.existsSync(absFilePath)) { + logger.error(`Secret ${absFilePath} is not exist`) + } + console.log('asdasd') + const envKey = path.basename(path.join(absFilePath, "..")) + const secretFileObjRaw = await fs.readFile(absFilePath, 'utf8') + const secretObj = JSON.parse(secretFileObjRaw) + const encryptionKey = getEncryptionKeyForEnv(envKey) + + const encryptVal = jjEncrypt(secretValue, encryptionKey) + secretObj[secretKey.toUpperCase()] = `${envKey}--${encryptVal}` + saveFileIfItChanged(JSON.stringify(secretObj, null, 4), absFilePath) +} + + + +const secrets = async () => { + + const secretFilePath = process.env.JJ_CLI_ARG_3 + + const absFilePath = path.resolve(process.cwd(), secretFilePath) + const envKey = path.basename(path.join(absFilePath, "..")) + + const secretKey = process.env.JJ_CLI_ARG_4 + const secretValue = process.env.JJ_CLI_ARG_5 + + if (!envKey || !secretKey || !secretValue) { + logger.error(`!envKey || !secretKey || !secretValue`) + process.exit(1) + } + + const environment = getJJConfigByKeyChain(`environments['${envKey}']`) + + if (_.isEmpty(environment)) { + logger.error(`environment is not exist`) + process.exit(1) + } + + + if (process.env.JJ_CLI_ARG_2 == 'setSecret') { + + await setSecretFile({ + absFilePath, + secretKey, + secretValue + }) + + + process.exit(0) + } + + + // secretsFolder = dynamicStringHandler(secretsFolder, "", process.env, false) + // const encryptionKey = getEncryptionKeyForEnv(process.env.JJ_CLI_ARG_3) + + // console.log(encryptionKey) + + // console.log(secretsFolder) + + // console.log(process.env.JJ_CLI_ARG_2) + // console.log(process.env.JJ_CLI_ARG_4) + // console.log(process.env.JJ_CLI_ARG_5) + + + // console.log('secrets') +} + +export default secrets \ No newline at end of file diff --git a/helpers/cryptoUtils.ts b/helpers/cryptoUtils.ts new file mode 100644 index 0000000..fec54c5 --- /dev/null +++ b/helpers/cryptoUtils.ts @@ -0,0 +1,16 @@ + +import AES from 'crypto-js/AES'; +import CryptoJS from 'crypto-js'; + +export const jjDecrypt = (encryptedText, key) => { + const bytes = CryptoJS.AES.decrypt(encryptedText, key); + const originalText = bytes.toString(CryptoJS.enc.Utf8); + return originalText; +}; + + + +export const jjEncrypt = (text, key) => { + return AES.encrypt(text, key).toString(); +}; + diff --git a/helpers/hashUtils.ts b/helpers/hashUtils.ts new file mode 100644 index 0000000..e69de29 diff --git a/helpers/jjConfigUtils.ts b/helpers/jjConfigUtils.ts index b0a6f88..6aafabc 100644 --- a/helpers/jjConfigUtils.ts +++ b/helpers/jjConfigUtils.ts @@ -8,7 +8,7 @@ export const jjConfigsExp = JSON.parse(rawConfigData) export const getJjYamlObj = () => { return jjConfigsExp.jjYamlObj || {} } -export const getJJConfigByKeyChain = (chain) => { +export const getJJConfigByKeyChain: any = (chain) => { return _.get(getJjYamlObj(), chain, {}) } \ No newline at end of file diff --git a/package.json b/package.json index 7ae1ae1..3a39d97 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "bugs": "https://github.com/https://github.com/jafarijason/jj/jj/issues", "dependencies": { "chalk": "^5.3.0", + "crypto-js": "^4.2.0", "flatted": "^3.3.1", "fs-extra": "^11.2.0", "inquirer": "^12.0.1",