dagger-env 0.6.7 → 1.0.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/README.md +16 -24
- package/dist/dagger-env.js.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/run-dagger-cmd.d.ts +17 -11
- package/dist/run-dagger-cmd.d.ts.map +1 -1
- package/dist/run-dagger-cmd.js +16 -21
- package/dist/run-dagger-cmd.js.map +1 -1
- package/package.json +5 -10
- package/src/dagger-env.spec.ts +203 -0
- package/src/dagger-env.ts +227 -0
- package/src/examples/run-dagger-cmd-usage.ts +76 -0
- package/src/index.ts +1 -0
- package/src/run-dagger-cmd.spec.ts +191 -0
- package/src/run-dagger-cmd.ts +134 -0
- package/dist/op.d.ts +0 -90
- package/dist/op.d.ts.map +0 -1
- package/dist/op.js +0 -34
- package/dist/op.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# dagger-env
|
|
2
2
|
|
|
3
|
-
A type-safe, reusable environment configuration abstraction for Dagger modules with full Zod v4 validation and
|
|
3
|
+
A type-safe, reusable environment configuration abstraction for Dagger modules with full Zod v4 validation and Infisical integration.
|
|
4
4
|
|
|
5
5
|
## Features
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ A type-safe, reusable environment configuration abstraction for Dagger modules w
|
|
|
9
9
|
- 🎯 **Consistent**: Standardized API across all Dagger modules
|
|
10
10
|
- 🛡️ **Validated**: Runtime validation of arguments, environment variables, and secrets
|
|
11
11
|
- 📦 **Modular**: Secret presets and derived environment variables
|
|
12
|
-
- 🔐 **
|
|
12
|
+
- 🔐 **Infisical Integration**: Built-in command runner backed by `infisical export`
|
|
13
13
|
- 🚀 **Easy to use**: Simple configuration-based setup
|
|
14
14
|
|
|
15
15
|
## Installation
|
|
@@ -18,7 +18,7 @@ A type-safe, reusable environment configuration abstraction for Dagger modules w
|
|
|
18
18
|
npm install dagger-env zod
|
|
19
19
|
```
|
|
20
20
|
|
|
21
|
-
**Note:** The command runner functionality (`dagger-env/run`) requires the
|
|
21
|
+
**Note:** The command runner functionality (`dagger-env/run`) requires the Infisical CLI (`infisical`) to be installed and authenticated.
|
|
22
22
|
|
|
23
23
|
## Quick Start
|
|
24
24
|
|
|
@@ -73,9 +73,9 @@ export class MyModule {
|
|
|
73
73
|
}
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
-
## Command Runner (
|
|
76
|
+
## Command Runner (Infisical Integration)
|
|
77
77
|
|
|
78
|
-
For projects using
|
|
78
|
+
For projects using Infisical for secret management, `dagger-env` provides a convenient command runner that fetches secrets with `infisical export` and passes them to `dagger call` via the `DAGGER_OPTIONS` environment variable:
|
|
79
79
|
|
|
80
80
|
```typescript
|
|
81
81
|
import { createDaggerEnv } from 'dagger-env'
|
|
@@ -102,14 +102,9 @@ const myDaggerEnv = createDaggerEnv({
|
|
|
102
102
|
|
|
103
103
|
// Create a command runner - simply pass your DaggerEnv instance
|
|
104
104
|
const runDaggerCommand = createDaggerCommandRunner({
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
{
|
|
109
|
-
id: 'your-section-id',
|
|
110
|
-
label: 'Shared'
|
|
111
|
-
}
|
|
112
|
-
],
|
|
105
|
+
projectId: 'your-project-id',
|
|
106
|
+
env: 'prod',
|
|
107
|
+
path: '/ci/my-repo',
|
|
113
108
|
dockerCommands: ['build', 'deploy', 'test'],
|
|
114
109
|
daggerEnv: myDaggerEnv
|
|
115
110
|
})
|
|
@@ -124,14 +119,11 @@ await runDaggerCommand('test', {
|
|
|
124
119
|
### Advanced Configuration
|
|
125
120
|
|
|
126
121
|
```typescript
|
|
127
|
-
// Advanced configuration with
|
|
122
|
+
// Advanced configuration with pre-command setup
|
|
128
123
|
const runDaggerCommand = createDaggerCommandRunner({
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
{ id: 'shared-section-id', label: 'Shared' },
|
|
133
|
-
{ id: 'project-section-id', label: 'Project Specific' }
|
|
134
|
-
],
|
|
124
|
+
projectId: 'your-project-id',
|
|
125
|
+
env: 'prod',
|
|
126
|
+
path: '/ci/my-repo',
|
|
135
127
|
dockerCommands: ['build', 'deploy', 'test'],
|
|
136
128
|
beforeCommand: async () => {
|
|
137
129
|
// Setup vendor files, modules, etc.
|
|
@@ -208,13 +200,13 @@ Returns array of secret names for a specific preset.
|
|
|
208
200
|
|
|
209
201
|
#### `createDaggerCommandRunner(config)`
|
|
210
202
|
|
|
211
|
-
Creates a function to run Dagger commands with
|
|
203
|
+
Creates a function to run Dagger commands with Infisical integration.
|
|
212
204
|
|
|
213
205
|
**Parameters:**
|
|
214
206
|
|
|
215
|
-
- `config.
|
|
216
|
-
- `config.
|
|
217
|
-
- `config.
|
|
207
|
+
- `config.projectId`: Infisical project ID
|
|
208
|
+
- `config.env`: Infisical environment slug (e.g. `prod`)
|
|
209
|
+
- `config.path`: Infisical folder path to fetch secrets from (e.g. `/ci/my-repo`)
|
|
218
210
|
- `config.dockerCommands`: Optional array of command names that should include Docker socket
|
|
219
211
|
- `config.beforeCommand`: Optional async function to run before executing the command
|
|
220
212
|
- `config.daggerEnv`: DaggerEnv instance for schema validation and type safety
|
package/dist/dagger-env.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dagger-env.js","sourceRoot":"","sources":["../src/dagger-env.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,mBAAmB,CAAA;AACvC,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAA;AA6B1B;;GAEG;AACH,MAAM,OAAO,SAAS;IAOQ;IANZ,aAAa,CAI5B;IAEF,YAA6B,MAAS;QAAT,WAAM,GAAN,MAAM,CAAG;QACrC,IAAI,CAAC,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC;YAC7B,IAAI,EAAE,MAAM,CAAC,IAAI;YACjB,GAAG,EAAE,MAAM,CAAC,GAAG;YACf,OAAO,EAAE,MAAM,C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C,cAAc,CAAC,IAAI,CAAC,CAAA;oBACvD,IAAI,cAAc,EAAE,CAAC;wBACpB,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,cAAc,CAAC,EAAE,CAAC;4BAC/C,CAAC,GAAG,CAAC,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAA;wBAC9B,CAAC;oBACF,CAAC;gBACF,CAAC;gBAED,4CAA4C;gBAC5C,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAA;gBACxB,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC;oBACxC,CAAC,GAAG,CAAC,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAA;gBAC9B,CAAC;gBAED,OAAO,CAAC,CAAA;YACT,CAAC;SACD,CAAA;IACF,CAAC;IAED;;OAEG;IACH,gBAAgB;QACf,OAAO,IAAI,CAAC,aAAa,CAAA;IAC1B,CAAC;IAED;;OAEG;IACH,gBAAgB;QACf,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,CAAA;IAC9C,CAAC;IAED;;OAEG;IACH,gBAAgB,CAAC,MAAiD;QACjE,MAAM,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;QACjD,IAAI,CAAC,OAAO,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,CAAC,0BAA0B,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAA;QAC5D,CAAC;QACD,OAAO,OAAO,CAAA;IACf,CAAC;CACD;AAED;;GAEG;AACH,MAAM,UAAU,eAAe,CAA4B,MAAS;IACnE,OAAO,IAAI,SAAS,CAAC,MAAM,CAAC,CAAA;AAC7B,CAAC","sourcesContent":["import { dag } from '@dagger.io/dagger'\nimport { z } from 'zod/v4'\n\nimport type { Container, Secret } from '@dagger.io/dagger'\n\n/**\n * Generic configuration for DaggerEnv\n */\nexport type DaggerEnvConfig = {\n\t/** Arguments schema */\n\targs: z.ZodObject<any>\n\t/** Environment variables schema */\n\tenv: z.ZodObject<any>\n\t/** Secrets schema */\n\tsecrets: z.ZodObject<any>\n\t/** Secret presets mapping preset names to arrays of secret names */\n\tsecretPresets: Record<string, readonly string[]>\n\t/** Derived environment variables based on secret names */\n\tderivedEnvVars: Record<string, Record<string, string>>\n}\n\n/**\n * Inferred options type from config\n */\nexport type DaggerOptionsFromConfig<T extends DaggerEnvConfig> = {\n\targs: z.output<T['args']>\n\tenv: z.output<T['env']>\n\tsecrets: z.output<T['secrets']>\n}\n\n/**\n * Reusable Dagger environment abstraction\n */\nexport class DaggerEnv<T extends DaggerEnvConfig> {\n\tprivate readonly optionsSchema: z.ZodObject<{\n\t\targs: T['args']\n\t\tenv: T['env']\n\t\tsecrets: T['secrets']\n\t}>\n\n\tconstructor(private readonly config: T) {\n\t\tthis.optionsSchema = z.object({\n\t\t\targs: config.args,\n\t\t\tenv: config.env,\n\t\t\tsecrets: config.secrets,\n\t\t})\n\t}\n\n\t/**\n\t * Parse dagger options from a Secret\n\t */\n\tasync parseDaggerOptions(options: Secret): Promise<DaggerOptionsFromConfig<T>> {\n\t\treturn this.optionsSchema.parse(\n\t\t\tJSON.parse(await options.plaintext())\n\t\t) as DaggerOptionsFromConfig<T>\n\t}\n\n\t/**\n\t * Create a function that applies environment variables and secrets to a container\n\t * based on the provided Dagger options, secret presets, and additional secret names.\n\t */\n\tasync getWithEnv(\n\t\tdaggerOptions: Secret | DaggerOptionsFromConfig<T>,\n\t\tsecretPresets: Array<Extract<keyof T['secretPresets'], string>>,\n\t\tsecretNames?: Array<Extract<keyof z.output<T['secrets']>, string>>\n\t): Promise<{\n\t\t/**\n\t\t * Apply environment variables and secrets to a container\n\t\t */\n\t\twithEnv: (con: Container) => Container\n\t\t/**\n\t\t * Remove environment variables and secrets from a container\n\t\t */\n\t\twithoutEnv: (con: Container) => Container\n\t}> {\n\t\tconst isSecret = (obj: any): obj is Secret =>\n\t\t\tobj &&\n\t\t\ttypeof obj === 'object' &&\n\t\t\t'id' in obj &&\n\t\t\t'plaintext' in obj &&\n\t\t\ttypeof obj.id === 'function' &&\n\t\t\ttypeof obj.plaintext === 'function'\n\n\t\tconst opts = isSecret(daggerOptions)\n\t\t\t? await this.parseDaggerOptions(daggerOptions)\n\t\t\t: daggerOptions\n\n\t\treturn {\n\t\t\twithEnv: (con: Container) => {\n\t\t\t\tlet c = con\n\n\t\t\t\t// Add individual secret names\n\t\t\t\tfor (const name of secretNames ?? []) {\n\t\t\t\t\tif (typeof name === 'string' && name in opts.secrets) {\n\t\t\t\t\t\tconst secret = (opts.secrets as Record<string, string>)[name]\n\t\t\t\t\t\tif (!secret) {\n\t\t\t\t\t\t\tthrow new Error(`Secret ${name} is undefined`)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tc = c.withSecretVariable(name, dag.setSecret(name, secret))\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst allSecretNames: string[] = [...(secretNames ?? [])]\n\n\t\t\t\t// Add secret presets\n\t\t\t\tfor (const preset of secretPresets) {\n\t\t\t\t\tconst presetSecrets = this.config.secretPresets[preset]\n\t\t\t\t\tif (!presetSecrets) {\n\t\t\t\t\t\tthrow new Error(`Unknown secret preset: ${String(preset)}`)\n\t\t\t\t\t}\n\n\t\t\t\t\tfor (const secretName of presetSecrets) {\n\t\t\t\t\t\tconst secret = (opts.secrets as Record<string, string>)[secretName]\n\t\t\t\t\t\tif (!secret) {\n\t\t\t\t\t\t\tthrow new Error(`Secret ${secretName} is undefined`)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tallSecretNames.push(secretName)\n\t\t\t\t\t\tc = c.withSecretVariable(secretName, dag.setSecret(secretName, secret))\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Add derived env vars based on secretNames\n\t\t\t\tfor (const name of allSecretNames) {\n\t\t\t\t\tconst derivedEnvVars = this.config.derivedEnvVars[name]\n\t\t\t\t\tif (derivedEnvVars) {\n\t\t\t\t\t\tfor (const [key, value] of Object.entries(derivedEnvVars)) {\n\t\t\t\t\t\t\tc = c.withEnvVariable(key, value)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Add environment variables from options\n\t\t\t\tconst envVars = opts.env\n\t\t\t\tfor (const [key, value] of Object.entries(envVars)) {\n\t\t\t\t\tif (value !== undefined && typeof value === 'string') {\n\t\t\t\t\t\tc = c.withEnvVariable(key, value)\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn c\n\t\t\t},\n\t\t\twithoutEnv: (con: Container) => {\n\t\t\t\tlet c = con\n\n\t\t\t\t// Add individual secret names\n\t\t\t\tfor (const name of secretNames ?? []) {\n\t\t\t\t\tif (typeof name === 'string' && name in opts.secrets) {\n\t\t\t\t\t\tconst secret = (opts.secrets as Record<string, string>)[name]\n\t\t\t\t\t\tif (!secret) {\n\t\t\t\t\t\t\tthrow new Error(`Secret ${name} is undefined`)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tc = c.withoutSecretVariable(name)\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tconst allSecretNames: string[] = [...(secretNames ?? [])]\n\n\t\t\t\t// remove secret presets\n\t\t\t\tfor (const preset of secretPresets) {\n\t\t\t\t\tconst presetSecrets = this.config.secretPresets[preset]\n\t\t\t\t\tif (!presetSecrets) {\n\t\t\t\t\t\tthrow new Error(`Unknown secret preset: ${String(preset)}`)\n\t\t\t\t\t}\n\n\t\t\t\t\tfor (const secretName of presetSecrets) {\n\t\t\t\t\t\tconst secret = (opts.secrets as Record<string, string>)[secretName]\n\t\t\t\t\t\tif (!secret) {\n\t\t\t\t\t\t\tthrow new Error(`Secret ${secretName} is undefined`)\n\t\t\t\t\t\t}\n\t\t\t\t\t\tallSecretNames.push(secretName)\n\t\t\t\t\t\tc = c.withoutSecretVariable(secretName)\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// remove derived env vars based on secretNames\n\t\t\t\tfor (const name of allSecretNames) {\n\t\t\t\t\tconst derivedEnvVars = this.config.derivedEnvVars[name]\n\t\t\t\t\tif (derivedEnvVars) {\n\t\t\t\t\t\tfor (const key of Object.keys(derivedEnvVars)) {\n\t\t\t\t\t\t\tc = c.withoutEnvVariable(key)\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// remove environment variables from options\n\t\t\t\tconst envVars = opts.env\n\t\t\t\tfor (const key of Object.keys(envVars)) {\n\t\t\t\t\tc = c.withoutEnvVariable(key)\n\t\t\t\t}\n\n\t\t\t\treturn c\n\t\t\t},\n\t\t}\n\t}\n\n\t/**\n\t * Get the options schema for this DaggerEnv instance\n\t */\n\tgetOptionsSchema() {\n\t\treturn this.optionsSchema\n\t}\n\n\t/**\n\t * Get available secret presets\n\t */\n\tgetSecretPresets(): Array<keyof T['secretPresets']> {\n\t\treturn Object.keys(this.config.secretPresets)\n\t}\n\n\t/**\n\t * Get secrets for a specific preset\n\t */\n\tgetPresetSecrets(preset: Extract<keyof T['secretPresets'], string>): readonly string[] {\n\t\tconst secrets = this.config.secretPresets[preset]\n\t\tif (!secrets) {\n\t\t\tthrow new Error(`Unknown secret preset: ${String(preset)}`)\n\t\t}\n\t\treturn secrets\n\t}\n}\n\n/**\n * Helper function to create a DaggerEnv instance with proper typing\n */\nexport function createDaggerEnv<T extends DaggerEnvConfig>(config: T): DaggerEnv<T> {\n\treturn new DaggerEnv(config)\n}\n"]}
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA","sourcesContent":["export * from './dagger-env.js'\n"]}
|
package/dist/run-dagger-cmd.d.ts
CHANGED
|
@@ -1,17 +1,23 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
1
2
|
import type { DaggerEnv, DaggerEnvConfig } from './dagger-env.js';
|
|
2
3
|
/**
|
|
3
|
-
*
|
|
4
|
+
* A single secret returned by `infisical export --format=json`
|
|
5
|
+
*/
|
|
6
|
+
export type InfisicalSecret = z.infer<typeof InfisicalSecret>;
|
|
7
|
+
export declare const InfisicalSecret: z.ZodObject<{
|
|
8
|
+
key: z.ZodString;
|
|
9
|
+
value: z.ZodString;
|
|
10
|
+
}, z.core.$strip>;
|
|
11
|
+
/**
|
|
12
|
+
* Configuration for running Dagger commands with Infisical integration
|
|
4
13
|
*/
|
|
5
14
|
export interface RunDaggerCommandConfig<T extends DaggerEnvConfig = DaggerEnvConfig> {
|
|
6
|
-
/**
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
|
|
12
|
-
label: string;
|
|
13
|
-
id: string;
|
|
14
|
-
}>;
|
|
15
|
+
/** Infisical project ID */
|
|
16
|
+
projectId: string;
|
|
17
|
+
/** Infisical environment slug (e.g. `prod`) */
|
|
18
|
+
env: string;
|
|
19
|
+
/** Infisical folder path to fetch secrets from (e.g. `/ci/my-repo`) */
|
|
20
|
+
path: string;
|
|
15
21
|
/** Commands that should include Docker socket if available */
|
|
16
22
|
dockerCommands?: string[];
|
|
17
23
|
/** Hook to run before executing the command (e.g., vendor file setup) */
|
|
@@ -31,7 +37,7 @@ export interface RunDaggerCommandOptions {
|
|
|
31
37
|
extraArgs?: string[];
|
|
32
38
|
}
|
|
33
39
|
/**
|
|
34
|
-
* Creates a function to run Dagger commands with
|
|
40
|
+
* Creates a function to run Dagger commands with Infisical integration
|
|
35
41
|
* @param config Configuration for the command runner
|
|
36
42
|
* @returns Function to execute Dagger commands
|
|
37
43
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run-dagger-cmd.d.ts","sourceRoot":"","sources":["../src/run-dagger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|
package/dist/run-dagger-cmd.js
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
1
2
|
import { $, fs } from 'zx';
|
|
2
|
-
|
|
3
|
+
export const InfisicalSecret = z.object({
|
|
4
|
+
key: z.string(),
|
|
5
|
+
value: z.string(),
|
|
6
|
+
});
|
|
3
7
|
/**
|
|
4
|
-
* Creates a function to run Dagger commands with
|
|
8
|
+
* Creates a function to run Dagger commands with Infisical integration
|
|
5
9
|
* @param config Configuration for the command runner
|
|
6
10
|
* @returns Function to execute Dagger commands
|
|
7
11
|
*/
|
|
8
12
|
export function createDaggerCommandRunner(config) {
|
|
9
|
-
const opItemUri = `op://${config.opVault}/${config.opItem}`;
|
|
10
13
|
return async function runDaggerCommand(commandName, options) {
|
|
11
14
|
const { args = {}, env = {}, extraArgs = [] } = options ?? {};
|
|
12
15
|
// Run any pre-command setup
|
|
13
16
|
if (config.beforeCommand) {
|
|
14
17
|
await config.beforeCommand();
|
|
15
18
|
}
|
|
16
|
-
// Environment variables to pass to the `
|
|
19
|
+
// Environment variables to pass to the `dagger` command
|
|
17
20
|
const envVars = {};
|
|
18
|
-
// Pass dagger cloud token in CI because we don't have user auth
|
|
19
|
-
if (process.env.CI !== undefined) {
|
|
20
|
-
envVars.DAGGER_CLOUD_TOKEN = `${opItemUri}/DAGGER_CLOUD_TOKEN`;
|
|
21
|
-
}
|
|
22
21
|
const commandArgs = [...extraArgs];
|
|
23
22
|
// Add Docker socket for specific commands if available
|
|
24
23
|
if (config.dockerCommands?.includes(commandName)) {
|
|
@@ -31,17 +30,17 @@ export function createDaggerCommandRunner(config) {
|
|
|
31
30
|
// Ignore if fs is not available or docker socket doesn't exist
|
|
32
31
|
}
|
|
33
32
|
}
|
|
34
|
-
// Fetch
|
|
35
|
-
const
|
|
36
|
-
//
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
const secrets = opItem.fields
|
|
40
|
-
.filter((f) => sectionsToInclude.some((s) => s.id === f.section?.id))
|
|
41
|
-
.reduce((acc, f) => {
|
|
42
|
-
acc[f.label] = f.value;
|
|
33
|
+
// Fetch secrets from Infisical
|
|
34
|
+
const exportedSecrets = InfisicalSecret.array().parse(await $ `infisical export --silent --format=json --projectId ${config.projectId} --env ${config.env} --path ${config.path}`.json());
|
|
35
|
+
// Extract secrets into a key/value map
|
|
36
|
+
const secrets = exportedSecrets.reduce((acc, s) => {
|
|
37
|
+
acc[s.key] = s.value;
|
|
43
38
|
return acc;
|
|
44
39
|
}, {});
|
|
40
|
+
// Pass dagger cloud token in CI because we don't have user auth
|
|
41
|
+
if (process.env.CI !== undefined && secrets.DAGGER_CLOUD_TOKEN !== undefined) {
|
|
42
|
+
envVars.DAGGER_CLOUD_TOKEN = secrets.DAGGER_CLOUD_TOKEN;
|
|
43
|
+
}
|
|
45
44
|
// Build environment variables for Dagger
|
|
46
45
|
const daggerEnv = { ...env };
|
|
47
46
|
if (process.env.CI !== undefined) {
|
|
@@ -59,10 +58,6 @@ export function createDaggerCommandRunner(config) {
|
|
|
59
58
|
envVars.DAGGER_OPTIONS = JSON.stringify(daggerOptions);
|
|
60
59
|
// Construct the command
|
|
61
60
|
const cmd = [
|
|
62
|
-
'op',
|
|
63
|
-
'run',
|
|
64
|
-
'--no-masking',
|
|
65
|
-
'--',
|
|
66
61
|
'dagger',
|
|
67
62
|
'call',
|
|
68
63
|
commandName,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"run-dagger-cmd.js","sourceRoot":"","sources":["../src/run-dagger-cmd.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"run-dagger-cmd.js","sourceRoot":"","sources":["../src/run-dagger-cmd.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAA;AAC1B,OAAO,EAAE,CAAC,EAAE,EAAE,EAAE,MAAM,IAAI,CAAA;AAQ1B,MAAM,CAAC,MAAM,e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nimport { $, fs } from 'zx'\n\nimport type { DaggerEnv, DaggerEnvConfig } from './dagger-env.js'\n\n/**\n * A single secret returned by `infisical export --format=json`\n */\nexport type InfisicalSecret = z.infer<typeof InfisicalSecret>\nexport const InfisicalSecret = z.object({\n\tkey: z.string(),\n\tvalue: z.string(),\n})\n\n/**\n * Configuration for running Dagger commands with Infisical integration\n */\nexport interface RunDaggerCommandConfig<T extends DaggerEnvConfig = DaggerEnvConfig> {\n\t/** Infisical project ID */\n\tprojectId: string\n\t/** Infisical environment slug (e.g. `prod`) */\n\tenv: string\n\t/** Infisical folder path to fetch secrets from (e.g. `/ci/my-repo`) */\n\tpath: string\n\t/** Commands that should include Docker socket if available */\n\tdockerCommands?: string[]\n\t/** Hook to run before executing the command (e.g., vendor file setup) */\n\tbeforeCommand?: () => Promise<void>\n\t/** DaggerEnv instance for schema validation and type safety */\n\tdaggerEnv: DaggerEnv<T>\n}\n\n/**\n * Options for individual command execution\n */\nexport interface RunDaggerCommandOptions {\n\t/** Arguments to pass to the Dagger command */\n\targs?: Record<string, any>\n\t/** Additional environment variables */\n\tenv?: Record<string, string>\n\t/** Additional command-line arguments */\n\textraArgs?: string[]\n}\n\n/**\n * Creates a function to run Dagger commands with Infisical integration\n * @param config Configuration for the command runner\n * @returns Function to execute Dagger commands\n */\nexport function createDaggerCommandRunner<T extends DaggerEnvConfig>(\n\tconfig: RunDaggerCommandConfig<T>\n) {\n\treturn async function runDaggerCommand(\n\t\tcommandName: string,\n\t\toptions?: RunDaggerCommandOptions\n\t): Promise<void> {\n\t\tconst { args = {}, env = {}, extraArgs = [] } = options ?? {}\n\n\t\t// Run any pre-command setup\n\t\tif (config.beforeCommand) {\n\t\t\tawait config.beforeCommand()\n\t\t}\n\n\t\t// Environment variables to pass to the `dagger` command\n\t\tconst envVars: Record<string, string> = {}\n\n\t\tconst commandArgs: string[] = [...extraArgs]\n\n\t\t// Add Docker socket for specific commands if available\n\t\tif (config.dockerCommands?.includes(commandName)) {\n\t\t\ttry {\n\t\t\t\tif (await fs.exists('/var/run/docker.sock')) {\n\t\t\t\t\tcommandArgs.push('--docker-socket=/var/run/docker.sock')\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// Ignore if fs is not available or docker socket doesn't exist\n\t\t\t}\n\t\t}\n\n\t\t// Fetch secrets from Infisical\n\t\tconst exportedSecrets = InfisicalSecret.array().parse(\n\t\t\tawait $`infisical export --silent --format=json --projectId ${config.projectId} --env ${config.env} --path ${config.path}`.json()\n\t\t)\n\n\t\t// Extract secrets into a key/value map\n\t\tconst secrets = exportedSecrets.reduce(\n\t\t\t(acc, s) => {\n\t\t\t\tacc[s.key] = s.value\n\t\t\t\treturn acc\n\t\t\t},\n\t\t\t{} as Record<string, string>\n\t\t)\n\n\t\t// Pass dagger cloud token in CI because we don't have user auth\n\t\tif (process.env.CI !== undefined && secrets.DAGGER_CLOUD_TOKEN !== undefined) {\n\t\t\tenvVars.DAGGER_CLOUD_TOKEN = secrets.DAGGER_CLOUD_TOKEN\n\t\t}\n\n\t\t// Build environment variables for Dagger\n\t\tconst daggerEnv: Record<string, string> = { ...env }\n\t\tif (process.env.CI !== undefined) {\n\t\t\tdaggerEnv.CI = process.env.CI\n\t\t}\n\t\tif (process.env.GITHUB_ACTIONS !== undefined) {\n\t\t\tdaggerEnv.GITHUB_ACTIONS = process.env.GITHUB_ACTIONS\n\t\t}\n\n\t\t// Validate and serialize dagger options\n\t\tconst daggerOptions = config.daggerEnv.getOptionsSchema().parse({\n\t\t\targs,\n\t\t\tenv: daggerEnv,\n\t\t\tsecrets,\n\t\t})\n\t\tenvVars.DAGGER_OPTIONS = JSON.stringify(daggerOptions)\n\n\t\t// Construct the command\n\t\tconst cmd: string[] = [\n\t\t\t'dagger',\n\t\t\t'call',\n\t\t\tcommandName,\n\t\t\t...commandArgs,\n\t\t\t'--options=env://DAGGER_OPTIONS',\n\t\t]\n\n\t\t// Execute the command\n\t\tawait $({\n\t\t\tenv: {\n\t\t\t\t...process.env,\n\t\t\t\t...envVars,\n\t\t\t},\n\t\t\tstdio: 'inherit',\n\t\t})`${cmd}`\n\t}\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dagger-env",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "A type-safe, reusable environment configuration abstraction for Dagger modules.",
|
|
6
6
|
"keywords": [
|
|
@@ -28,12 +28,6 @@
|
|
|
28
28
|
"default": "./dist/index.js"
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
|
-
"./op": {
|
|
32
|
-
"import": {
|
|
33
|
-
"types": "./dist/op.d.ts",
|
|
34
|
-
"default": "./dist/op.js"
|
|
35
|
-
}
|
|
36
|
-
},
|
|
37
31
|
"./run": {
|
|
38
32
|
"import": {
|
|
39
33
|
"types": "./dist/run-dagger-cmd.d.ts",
|
|
@@ -44,13 +38,14 @@
|
|
|
44
38
|
"main": "./dist/index.js",
|
|
45
39
|
"module": "./dist/index.js",
|
|
46
40
|
"files": [
|
|
47
|
-
"dist"
|
|
41
|
+
"dist",
|
|
42
|
+
"src"
|
|
48
43
|
],
|
|
49
44
|
"dependencies": {
|
|
50
45
|
"zx": "8.8.4"
|
|
51
46
|
},
|
|
52
47
|
"devDependencies": {
|
|
53
|
-
"@dagger.io/dagger": "0.20.
|
|
48
|
+
"@dagger.io/dagger": "0.20.6",
|
|
54
49
|
"typescript": "5.8.2",
|
|
55
50
|
"vitest": "3.2.4",
|
|
56
51
|
"zod": "4.1.12",
|
|
@@ -64,7 +59,7 @@
|
|
|
64
59
|
"access": "public"
|
|
65
60
|
},
|
|
66
61
|
"scripts": {
|
|
67
|
-
"build": "runx build tsc ./src/index.ts ./src/
|
|
62
|
+
"build": "runx build tsc ./src/index.ts ./src/run-dagger-cmd.ts",
|
|
68
63
|
"check:exports": "runx check --exports",
|
|
69
64
|
"check:lint": "run-eslint",
|
|
70
65
|
"check:types": "run-tsc",
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { z } from 'zod/v4'
|
|
3
|
+
|
|
4
|
+
import { createDaggerEnv } from './dagger-env'
|
|
5
|
+
|
|
6
|
+
import type { DaggerOptionsFromConfig } from './dagger-env'
|
|
7
|
+
|
|
8
|
+
describe('DaggerEnv', () => {
|
|
9
|
+
const daggerEnv = createDaggerEnv({
|
|
10
|
+
args: z.object({
|
|
11
|
+
push: z.string().optional(),
|
|
12
|
+
environment: z.enum(['dev', 'prod']).optional(),
|
|
13
|
+
}),
|
|
14
|
+
env: z.object({
|
|
15
|
+
CI: z.string().optional(),
|
|
16
|
+
NODE_ENV: z.string().optional(),
|
|
17
|
+
}),
|
|
18
|
+
secrets: z.object({
|
|
19
|
+
API_TOKEN: z.string(),
|
|
20
|
+
DATABASE_URL: z.string(),
|
|
21
|
+
REDIS_URL: z.string(),
|
|
22
|
+
}),
|
|
23
|
+
secretPresets: {
|
|
24
|
+
api: ['API_TOKEN', 'DATABASE_URL'],
|
|
25
|
+
cache: ['REDIS_URL'],
|
|
26
|
+
},
|
|
27
|
+
derivedEnvVars: {
|
|
28
|
+
API_TOKEN: {
|
|
29
|
+
API_BASE_URL: 'https://api.example.com',
|
|
30
|
+
API_VERSION: 'v1',
|
|
31
|
+
},
|
|
32
|
+
DATABASE_URL: {
|
|
33
|
+
DB_POOL_SIZE: '10',
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('should create a DaggerEnv instance', () => {
|
|
39
|
+
expect(daggerEnv).toBeDefined()
|
|
40
|
+
expect(typeof daggerEnv.parseDaggerOptions).toBe('function')
|
|
41
|
+
expect(typeof daggerEnv.getWithEnv).toBe('function')
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('should return the options schema', () => {
|
|
45
|
+
const schema = daggerEnv.getOptionsSchema()
|
|
46
|
+
expect(schema).toBeDefined()
|
|
47
|
+
expect(schema.shape.args).toBeDefined()
|
|
48
|
+
expect(schema.shape.env).toBeDefined()
|
|
49
|
+
expect(schema.shape.secrets).toBeDefined()
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('should return available secret presets', () => {
|
|
53
|
+
const presets = daggerEnv.getSecretPresets()
|
|
54
|
+
expect(presets).toEqual(['api', 'cache'])
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('should return secrets for a specific preset', () => {
|
|
58
|
+
const apiSecrets = daggerEnv.getPresetSecrets('api')
|
|
59
|
+
expect(apiSecrets).toEqual(['API_TOKEN', 'DATABASE_URL'])
|
|
60
|
+
|
|
61
|
+
const cacheSecrets = daggerEnv.getPresetSecrets('cache')
|
|
62
|
+
expect(cacheSecrets).toEqual(['REDIS_URL'])
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('should throw error for unknown preset', () => {
|
|
66
|
+
expect(() => {
|
|
67
|
+
daggerEnv.getPresetSecrets('unknown' as any)
|
|
68
|
+
}).toThrow('Unknown secret preset: unknown')
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('should validate options schema', () => {
|
|
72
|
+
const schema = daggerEnv.getOptionsSchema()
|
|
73
|
+
|
|
74
|
+
const validOptions = {
|
|
75
|
+
args: {
|
|
76
|
+
push: 'true',
|
|
77
|
+
environment: 'dev',
|
|
78
|
+
},
|
|
79
|
+
env: {
|
|
80
|
+
CI: 'true',
|
|
81
|
+
NODE_ENV: 'development',
|
|
82
|
+
},
|
|
83
|
+
secrets: {
|
|
84
|
+
API_TOKEN: 'test-token',
|
|
85
|
+
DATABASE_URL: 'postgres://localhost/test',
|
|
86
|
+
REDIS_URL: 'redis://localhost:6379',
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const result = schema.parse(validOptions)
|
|
91
|
+
expect(result).toEqual(validOptions)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('should reject invalid options', () => {
|
|
95
|
+
const schema = daggerEnv.getOptionsSchema()
|
|
96
|
+
|
|
97
|
+
const invalidOptions = {
|
|
98
|
+
args: {
|
|
99
|
+
environment: 'invalid', // not in enum
|
|
100
|
+
},
|
|
101
|
+
env: {
|
|
102
|
+
CI: 'true',
|
|
103
|
+
},
|
|
104
|
+
secrets: {
|
|
105
|
+
// missing required secrets
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
expect(() => schema.parse(invalidOptions)).toThrow()
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
describe('getWithEnv()', () => {
|
|
113
|
+
describe('type inference', () => {
|
|
114
|
+
describe('secretPresets', () => {
|
|
115
|
+
it('should not have type error for valid preset', async () => {
|
|
116
|
+
await daggerEnv.getWithEnv({} as any, ['api'], ['API_TOKEN'])
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
it('should have type error for unknown preset', async () => {
|
|
120
|
+
await daggerEnv.getWithEnv(
|
|
121
|
+
{} as any,
|
|
122
|
+
[
|
|
123
|
+
// @ts-expect-error
|
|
124
|
+
'unknown',
|
|
125
|
+
],
|
|
126
|
+
['API_TOKEN']
|
|
127
|
+
)
|
|
128
|
+
})
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
describe('secretNames', () => {
|
|
132
|
+
it('should not have type error for valid secret name', async () => {
|
|
133
|
+
await daggerEnv.getWithEnv({} as any, ['api'], ['API_TOKEN'])
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
it('should have type error for unknown secret name', async () => {
|
|
137
|
+
await daggerEnv.getWithEnv(
|
|
138
|
+
{} as any,
|
|
139
|
+
['api'],
|
|
140
|
+
[
|
|
141
|
+
// @ts-expect-error
|
|
142
|
+
'INVALID_SECRET',
|
|
143
|
+
]
|
|
144
|
+
)
|
|
145
|
+
})
|
|
146
|
+
})
|
|
147
|
+
})
|
|
148
|
+
})
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
describe('DaggerEnv integration', () => {
|
|
152
|
+
it('should work with a complex configuration', () => {
|
|
153
|
+
const complexConfig = {
|
|
154
|
+
args: z.object({
|
|
155
|
+
push: z.string().optional(),
|
|
156
|
+
filter: z.string().optional(),
|
|
157
|
+
}),
|
|
158
|
+
env: z.object({
|
|
159
|
+
CI: z.string().optional(),
|
|
160
|
+
GITHUB_ACTIONS: z.string().optional(),
|
|
161
|
+
}),
|
|
162
|
+
secrets: z.object({
|
|
163
|
+
BUILD_TOKEN: z.string(),
|
|
164
|
+
CACHE_KEY: z.string(),
|
|
165
|
+
API_TOKEN: z.string(),
|
|
166
|
+
DEPLOY_TOKEN: z.string(),
|
|
167
|
+
}),
|
|
168
|
+
secretPresets: {
|
|
169
|
+
build: ['BUILD_TOKEN', 'CACHE_KEY'],
|
|
170
|
+
deploy: ['API_TOKEN', 'DEPLOY_TOKEN'],
|
|
171
|
+
},
|
|
172
|
+
derivedEnvVars: {
|
|
173
|
+
BUILD_TOKEN: {
|
|
174
|
+
BUILD_API: 'https://build.example.com',
|
|
175
|
+
BUILD_TEAM: 'team_example',
|
|
176
|
+
},
|
|
177
|
+
API_TOKEN: {
|
|
178
|
+
API_ACCOUNT_ID: 'test-account-id',
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
} as const
|
|
182
|
+
|
|
183
|
+
const daggerEnv = createDaggerEnv(complexConfig)
|
|
184
|
+
|
|
185
|
+
expect(daggerEnv.getSecretPresets()).toEqual(['build', 'deploy'])
|
|
186
|
+
expect(daggerEnv.getPresetSecrets('build')).toEqual(['BUILD_TOKEN', 'CACHE_KEY'])
|
|
187
|
+
|
|
188
|
+
const schema = daggerEnv.getOptionsSchema()
|
|
189
|
+
const validOptions = {
|
|
190
|
+
args: { push: 'true', filter: 'api' },
|
|
191
|
+
env: { CI: 'true', GITHUB_ACTIONS: 'true' },
|
|
192
|
+
secrets: {
|
|
193
|
+
BUILD_TOKEN: 'test-build-token',
|
|
194
|
+
CACHE_KEY: 'test-cache-key',
|
|
195
|
+
API_TOKEN: 'test-api-token',
|
|
196
|
+
DEPLOY_TOKEN: 'test-deploy-token',
|
|
197
|
+
},
|
|
198
|
+
} satisfies DaggerOptionsFromConfig<typeof complexConfig>
|
|
199
|
+
|
|
200
|
+
const result = schema.parse(validOptions)
|
|
201
|
+
expect(result).toEqual(validOptions)
|
|
202
|
+
})
|
|
203
|
+
})
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { dag } from '@dagger.io/dagger'
|
|
2
|
+
import { z } from 'zod/v4'
|
|
3
|
+
|
|
4
|
+
import type { Container, Secret } from '@dagger.io/dagger'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Generic configuration for DaggerEnv
|
|
8
|
+
*/
|
|
9
|
+
export type DaggerEnvConfig = {
|
|
10
|
+
/** Arguments schema */
|
|
11
|
+
args: z.ZodObject<any>
|
|
12
|
+
/** Environment variables schema */
|
|
13
|
+
env: z.ZodObject<any>
|
|
14
|
+
/** Secrets schema */
|
|
15
|
+
secrets: z.ZodObject<any>
|
|
16
|
+
/** Secret presets mapping preset names to arrays of secret names */
|
|
17
|
+
secretPresets: Record<string, readonly string[]>
|
|
18
|
+
/** Derived environment variables based on secret names */
|
|
19
|
+
derivedEnvVars: Record<string, Record<string, string>>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Inferred options type from config
|
|
24
|
+
*/
|
|
25
|
+
export type DaggerOptionsFromConfig<T extends DaggerEnvConfig> = {
|
|
26
|
+
args: z.output<T['args']>
|
|
27
|
+
env: z.output<T['env']>
|
|
28
|
+
secrets: z.output<T['secrets']>
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Reusable Dagger environment abstraction
|
|
33
|
+
*/
|
|
34
|
+
export class DaggerEnv<T extends DaggerEnvConfig> {
|
|
35
|
+
private readonly optionsSchema: z.ZodObject<{
|
|
36
|
+
args: T['args']
|
|
37
|
+
env: T['env']
|
|
38
|
+
secrets: T['secrets']
|
|
39
|
+
}>
|
|
40
|
+
|
|
41
|
+
constructor(private readonly config: T) {
|
|
42
|
+
this.optionsSchema = z.object({
|
|
43
|
+
args: config.args,
|
|
44
|
+
env: config.env,
|
|
45
|
+
secrets: config.secrets,
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Parse dagger options from a Secret
|
|
51
|
+
*/
|
|
52
|
+
async parseDaggerOptions(options: Secret): Promise<DaggerOptionsFromConfig<T>> {
|
|
53
|
+
return this.optionsSchema.parse(
|
|
54
|
+
JSON.parse(await options.plaintext())
|
|
55
|
+
) as DaggerOptionsFromConfig<T>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Create a function that applies environment variables and secrets to a container
|
|
60
|
+
* based on the provided Dagger options, secret presets, and additional secret names.
|
|
61
|
+
*/
|
|
62
|
+
async getWithEnv(
|
|
63
|
+
daggerOptions: Secret | DaggerOptionsFromConfig<T>,
|
|
64
|
+
secretPresets: Array<Extract<keyof T['secretPresets'], string>>,
|
|
65
|
+
secretNames?: Array<Extract<keyof z.output<T['secrets']>, string>>
|
|
66
|
+
): Promise<{
|
|
67
|
+
/**
|
|
68
|
+
* Apply environment variables and secrets to a container
|
|
69
|
+
*/
|
|
70
|
+
withEnv: (con: Container) => Container
|
|
71
|
+
/**
|
|
72
|
+
* Remove environment variables and secrets from a container
|
|
73
|
+
*/
|
|
74
|
+
withoutEnv: (con: Container) => Container
|
|
75
|
+
}> {
|
|
76
|
+
const isSecret = (obj: any): obj is Secret =>
|
|
77
|
+
obj &&
|
|
78
|
+
typeof obj === 'object' &&
|
|
79
|
+
'id' in obj &&
|
|
80
|
+
'plaintext' in obj &&
|
|
81
|
+
typeof obj.id === 'function' &&
|
|
82
|
+
typeof obj.plaintext === 'function'
|
|
83
|
+
|
|
84
|
+
const opts = isSecret(daggerOptions)
|
|
85
|
+
? await this.parseDaggerOptions(daggerOptions)
|
|
86
|
+
: daggerOptions
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
withEnv: (con: Container) => {
|
|
90
|
+
let c = con
|
|
91
|
+
|
|
92
|
+
// Add individual secret names
|
|
93
|
+
for (const name of secretNames ?? []) {
|
|
94
|
+
if (typeof name === 'string' && name in opts.secrets) {
|
|
95
|
+
const secret = (opts.secrets as Record<string, string>)[name]
|
|
96
|
+
if (!secret) {
|
|
97
|
+
throw new Error(`Secret ${name} is undefined`)
|
|
98
|
+
}
|
|
99
|
+
c = c.withSecretVariable(name, dag.setSecret(name, secret))
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const allSecretNames: string[] = [...(secretNames ?? [])]
|
|
104
|
+
|
|
105
|
+
// Add secret presets
|
|
106
|
+
for (const preset of secretPresets) {
|
|
107
|
+
const presetSecrets = this.config.secretPresets[preset]
|
|
108
|
+
if (!presetSecrets) {
|
|
109
|
+
throw new Error(`Unknown secret preset: ${String(preset)}`)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
for (const secretName of presetSecrets) {
|
|
113
|
+
const secret = (opts.secrets as Record<string, string>)[secretName]
|
|
114
|
+
if (!secret) {
|
|
115
|
+
throw new Error(`Secret ${secretName} is undefined`)
|
|
116
|
+
}
|
|
117
|
+
allSecretNames.push(secretName)
|
|
118
|
+
c = c.withSecretVariable(secretName, dag.setSecret(secretName, secret))
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Add derived env vars based on secretNames
|
|
123
|
+
for (const name of allSecretNames) {
|
|
124
|
+
const derivedEnvVars = this.config.derivedEnvVars[name]
|
|
125
|
+
if (derivedEnvVars) {
|
|
126
|
+
for (const [key, value] of Object.entries(derivedEnvVars)) {
|
|
127
|
+
c = c.withEnvVariable(key, value)
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Add environment variables from options
|
|
133
|
+
const envVars = opts.env
|
|
134
|
+
for (const [key, value] of Object.entries(envVars)) {
|
|
135
|
+
if (value !== undefined && typeof value === 'string') {
|
|
136
|
+
c = c.withEnvVariable(key, value)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return c
|
|
141
|
+
},
|
|
142
|
+
withoutEnv: (con: Container) => {
|
|
143
|
+
let c = con
|
|
144
|
+
|
|
145
|
+
// Add individual secret names
|
|
146
|
+
for (const name of secretNames ?? []) {
|
|
147
|
+
if (typeof name === 'string' && name in opts.secrets) {
|
|
148
|
+
const secret = (opts.secrets as Record<string, string>)[name]
|
|
149
|
+
if (!secret) {
|
|
150
|
+
throw new Error(`Secret ${name} is undefined`)
|
|
151
|
+
}
|
|
152
|
+
c = c.withoutSecretVariable(name)
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const allSecretNames: string[] = [...(secretNames ?? [])]
|
|
157
|
+
|
|
158
|
+
// remove secret presets
|
|
159
|
+
for (const preset of secretPresets) {
|
|
160
|
+
const presetSecrets = this.config.secretPresets[preset]
|
|
161
|
+
if (!presetSecrets) {
|
|
162
|
+
throw new Error(`Unknown secret preset: ${String(preset)}`)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
for (const secretName of presetSecrets) {
|
|
166
|
+
const secret = (opts.secrets as Record<string, string>)[secretName]
|
|
167
|
+
if (!secret) {
|
|
168
|
+
throw new Error(`Secret ${secretName} is undefined`)
|
|
169
|
+
}
|
|
170
|
+
allSecretNames.push(secretName)
|
|
171
|
+
c = c.withoutSecretVariable(secretName)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// remove derived env vars based on secretNames
|
|
176
|
+
for (const name of allSecretNames) {
|
|
177
|
+
const derivedEnvVars = this.config.derivedEnvVars[name]
|
|
178
|
+
if (derivedEnvVars) {
|
|
179
|
+
for (const key of Object.keys(derivedEnvVars)) {
|
|
180
|
+
c = c.withoutEnvVariable(key)
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// remove environment variables from options
|
|
186
|
+
const envVars = opts.env
|
|
187
|
+
for (const key of Object.keys(envVars)) {
|
|
188
|
+
c = c.withoutEnvVariable(key)
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
return c
|
|
192
|
+
},
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Get the options schema for this DaggerEnv instance
|
|
198
|
+
*/
|
|
199
|
+
getOptionsSchema() {
|
|
200
|
+
return this.optionsSchema
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Get available secret presets
|
|
205
|
+
*/
|
|
206
|
+
getSecretPresets(): Array<keyof T['secretPresets']> {
|
|
207
|
+
return Object.keys(this.config.secretPresets)
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Get secrets for a specific preset
|
|
212
|
+
*/
|
|
213
|
+
getPresetSecrets(preset: Extract<keyof T['secretPresets'], string>): readonly string[] {
|
|
214
|
+
const secrets = this.config.secretPresets[preset]
|
|
215
|
+
if (!secrets) {
|
|
216
|
+
throw new Error(`Unknown secret preset: ${String(preset)}`)
|
|
217
|
+
}
|
|
218
|
+
return secrets
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Helper function to create a DaggerEnv instance with proper typing
|
|
224
|
+
*/
|
|
225
|
+
export function createDaggerEnv<T extends DaggerEnvConfig>(config: T): DaggerEnv<T> {
|
|
226
|
+
return new DaggerEnv(config)
|
|
227
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example usage of the runDaggerCommand abstraction
|
|
3
|
+
*
|
|
4
|
+
* This example shows a realistic configuration for a project that uses Dagger
|
|
5
|
+
* with Infisical integration for secret management.
|
|
6
|
+
*
|
|
7
|
+
* Import pattern:
|
|
8
|
+
* - Main DaggerEnv functionality: import { createDaggerEnv } from 'dagger-env'
|
|
9
|
+
* - Command runner functionality: import { createDaggerCommandRunner } from 'dagger-env/run'
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { createDaggerEnv } from 'dagger-env'
|
|
13
|
+
import { createDaggerCommandRunner } from 'dagger-env/run'
|
|
14
|
+
import { z } from 'zod/v4'
|
|
15
|
+
|
|
16
|
+
// Create a DaggerEnv instance with your project's specific configuration
|
|
17
|
+
const daggerEnv = createDaggerEnv({
|
|
18
|
+
args: z.object({
|
|
19
|
+
environment: z.enum(['dev', 'staging', 'prod']).optional(),
|
|
20
|
+
push: z.boolean().optional(),
|
|
21
|
+
verbose: z.boolean().optional(),
|
|
22
|
+
}),
|
|
23
|
+
env: z.object({
|
|
24
|
+
// CI environment variables
|
|
25
|
+
CI: z.string().optional(),
|
|
26
|
+
GITHUB_ACTIONS: z.string().optional(),
|
|
27
|
+
GITHUB_SHA: z.string().optional(),
|
|
28
|
+
// Project-specific environment variables
|
|
29
|
+
NODE_ENV: z.string().optional(),
|
|
30
|
+
LOG_LEVEL: z.string().optional(),
|
|
31
|
+
}),
|
|
32
|
+
secrets: z.object({
|
|
33
|
+
// API credentials
|
|
34
|
+
API_TOKEN: z.string(),
|
|
35
|
+
API_SECRET: z.string(),
|
|
36
|
+
// Database connection
|
|
37
|
+
DATABASE_URL: z.string(),
|
|
38
|
+
// External service keys
|
|
39
|
+
AWS_ACCESS_KEY_ID: z.string(),
|
|
40
|
+
AWS_SECRET_ACCESS_KEY: z.string(),
|
|
41
|
+
SENTRY_DSN: z.string().optional(),
|
|
42
|
+
}),
|
|
43
|
+
secretPresets: {
|
|
44
|
+
api: ['API_TOKEN', 'API_SECRET'],
|
|
45
|
+
database: ['DATABASE_URL'],
|
|
46
|
+
aws: ['AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY'],
|
|
47
|
+
all: [
|
|
48
|
+
'API_TOKEN',
|
|
49
|
+
'API_SECRET',
|
|
50
|
+
'DATABASE_URL',
|
|
51
|
+
'AWS_ACCESS_KEY_ID',
|
|
52
|
+
'AWS_SECRET_ACCESS_KEY',
|
|
53
|
+
'SENTRY_DSN',
|
|
54
|
+
],
|
|
55
|
+
} as const,
|
|
56
|
+
derivedEnvVars: {} as const,
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
// Create the command runner with Infisical configuration
|
|
60
|
+
export const runDaggerCommand = createDaggerCommandRunner({
|
|
61
|
+
// Infisical configuration
|
|
62
|
+
projectId: 'your-project-id', // Replace with your actual Infisical project ID
|
|
63
|
+
env: 'prod', // Infisical environment slug
|
|
64
|
+
path: '/ci/my-repo', // Infisical folder path to fetch secrets from
|
|
65
|
+
// Commands that need Docker socket access
|
|
66
|
+
dockerCommands: ['build', 'test', 'deploy', 'migrate', 'seed'],
|
|
67
|
+
// Optional: Run setup before executing commands
|
|
68
|
+
beforeCommand: async () => {
|
|
69
|
+
// Example: Set up vendor files, download dependencies, etc.
|
|
70
|
+
console.log('Preparing Dagger environment...')
|
|
71
|
+
// const modules = await getDaggerModules()
|
|
72
|
+
// await setupDaggerVendorFiles(modules)
|
|
73
|
+
},
|
|
74
|
+
// Pass the DaggerEnv instance directly
|
|
75
|
+
daggerEnv,
|
|
76
|
+
})
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dagger-env.js'
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { z } from 'zod/v4'
|
|
3
|
+
|
|
4
|
+
import { createDaggerEnv } from './dagger-env'
|
|
5
|
+
import { createDaggerCommandRunner } from './run-dagger-cmd'
|
|
6
|
+
|
|
7
|
+
import type { RunDaggerCommandConfig } from './run-dagger-cmd'
|
|
8
|
+
|
|
9
|
+
const mocks = vi.hoisted(() => ({
|
|
10
|
+
/** Resolves the JSON payload returned by `infisical export` */
|
|
11
|
+
exportJson: vi.fn(),
|
|
12
|
+
/** Records the rendered `infisical export` command string */
|
|
13
|
+
exportCmd: vi.fn(),
|
|
14
|
+
/** Records the final command execution: (options, argv) */
|
|
15
|
+
spawn: vi.fn(
|
|
16
|
+
async (_opts: { env: Record<string, string | undefined> }, _argv: string[]) => undefined
|
|
17
|
+
),
|
|
18
|
+
/** Mock for fs.exists() (docker socket probe) */
|
|
19
|
+
exists: vi.fn(async () => false),
|
|
20
|
+
}))
|
|
21
|
+
|
|
22
|
+
vi.mock('zx', () => {
|
|
23
|
+
const $ = (firstArg: unknown, ...vals: unknown[]) => {
|
|
24
|
+
if (Array.isArray(firstArg)) {
|
|
25
|
+
// Direct template call: $`infisical export ...`
|
|
26
|
+
const pieces = firstArg as readonly string[]
|
|
27
|
+
const cmd = pieces.reduce(
|
|
28
|
+
(acc, piece, i) => acc + piece + (i < vals.length ? String(vals[i]) : ''),
|
|
29
|
+
''
|
|
30
|
+
)
|
|
31
|
+
mocks.exportCmd(cmd)
|
|
32
|
+
return { json: mocks.exportJson }
|
|
33
|
+
}
|
|
34
|
+
// Options call: $({ env, stdio }) returns a template executor
|
|
35
|
+
return (_pieces: TemplateStringsArray, ...templateVals: unknown[]) =>
|
|
36
|
+
mocks.spawn(
|
|
37
|
+
firstArg as { env: Record<string, string | undefined> },
|
|
38
|
+
templateVals[0] as string[]
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
return { $, fs: { exists: mocks.exists } }
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Environment variables passed to the spawned process
|
|
46
|
+
*/
|
|
47
|
+
type SpawnEnv = Record<string, string | undefined>
|
|
48
|
+
|
|
49
|
+
function getSpawnCall(): { env: SpawnEnv; argv: string[] } {
|
|
50
|
+
expect(mocks.spawn).toHaveBeenCalledTimes(1)
|
|
51
|
+
const call = mocks.spawn.mock.calls.at(0)
|
|
52
|
+
if (!call) {
|
|
53
|
+
throw new Error('spawn was not called')
|
|
54
|
+
}
|
|
55
|
+
const [opts, argv] = call
|
|
56
|
+
return { env: opts.env, argv }
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function createRunner(overrides?: Partial<Pick<RunDaggerCommandConfig, 'dockerCommands'>>) {
|
|
60
|
+
const daggerEnv = createDaggerEnv({
|
|
61
|
+
args: z.object({
|
|
62
|
+
push: z.string().optional(),
|
|
63
|
+
}),
|
|
64
|
+
env: z.object({
|
|
65
|
+
CI: z.string().optional(),
|
|
66
|
+
GITHUB_ACTIONS: z.string().optional(),
|
|
67
|
+
NODE_ENV: z.string().optional(),
|
|
68
|
+
}),
|
|
69
|
+
secrets: z.object({
|
|
70
|
+
API_TOKEN: z.string(),
|
|
71
|
+
}),
|
|
72
|
+
secretPresets: {
|
|
73
|
+
api: ['API_TOKEN'],
|
|
74
|
+
},
|
|
75
|
+
derivedEnvVars: {},
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
return createDaggerCommandRunner({
|
|
79
|
+
projectId: 'test-project-id',
|
|
80
|
+
env: 'prod',
|
|
81
|
+
path: '/ci/test',
|
|
82
|
+
daggerEnv,
|
|
83
|
+
...overrides,
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
describe('createDaggerCommandRunner()', () => {
|
|
88
|
+
beforeEach(() => {
|
|
89
|
+
vi.clearAllMocks()
|
|
90
|
+
vi.stubEnv('CI', undefined)
|
|
91
|
+
vi.stubEnv('GITHUB_ACTIONS', undefined)
|
|
92
|
+
vi.stubEnv('DAGGER_CLOUD_TOKEN', undefined)
|
|
93
|
+
mocks.exportJson.mockResolvedValue([
|
|
94
|
+
{ key: 'API_TOKEN', value: 'test-api-token', comment: '' },
|
|
95
|
+
{ key: 'DAGGER_CLOUD_TOKEN', value: 'test-dagger-cloud-token', comment: '' },
|
|
96
|
+
])
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
afterEach(() => {
|
|
100
|
+
vi.unstubAllEnvs()
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
it('builds the expected DAGGER_OPTIONS payload from the infisical export', async () => {
|
|
104
|
+
const runDaggerCommand = createRunner()
|
|
105
|
+
await runDaggerCommand('test-cmd', {
|
|
106
|
+
args: { push: 'true' },
|
|
107
|
+
env: { NODE_ENV: 'production' },
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
expect(mocks.exportCmd).toHaveBeenCalledWith(
|
|
111
|
+
'infisical export --silent --format=json --projectId test-project-id --env prod --path /ci/test'
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
const { env } = getSpawnCall()
|
|
115
|
+
expect(JSON.parse(env.DAGGER_OPTIONS as string)).toStrictEqual({
|
|
116
|
+
args: { push: 'true' },
|
|
117
|
+
env: { NODE_ENV: 'production' },
|
|
118
|
+
secrets: { API_TOKEN: 'test-api-token' },
|
|
119
|
+
})
|
|
120
|
+
expect(env.DAGGER_CLOUD_TOKEN).toBeUndefined()
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('passes DAGGER_CLOUD_TOKEN to the runner env (but not DAGGER_OPTIONS) in CI', async () => {
|
|
124
|
+
vi.stubEnv('CI', 'true')
|
|
125
|
+
vi.stubEnv('GITHUB_ACTIONS', 'true')
|
|
126
|
+
|
|
127
|
+
const runDaggerCommand = createRunner()
|
|
128
|
+
await runDaggerCommand('test-cmd')
|
|
129
|
+
|
|
130
|
+
const { env } = getSpawnCall()
|
|
131
|
+
const daggerOptions = JSON.parse(env.DAGGER_OPTIONS as string)
|
|
132
|
+
expect(daggerOptions.secrets).not.toHaveProperty('DAGGER_CLOUD_TOKEN')
|
|
133
|
+
expect(daggerOptions.env).toStrictEqual({ CI: 'true', GITHUB_ACTIONS: 'true' })
|
|
134
|
+
expect(env.DAGGER_CLOUD_TOKEN).toBe('test-dagger-cloud-token')
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('spawns dagger directly without an op wrapper', async () => {
|
|
138
|
+
const runDaggerCommand = createRunner()
|
|
139
|
+
await runDaggerCommand('test-cmd', { extraArgs: ['--verbose'] })
|
|
140
|
+
|
|
141
|
+
const { argv } = getSpawnCall()
|
|
142
|
+
expect(argv[0]).toBe('dagger')
|
|
143
|
+
expect(argv).not.toContain('op')
|
|
144
|
+
expect(argv).toStrictEqual([
|
|
145
|
+
'dagger',
|
|
146
|
+
'call',
|
|
147
|
+
'test-cmd',
|
|
148
|
+
'--verbose',
|
|
149
|
+
'--options=env://DAGGER_OPTIONS',
|
|
150
|
+
])
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
describe('docker socket', () => {
|
|
154
|
+
it('appends --docker-socket for dockerCommands when the socket exists', async () => {
|
|
155
|
+
mocks.exists.mockResolvedValue(true)
|
|
156
|
+
|
|
157
|
+
const runDaggerCommand = createRunner({ dockerCommands: ['build'] })
|
|
158
|
+
await runDaggerCommand('build')
|
|
159
|
+
|
|
160
|
+
const { argv } = getSpawnCall()
|
|
161
|
+
expect(argv).toStrictEqual([
|
|
162
|
+
'dagger',
|
|
163
|
+
'call',
|
|
164
|
+
'build',
|
|
165
|
+
'--docker-socket=/var/run/docker.sock',
|
|
166
|
+
'--options=env://DAGGER_OPTIONS',
|
|
167
|
+
])
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
it('does not append --docker-socket for non-docker commands', async () => {
|
|
171
|
+
mocks.exists.mockResolvedValue(true)
|
|
172
|
+
|
|
173
|
+
const runDaggerCommand = createRunner({ dockerCommands: ['build'] })
|
|
174
|
+
await runDaggerCommand('test-cmd')
|
|
175
|
+
|
|
176
|
+
const { argv } = getSpawnCall()
|
|
177
|
+
expect(argv).not.toContain('--docker-socket=/var/run/docker.sock')
|
|
178
|
+
expect(mocks.exists).not.toHaveBeenCalled()
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
it('does not append --docker-socket when the socket does not exist', async () => {
|
|
182
|
+
mocks.exists.mockResolvedValue(false)
|
|
183
|
+
|
|
184
|
+
const runDaggerCommand = createRunner({ dockerCommands: ['build'] })
|
|
185
|
+
await runDaggerCommand('build')
|
|
186
|
+
|
|
187
|
+
const { argv } = getSpawnCall()
|
|
188
|
+
expect(argv).not.toContain('--docker-socket=/var/run/docker.sock')
|
|
189
|
+
})
|
|
190
|
+
})
|
|
191
|
+
})
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { z } from 'zod/v4'
|
|
2
|
+
import { $, fs } from 'zx'
|
|
3
|
+
|
|
4
|
+
import type { DaggerEnv, DaggerEnvConfig } from './dagger-env.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A single secret returned by `infisical export --format=json`
|
|
8
|
+
*/
|
|
9
|
+
export type InfisicalSecret = z.infer<typeof InfisicalSecret>
|
|
10
|
+
export const InfisicalSecret = z.object({
|
|
11
|
+
key: z.string(),
|
|
12
|
+
value: z.string(),
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Configuration for running Dagger commands with Infisical integration
|
|
17
|
+
*/
|
|
18
|
+
export interface RunDaggerCommandConfig<T extends DaggerEnvConfig = DaggerEnvConfig> {
|
|
19
|
+
/** Infisical project ID */
|
|
20
|
+
projectId: string
|
|
21
|
+
/** Infisical environment slug (e.g. `prod`) */
|
|
22
|
+
env: string
|
|
23
|
+
/** Infisical folder path to fetch secrets from (e.g. `/ci/my-repo`) */
|
|
24
|
+
path: string
|
|
25
|
+
/** Commands that should include Docker socket if available */
|
|
26
|
+
dockerCommands?: string[]
|
|
27
|
+
/** Hook to run before executing the command (e.g., vendor file setup) */
|
|
28
|
+
beforeCommand?: () => Promise<void>
|
|
29
|
+
/** DaggerEnv instance for schema validation and type safety */
|
|
30
|
+
daggerEnv: DaggerEnv<T>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Options for individual command execution
|
|
35
|
+
*/
|
|
36
|
+
export interface RunDaggerCommandOptions {
|
|
37
|
+
/** Arguments to pass to the Dagger command */
|
|
38
|
+
args?: Record<string, any>
|
|
39
|
+
/** Additional environment variables */
|
|
40
|
+
env?: Record<string, string>
|
|
41
|
+
/** Additional command-line arguments */
|
|
42
|
+
extraArgs?: string[]
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Creates a function to run Dagger commands with Infisical integration
|
|
47
|
+
* @param config Configuration for the command runner
|
|
48
|
+
* @returns Function to execute Dagger commands
|
|
49
|
+
*/
|
|
50
|
+
export function createDaggerCommandRunner<T extends DaggerEnvConfig>(
|
|
51
|
+
config: RunDaggerCommandConfig<T>
|
|
52
|
+
) {
|
|
53
|
+
return async function runDaggerCommand(
|
|
54
|
+
commandName: string,
|
|
55
|
+
options?: RunDaggerCommandOptions
|
|
56
|
+
): Promise<void> {
|
|
57
|
+
const { args = {}, env = {}, extraArgs = [] } = options ?? {}
|
|
58
|
+
|
|
59
|
+
// Run any pre-command setup
|
|
60
|
+
if (config.beforeCommand) {
|
|
61
|
+
await config.beforeCommand()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Environment variables to pass to the `dagger` command
|
|
65
|
+
const envVars: Record<string, string> = {}
|
|
66
|
+
|
|
67
|
+
const commandArgs: string[] = [...extraArgs]
|
|
68
|
+
|
|
69
|
+
// Add Docker socket for specific commands if available
|
|
70
|
+
if (config.dockerCommands?.includes(commandName)) {
|
|
71
|
+
try {
|
|
72
|
+
if (await fs.exists('/var/run/docker.sock')) {
|
|
73
|
+
commandArgs.push('--docker-socket=/var/run/docker.sock')
|
|
74
|
+
}
|
|
75
|
+
} catch {
|
|
76
|
+
// Ignore if fs is not available or docker socket doesn't exist
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Fetch secrets from Infisical
|
|
81
|
+
const exportedSecrets = InfisicalSecret.array().parse(
|
|
82
|
+
await $`infisical export --silent --format=json --projectId ${config.projectId} --env ${config.env} --path ${config.path}`.json()
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
// Extract secrets into a key/value map
|
|
86
|
+
const secrets = exportedSecrets.reduce(
|
|
87
|
+
(acc, s) => {
|
|
88
|
+
acc[s.key] = s.value
|
|
89
|
+
return acc
|
|
90
|
+
},
|
|
91
|
+
{} as Record<string, string>
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
// Pass dagger cloud token in CI because we don't have user auth
|
|
95
|
+
if (process.env.CI !== undefined && secrets.DAGGER_CLOUD_TOKEN !== undefined) {
|
|
96
|
+
envVars.DAGGER_CLOUD_TOKEN = secrets.DAGGER_CLOUD_TOKEN
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Build environment variables for Dagger
|
|
100
|
+
const daggerEnv: Record<string, string> = { ...env }
|
|
101
|
+
if (process.env.CI !== undefined) {
|
|
102
|
+
daggerEnv.CI = process.env.CI
|
|
103
|
+
}
|
|
104
|
+
if (process.env.GITHUB_ACTIONS !== undefined) {
|
|
105
|
+
daggerEnv.GITHUB_ACTIONS = process.env.GITHUB_ACTIONS
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Validate and serialize dagger options
|
|
109
|
+
const daggerOptions = config.daggerEnv.getOptionsSchema().parse({
|
|
110
|
+
args,
|
|
111
|
+
env: daggerEnv,
|
|
112
|
+
secrets,
|
|
113
|
+
})
|
|
114
|
+
envVars.DAGGER_OPTIONS = JSON.stringify(daggerOptions)
|
|
115
|
+
|
|
116
|
+
// Construct the command
|
|
117
|
+
const cmd: string[] = [
|
|
118
|
+
'dagger',
|
|
119
|
+
'call',
|
|
120
|
+
commandName,
|
|
121
|
+
...commandArgs,
|
|
122
|
+
'--options=env://DAGGER_OPTIONS',
|
|
123
|
+
]
|
|
124
|
+
|
|
125
|
+
// Execute the command
|
|
126
|
+
await $({
|
|
127
|
+
env: {
|
|
128
|
+
...process.env,
|
|
129
|
+
...envVars,
|
|
130
|
+
},
|
|
131
|
+
stdio: 'inherit',
|
|
132
|
+
})`${cmd}`
|
|
133
|
+
}
|
|
134
|
+
}
|
package/dist/op.d.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod/v4';
|
|
2
|
-
/**
|
|
3
|
-
* 1Password vault information
|
|
4
|
-
*/
|
|
5
|
-
export type OPVault = z.infer<typeof OPVault>;
|
|
6
|
-
export declare const OPVault: z.ZodObject<{
|
|
7
|
-
id: z.ZodString;
|
|
8
|
-
name: z.ZodString;
|
|
9
|
-
}, z.core.$strip>;
|
|
10
|
-
/**
|
|
11
|
-
* 1Password item section information
|
|
12
|
-
*/
|
|
13
|
-
export type OPSection = z.infer<typeof OPSection>;
|
|
14
|
-
export declare const OPSection: z.ZodObject<{
|
|
15
|
-
id: z.ZodString;
|
|
16
|
-
label: z.ZodOptional<z.ZodString>;
|
|
17
|
-
}, z.core.$strip>;
|
|
18
|
-
/**
|
|
19
|
-
* 1Password item field
|
|
20
|
-
*/
|
|
21
|
-
export type OPField = z.infer<typeof OPField>;
|
|
22
|
-
export declare const OPField: z.ZodObject<{
|
|
23
|
-
id: z.ZodString;
|
|
24
|
-
section: z.ZodOptional<z.ZodObject<{
|
|
25
|
-
id: z.ZodString;
|
|
26
|
-
label: z.ZodOptional<z.ZodString>;
|
|
27
|
-
}, z.core.$strip>>;
|
|
28
|
-
type: z.ZodEnum<{
|
|
29
|
-
STRING: "STRING";
|
|
30
|
-
CONCEALED: "CONCEALED";
|
|
31
|
-
MONTH_YEAR: "MONTH_YEAR";
|
|
32
|
-
}>;
|
|
33
|
-
purpose: z.ZodOptional<z.ZodEnum<{
|
|
34
|
-
NOTES: "NOTES";
|
|
35
|
-
}>>;
|
|
36
|
-
label: z.ZodString;
|
|
37
|
-
value: z.ZodString;
|
|
38
|
-
reference: z.ZodString;
|
|
39
|
-
}, z.core.$strip>;
|
|
40
|
-
/**
|
|
41
|
-
* 1Password item category
|
|
42
|
-
*/
|
|
43
|
-
export type OPCategory = z.infer<typeof OPCategory>;
|
|
44
|
-
export declare const OPCategory: z.ZodEnum<{
|
|
45
|
-
SECURE_NOTE: "SECURE_NOTE";
|
|
46
|
-
}>;
|
|
47
|
-
/**
|
|
48
|
-
* Complete 1Password item structure
|
|
49
|
-
*/
|
|
50
|
-
export type OPItem = z.infer<typeof OPItem>;
|
|
51
|
-
export declare const OPItem: z.ZodObject<{
|
|
52
|
-
id: z.ZodString;
|
|
53
|
-
title: z.ZodString;
|
|
54
|
-
favorite: z.ZodBoolean;
|
|
55
|
-
version: z.ZodInt;
|
|
56
|
-
vault: z.ZodObject<{
|
|
57
|
-
id: z.ZodString;
|
|
58
|
-
name: z.ZodString;
|
|
59
|
-
}, z.core.$strip>;
|
|
60
|
-
category: z.ZodEnum<{
|
|
61
|
-
SECURE_NOTE: "SECURE_NOTE";
|
|
62
|
-
}>;
|
|
63
|
-
last_edited_by: z.ZodString;
|
|
64
|
-
created_at: z.ZodISODateTime;
|
|
65
|
-
updated_at: z.ZodISODateTime;
|
|
66
|
-
additional_information: z.ZodString;
|
|
67
|
-
sections: z.ZodArray<z.ZodObject<{
|
|
68
|
-
id: z.ZodString;
|
|
69
|
-
label: z.ZodOptional<z.ZodString>;
|
|
70
|
-
}, z.core.$strip>>;
|
|
71
|
-
fields: z.ZodArray<z.ZodObject<{
|
|
72
|
-
id: z.ZodString;
|
|
73
|
-
section: z.ZodOptional<z.ZodObject<{
|
|
74
|
-
id: z.ZodString;
|
|
75
|
-
label: z.ZodOptional<z.ZodString>;
|
|
76
|
-
}, z.core.$strip>>;
|
|
77
|
-
type: z.ZodEnum<{
|
|
78
|
-
STRING: "STRING";
|
|
79
|
-
CONCEALED: "CONCEALED";
|
|
80
|
-
MONTH_YEAR: "MONTH_YEAR";
|
|
81
|
-
}>;
|
|
82
|
-
purpose: z.ZodOptional<z.ZodEnum<{
|
|
83
|
-
NOTES: "NOTES";
|
|
84
|
-
}>>;
|
|
85
|
-
label: z.ZodString;
|
|
86
|
-
value: z.ZodString;
|
|
87
|
-
reference: z.ZodString;
|
|
88
|
-
}, z.core.$strip>>;
|
|
89
|
-
}, z.core.$strip>;
|
|
90
|
-
//# sourceMappingURL=op.d.ts.map
|
package/dist/op.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"op.d.ts","sourceRoot":"","sources":["../src/op.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAA;AAE1B;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,OAAO,CAAC,CAAA;AAC7C,eAAO,MAAM,OAAO;;;iBAGlB,CAAA;AAEF;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,SAAS,CAAC,CAAA;AACjD,eAAO,MAAM,SAAS;;;iBAGpB,CAAA;AAEF;;GAEG;AACH,MAAM,MAAM,OAAO,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,OAAO,CAAC,CAAA;AAC7C,eAAO,MAAM,OAAO;;;;;;;;;;;;;;;;;iBAQlB,CAAA;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,UAAU,CAAC,CAAA;AACnD,eAAO,MAAM,UAAU;;EAA0B,CAAA;AAEjD;;GAEG;AACH,MAAM,MAAM,MAAM,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,MAAM,CAAC,CAAA;AAC3C,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAajB,CAAA"}
|
package/dist/op.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod/v4';
|
|
2
|
-
export const OPVault = z.object({
|
|
3
|
-
id: z.string(),
|
|
4
|
-
name: z.string(),
|
|
5
|
-
});
|
|
6
|
-
export const OPSection = z.object({
|
|
7
|
-
id: z.string(),
|
|
8
|
-
label: z.string().optional(),
|
|
9
|
-
});
|
|
10
|
-
export const OPField = z.object({
|
|
11
|
-
id: z.string(),
|
|
12
|
-
section: OPSection.optional(),
|
|
13
|
-
type: z.enum(['STRING', 'CONCEALED', 'MONTH_YEAR']),
|
|
14
|
-
purpose: z.enum(['NOTES']).optional(),
|
|
15
|
-
label: z.string(),
|
|
16
|
-
value: z.string(),
|
|
17
|
-
reference: z.string(),
|
|
18
|
-
});
|
|
19
|
-
export const OPCategory = z.enum(['SECURE_NOTE']);
|
|
20
|
-
export const OPItem = z.object({
|
|
21
|
-
id: z.string(),
|
|
22
|
-
title: z.string(),
|
|
23
|
-
favorite: z.boolean(),
|
|
24
|
-
version: z.int(),
|
|
25
|
-
vault: OPVault,
|
|
26
|
-
category: OPCategory,
|
|
27
|
-
last_edited_by: z.string(),
|
|
28
|
-
created_at: z.iso.datetime(),
|
|
29
|
-
updated_at: z.iso.datetime(),
|
|
30
|
-
additional_information: z.string(),
|
|
31
|
-
sections: z.array(OPSection),
|
|
32
|
-
fields: z.array(OPField),
|
|
33
|
-
});
|
|
34
|
-
//# sourceMappingURL=op.js.map
|
package/dist/op.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"op.js","sourceRoot":"","sources":["../src/op.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,QAAQ,CAAA;AAM1B,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC;IAC/B,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE;IACd,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE;CAChB,CAAC,CAAA;AAMF,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,CAAC,MAAM,CAAC;IACjC,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE;IACd,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE;CAC5B,CAAC,CAAA;AAMF,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,CAAC,MAAM,CAAC;IAC/B,EAAE,EAAE,CAAC,CAAC,MAAM,EAAE;IACd,OAAO,EAAE,SAAS,CAAC,QAAQ,EAAE;IAC7B,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,QAAQ,EAAE,WAA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