This commit is contained in:
2024-11-13 01:17:25 -05:00
parent 0ed24ed43b
commit 9ac157315a
10 changed files with 148 additions and 24 deletions
-20
View File
@@ -2,25 +2,6 @@
autoload -U compinit autoload -U compinit
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 if [[ -e "$JJ_SOURCE_DIR/~~tmp/shEnvs.sh" ]]; then
source $JJ_SOURCE_DIR/~~tmp/shEnvs.sh source $JJ_SOURCE_DIR/~~tmp/shEnvs.sh
fi fi
@@ -29,7 +10,6 @@ fi
typeset -A opt_args typeset -A opt_args
function _AuctoCompletion(){ function _AuctoCompletion(){
# echo $words
COMMANDS_WITH_HELP=$($JJ_SOURCE_DIR/bashUtils/auto-completion.ts "1" $words ) COMMANDS_WITH_HELP=$($JJ_SOURCE_DIR/bashUtils/auto-completion.ts "1" $words )
COMMANDS=$($JJ_SOURCE_DIR/bashUtils/auto-completion.ts "0" $words ) COMMANDS=$($JJ_SOURCE_DIR/bashUtils/auto-completion.ts "0" $words )
+8 -2
View File
@@ -1,6 +1,7 @@
#!/usr/bin/env bun #!/usr/bin/env bun
import { bashRunAndReturnSync } from "@helpers/bashUtils" import { bashRunAndReturnSync } from "@helpers/bashUtils"
import { dynamicStringHandler } from "@helpers/dynamicStringHandler"
import { getJJConfigByKeyChain } from "@helpers/jjConfigUtils" import { getJJConfigByKeyChain } from "@helpers/jjConfigUtils"
async function main() { async function main() {
@@ -33,9 +34,14 @@ async function main() {
} }
const oldArgOrder = remainingArgsCount const oldArgOrder = remainingArgsCount
remainingArgsCount = 0 remainingArgsCount = 0
const command = let command =
matchingCommand?.commandForRetrieveKeys + ` ${matchingCommand?.commandForRetrieveKeys?.includes("auto-completion") ? ` auto-completion ${process.argv.join(" ")}` : ""}` 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") || [] const newCommands = res?.split("\n") || []
newCommands.push(String("")) newCommands.push(String(""))
lastArrayOfKeys = [] lastArrayOfKeys = []
+12
View File
@@ -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)
BIN
View File
Binary file not shown.
+3 -1
View File
@@ -48,7 +48,9 @@ const cliHelp = async () => {
if (process.env.JJ_CLI_ARG_2 == "auto-completion") { if (process.env.JJ_CLI_ARG_2 == "auto-completion") {
// const aaa = getJJConfigByKeyChain(`commands`) // 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 return
} }
console.log(chalk.blue.italic.bold("Usage: jj <COMMAND>")) console.log(chalk.blue.italic.bold("Usage: jj <COMMAND>"))
+107
View File
@@ -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
+16
View File
@@ -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();
};
View File
+1 -1
View File
@@ -8,7 +8,7 @@ export const jjConfigsExp = JSON.parse(rawConfigData)
export const getJjYamlObj = () => { return jjConfigsExp.jjYamlObj || {} } export const getJjYamlObj = () => { return jjConfigsExp.jjYamlObj || {} }
export const getJJConfigByKeyChain = (chain) => { export const getJJConfigByKeyChain: any = (chain) => {
return _.get(getJjYamlObj(), chain, {}) return _.get(getJjYamlObj(), chain, {})
} }
+1
View File
@@ -9,6 +9,7 @@
"bugs": "https://github.com/https://github.com/jafarijason/jj/jj/issues", "bugs": "https://github.com/https://github.com/jafarijason/jj/jj/issues",
"dependencies": { "dependencies": {
"chalk": "^5.3.0", "chalk": "^5.3.0",
"crypto-js": "^4.2.0",
"flatted": "^3.3.1", "flatted": "^3.3.1",
"fs-extra": "^11.2.0", "fs-extra": "^11.2.0",
"inquirer": "^12.0.1", "inquirer": "^12.0.1",