import { gracefulExit } from './chunk-XQSWW35O.js';
import { define } from './chunk-WIL4LMMA.js';
import { checkForSchemaErrors } from './chunk-4MCNC4YY.js';
import { loadVarlockEnvGraph } from './chunk-5SI4EGRL.js';
import { CliExitError } from './chunk-7M6WZ65V.js';
import { __name } from './chunk-IBAHCH6O.js';

// src/cli/commands/printenv.command.ts
var commandSpec = define({
  name: "printenv",
  description: "Print the resolved value of a single environment variable",
  args: {
    path: {
      type: "string",
      short: "p",
      multiple: true,
      description: "Path to a specific .env file or directory (with trailing slash) to use as the entry point (can be specified multiple times)"
    },
    "clear-cache": {
      type: "boolean",
      description: "Clear cache and re-resolve all values"
    },
    "skip-cache": {
      type: "boolean",
      description: "Skip cache entirely for this invocation"
    }
  },
  examples: `
Prints the resolved value of a single environment variable.
Useful within larger shell commands where you need a single env var value.

Examples:
  varlock printenv MY_VAR                    # Print the value of MY_VAR
  varlock printenv --path .env.prod MY_VAR   # Use a specific .env file
  varlock printenv --path ./config/ MY_VAR   # Use a specific directory
  varlock printenv -p ./envs -p ./overrides MY_VAR  # Use multiple directories

\u{1F4CD} Note: Use sh -c to embed this in shell commands, e.g.:
       sh -c 'do-something --token $(varlock printenv MY_TOKEN)'

\u{1F4A1} Tip: Unlike \`varlock run -- echo $MY_VAR\`, this works because the shell
       expansion happens after varlock has printed the value.
  `.trim()
});
var commandFn = /* @__PURE__ */ __name(async (ctx) => {
  const positionals = (ctx.positionals ?? []).slice(ctx.commandPath?.length ?? 0);
  if (!positionals.length) {
    throw new CliExitError("Missing required argument: variable name", {
      suggestion: "Run `varlock printenv MY_VAR` to print the value of MY_VAR"
    });
  }
  const varName = positionals[0];
  const envGraph = await loadVarlockEnvGraph({
    entryFilePaths: ctx.values.path,
    clearCache: ctx.values["clear-cache"],
    skipCache: ctx.values["skip-cache"]
  });
  checkForSchemaErrors(envGraph);
  if (!(varName in envGraph.configSchema)) {
    throw new CliExitError(`Variable "${varName}" not found in schema`);
  }
  await envGraph.resolveItemWithDeps(varName);
  const item = envGraph.configSchema[varName];
  if (item.validationState === "error") {
    for (const err of item.errors) {
      console.error(`\u{1F6A8} ${err.message}`);
    }
    return gracefulExit(1);
  }
  const value = item.resolvedValue;
  if (value === void 0 || value === null) {
    console.log("");
  } else {
    console.log(String(value));
  }
}, "commandFn");

export { commandFn, commandSpec };
//# sourceMappingURL=chunk-H6XEWY2M.js.map
//# sourceMappingURL=chunk-H6XEWY2M.js.map