WIP
This commit is contained in:
+3
-1
@@ -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 <COMMAND>"))
|
||||
|
||||
+107
@@ -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
|
||||
Reference in New Issue
Block a user