Skip to content

Commit

Permalink
Add fix to support passing in JSON as string via command line argumen…
Browse files Browse the repository at this point in the history
…ts (#280)

When running a command like the one below, the secrets are parsed as if
they're an array/object. This should resolve that

```bash
nx deploy "some-function" ... "--secrets" "[ENV_FILE=some-env]"
```
  • Loading branch information
TriPSs authored Jun 5, 2024
2 parents 51acd5e + 1ba4ef8 commit 2345d73
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
2 changes: 1 addition & 1 deletion packages/gcp-cloud-run/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nx-extend/gcp-cloud-run",
"version": "10.1.1",
"version": "10.1.2",
"homepage": "https://github.com/TriPSs/nx-extend/blob/master/packages/gcp-cloud-run/README.md",
"bugs": {
"url": "https://github.com/tripss/nx-extend/issues"
Expand Down
34 changes: 25 additions & 9 deletions packages/gcp-cloud-run/src/utils/get-valid-secrets.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
import { logger } from '@nx/devkit'

export function getValidSecrets(secrets?: string[] | Record<string, string>): string[] {
export function getValidSecrets(
secrets?: string | string[] | Record<string, string>
): string[] {
if (!secrets) {
return []
}

if (typeof secrets === 'string') {
try {
secrets = JSON.parse(secrets)
} catch {
throw new Error('Invalid JSON passed to secrets argument')
}
}

if (!Array.isArray(secrets)) {
secrets = Object.keys(secrets).map((secret) => `${secret}=${secrets[secret]}`)
secrets = Object.keys(secrets).map(
(secret) => `${secret}=${secrets[secret]}`
)
}

return secrets.map((secret) => {
if (secret.includes('=') && secret.includes(':')) {
return secret
}
return secrets
.map((secret) => {
if (secret.includes('=') && secret.includes(':')) {
return secret
}

logger.warn(`"${secret}" is not a valid secret! It should be in the following format "ENV_VAR_NAME=SECRET:VERSION"`)
logger.warn(
`"${secret}" is not a valid secret! It should be in the following format "ENV_VAR_NAME=SECRET:VERSION"`
)

return false
}).filter(Boolean) as string[]
return false
})
.filter(Boolean) as string[]
}
2 changes: 1 addition & 1 deletion packages/gcp-functions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nx-extend/gcp-functions",
"version": "13.1.4",
"version": "13.1.5",
"homepage": "https://github.com/TriPSs/nx-extend/blob/master/packages/gcp-functions/README.md",
"bugs": {
"url": "https://github.com/tripss/nx-extend/issues"
Expand Down
12 changes: 10 additions & 2 deletions packages/gcp-functions/src/utils/get-valid-secrets.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { logger } from '@nx/devkit'

export function getValidSecrets(secrets?: string[] | Record<string, string>): string[] {
export function getValidSecrets(secrets?: string | string[] | Record<string, string>): string[] {
if (!secrets) {
return []
}

if (!Array.isArray(secrets)) {
if (typeof secrets === 'string') {
try {
secrets = JSON.parse(secrets)
} catch {
throw new Error('Invalid JSON passed to secrets argument')
}
}

if (!Array.isArray(secrets)) {
secrets = Object.keys(secrets).map((secret) => `${secret}=${secrets[secret]}`)
}

Expand Down

0 comments on commit 2345d73

Please sign in to comment.