@toa.io/cli 0.4.0-dev.8 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/cli",
3
- "version": "0.4.0-dev.8",
3
+ "version": "0.4.0",
4
4
  "description": "Toa CLI",
5
5
  "author": "temich <tema.gurtovoy@gmail.com>",
6
6
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -22,11 +22,12 @@
22
22
  "@toa.io/runtime": "*"
23
23
  },
24
24
  "dependencies": {
25
- "@toa.io/console": "0.4.0-dev.8",
26
- "@toa.io/generic": "0.4.0-dev.8",
27
- "@toa.io/kubernetes": "0.4.0-dev.8",
28
- "@toa.io/norm": "0.4.0-dev.8",
29
- "@toa.io/yaml": "0.4.0-dev.8",
25
+ "@toa.io/console": "0.4.0",
26
+ "@toa.io/generic": "0.4.0",
27
+ "@toa.io/kubernetes": "0.4.0",
28
+ "@toa.io/norm": "0.4.0",
29
+ "@toa.io/yaml": "0.4.0",
30
+ "dotenv": "16.0.3",
30
31
  "find-up": "5.0.0",
31
32
  "yargs": "17.6.2"
32
33
  },
package/readme.md CHANGED
@@ -78,6 +78,21 @@ the context will be found and replayed sequentially.
78
78
  </dd>
79
79
  </dl>
80
80
 
81
+ ### env
82
+
83
+ <dl>
84
+ <dt><code>toa env [environment]</code></dt>
85
+ <dd>Select environment. Set local environment variables to <code>.env</code> file.
86
+
87
+ <code>environment</code> deployment environment name (default <code>local</code>).<br/>
88
+ <code>--path</code> path to context (default <code>.</code>)<br/>
89
+ </dd>
90
+ </dl>
91
+
92
+ > It is recommended to add `.env` to `.gitignore`.
93
+
94
+ > Credentials specified in a `.env` file are preserved during environment selection.
95
+
81
96
  ## Deployment
82
97
 
83
98
  > Deployment commands use current `kubectl` context.
@@ -86,7 +101,10 @@ the context will be found and replayed sequentially.
86
101
 
87
102
  <dl>
88
103
  <dt><code>toa deploy [environment]</code></dt>
89
- <dd>Deploy context.</dd>
104
+ <dd>Deploy context.
105
+
106
+ <code>environment</code> deployment environment name (default <code>default</code>).<br/>
107
+ </dd>
90
108
  </dl>
91
109
 
92
110
  ### conceal
@@ -6,6 +6,7 @@ const builder = (yargs) => {
6
6
  yargs
7
7
  .positional('environment', {
8
8
  type: 'string',
9
+ default: 'default',
9
10
  desc: 'Deployment environment'
10
11
  })
11
12
  .option('path', {
@@ -0,0 +1,24 @@
1
+ 'use strict'
2
+
3
+ const { env } = require('../handlers/env')
4
+
5
+ const builder = (yargs) => {
6
+ yargs
7
+ .positional('environment', {
8
+ type: 'string',
9
+ default: 'local',
10
+ desc: 'Environment name'
11
+ })
12
+ .option('path', {
13
+ alias: 'p',
14
+ group: 'Command options:',
15
+ describe: 'Path to component',
16
+ type: 'string',
17
+ default: '.'
18
+ })
19
+ }
20
+
21
+ exports.command = 'env [environment]'
22
+ exports.desc = 'Select environment'
23
+ exports.builder = builder
24
+ exports.handler = env
@@ -0,0 +1,61 @@
1
+ 'use strict'
2
+
3
+ const { join } = require('node:path')
4
+ const dotenv = require('dotenv')
5
+ const { file } = require('@toa.io/filesystem')
6
+ const boot = require('@toa.io/boot')
7
+ const { context: find } = require('../util/find')
8
+
9
+ async function env (argv) {
10
+ const path = find(argv.path)
11
+ const filepath = join(path, '.env')
12
+ const operator = await boot.deployment(path, argv.environment)
13
+ const variables = operator.variables()
14
+ const currentValues = await read(filepath)
15
+ const nextValues = merge(variables.global, currentValues)
16
+
17
+ await write(filepath, nextValues)
18
+ }
19
+
20
+ /**
21
+ * @param path {string}
22
+ * @returns {Record<string, string>}
23
+ */
24
+ async function read (path) {
25
+ const exists = await file.is(path)
26
+
27
+ if (!exists) return {}
28
+
29
+ const contents = await file.read(path)
30
+
31
+ return dotenv.parse(contents)
32
+ }
33
+
34
+ /**
35
+ * @param {string} path
36
+ * @param {toa.deployment.dependency.Variable[]} values
37
+ * @return {Promise<void>}
38
+ */
39
+ async function write (path, values) {
40
+ const contents = values.reduce((lines, { name, value }) => lines + `${name}=${value}\n`, '')
41
+
42
+ await file.write(path, contents)
43
+ }
44
+
45
+ /**
46
+ * @param {toa.deployment.dependency.Variable[] } variables
47
+ * @param {Record<string, string>} current
48
+ * @return {toa.deployment.dependency.Variable[]}
49
+ */
50
+ function merge (variables, current) {
51
+ return variables.map((variable) => {
52
+ if (variable.secret === undefined) return variable
53
+
54
+ return {
55
+ name: variable.name,
56
+ value: current[variable.name] ?? ''
57
+ }
58
+ })
59
+ }
60
+
61
+ exports.env = env
package/src/program.js CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  'use strict'
4
4
 
5
+ if (!('TOA_ENV' in process.env)) require('dotenv').config()
6
+
5
7
  const yargs = require('yargs/yargs')
6
8
 
7
9
  const { console } = require('@toa.io/console')