Skip to content

Commit

Permalink
feat: cli support env (#1718)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyoct authored Dec 8, 2023
1 parent 6e41123 commit 80b88c0
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@
"lint-staged": {
"*.{ts,js}": "eslint --fix"
}
}
}
3 changes: 3 additions & 0 deletions cli/src/action/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as fs from 'node:fs'
import { pull as depPull } from '../dependency'
import { pullAll as policyPull } from '../policy'
import { pullAll as funcPull } from '../function'
import { pull as envPull } from '../environment'
import { AppSchema } from '../../schema/app'

import {
Expand Down Expand Up @@ -118,6 +119,8 @@ export async function init(appid: string, options: { sync: boolean; basicMode: b
policyPull()
// pull functions
funcPull({ force: true })
// pull env
envPull()
}
console.log(`${getEmoji('🚀')} application ${app.name} init success`)
}
Expand Down
18 changes: 18 additions & 0 deletions cli/src/action/environment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { environmentVariableControllerGet, environmentVariableControllerUpdateAll } from "../../api/v1/application";
import { AppSchema } from "../../schema/app";
import { EnvironmentSchema } from "../../schema/environment";
import { getEmoji } from "../../util/print";

export async function pull(): Promise<void> {
const appSchema = AppSchema.read()
const env = await environmentVariableControllerGet(appSchema.appid)
EnvironmentSchema.write(env)
console.log(`${getEmoji('✅')} env pulled`)
}

export async function push(): Promise<void> {
const appSchema = AppSchema.read()
const env = EnvironmentSchema.read()
await environmentVariableControllerUpdateAll(appSchema.appid, env)
console.log(`${getEmoji('✅')} env pushed`)
}
28 changes: 28 additions & 0 deletions cli/src/command/environment/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Command, program } from 'commander'
import { checkApplication } from '../../common/hook'
import { pull, push } from '../../action/environment'

export function command(): Command {
const cmd = program
.command('environment')
.alias('env')
.hook('preAction', () => {
checkApplication()
})

cmd
.command('pull')
.description('push environment variables')
.action(() => {
pull()
})

cmd
.command('push')
.description('pull environment variables')
.action(() => {
push()
})

return cmd
}
1 change: 1 addition & 0 deletions cli/src/common/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const USER_SCHEMA_NAME = 'user.yaml'
export const FUNCTION_SCHEMA_DIRECTORY = 'functions'
export const FUNCTION_SCHEMA_SUFFIX = '.yaml'
export const DEPLOY_SCHEMA_NAME = 'deploy.yaml'
export const ENVIRONMENT_SCHEMA_NAME = '.env'

export const IGNORE_FILE_NAME = '.gitignore'
export const POLICIES_DIRECTORY_NAME = 'policies'
Expand Down
3 changes: 3 additions & 0 deletions cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { command as websiteCommand } from './command/website'
import { command as deployCommand } from './command/deploy'
import { command as triggerCommand } from './command/trigger'
import { command as databaseCommand } from './command/database'
import { command as environmentCommand } from './command/environment'


const program = new Command()
program.option('-v, --version', 'output version').action((options) => {
Expand All @@ -34,5 +36,6 @@ program.addCommand(websiteCommand())
program.addCommand(deployCommand())
program.addCommand(triggerCommand())
program.addCommand(databaseCommand())
program.addCommand(environmentCommand())

program.parse(process.argv)
45 changes: 45 additions & 0 deletions cli/src/schema/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as path from 'node:path'
import * as fs from 'node:fs'
import { getAppPath } from '../util/sys'
import { ENVIRONMENT_SCHEMA_NAME } from '../common/constant'
import * as dotenv from 'dotenv'
import { exist } from '../util/file'


export class EnvironmentSchema {

variables: EnvironmentVariable[]


static read(): EnvironmentVariable[] {
const configPath = path.join(getAppPath(), ENVIRONMENT_SCHEMA_NAME)
if (!exist(configPath)) {
return null
}
const dataStr = fs.readFileSync(configPath, 'utf-8')
const data = dotenv.parse(dataStr)
const env: EnvironmentVariable[] = []
for (const key in data) {
env.push({
name: key,
value: data[key]
})
}
return env
}

static write(env: EnvironmentVariable[]): void {
const configPath = path.join(getAppPath(), ENVIRONMENT_SCHEMA_NAME)
let dataStr = ''
for (const item of env) {
dataStr += `${item.name}=${item.value}\n`
}
fs.writeFileSync(configPath, dataStr)
}

}

export interface EnvironmentVariable {
name: string
value: string
}

0 comments on commit 80b88c0

Please sign in to comment.