-
-
Notifications
You must be signed in to change notification settings - Fork 4
feat: Add environment variable support for CLI arguments #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,280 @@ | ||||||||||||
| # Environment Variables | ||||||||||||
|
|
||||||||||||
| StructKit supports environment variables to configure CLI arguments without requiring them to be specified on the command line. This is particularly useful for CI/CD pipelines, containerized environments, and automation workflows. | ||||||||||||
|
|
||||||||||||
| ## Overview | ||||||||||||
|
|
||||||||||||
| Environment variables allow you to set default values for CLI arguments. Command-line arguments always take precedence over environment variables, ensuring flexibility when needed. | ||||||||||||
|
|
||||||||||||
| ## High Priority Environment Variables | ||||||||||||
|
|
||||||||||||
| ### `STRUCTKIT_GLOBAL_SYSTEM_PROMPT` | ||||||||||||
|
|
||||||||||||
| Sets the global system prompt for OpenAI integration. | ||||||||||||
|
|
||||||||||||
| **CLI Equivalent:** `--global-system-prompt` / `-p` | ||||||||||||
|
|
||||||||||||
| **Use Case:** Typically a long or sensitive value that users want to set once. Avoids repeating the same prompt across multiple invocations. Ideal for CI/CD workflows and container initialization. | ||||||||||||
|
|
||||||||||||
| **Example:** | ||||||||||||
| ```bash | ||||||||||||
| export STRUCTKIT_GLOBAL_SYSTEM_PROMPT="You are a helpful assistant for generating project structures." | ||||||||||||
| structkit generate my-structure ./output | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ### `STRUCTKIT_INPUT_STORE` | ||||||||||||
|
|
||||||||||||
| Sets the path to the input store for template variables. | ||||||||||||
|
|
||||||||||||
| **CLI Equivalent:** `--input-store` / `-n` | ||||||||||||
|
|
||||||||||||
| **Default:** `/tmp/structkit/input.json` | ||||||||||||
|
|
||||||||||||
| **Use Case:** Allows users to set a consistent default location for input data. Useful for workflows that need persistent input across multiple runs. | ||||||||||||
|
|
||||||||||||
| **Example:** | ||||||||||||
| ```bash | ||||||||||||
| export STRUCTKIT_INPUT_STORE="/home/user/structkit-inputs/data.json" | ||||||||||||
| structkit generate my-structure ./output | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ### `STRUCTKIT_BACKUP_PATH` | ||||||||||||
|
|
||||||||||||
| Sets the default backup location for file backups. | ||||||||||||
|
|
||||||||||||
| **CLI Equivalent:** `--backup` / `-b` | ||||||||||||
|
|
||||||||||||
| **Use Case:** Set a default backup location project-wide or environment-wide. Saves typing in repetitive operations. Useful for ensuring backups go to a specific location (e.g., mounted volume in containers). | ||||||||||||
|
|
||||||||||||
| **Example:** | ||||||||||||
| ```bash | ||||||||||||
| export STRUCTKIT_BACKUP_PATH="/backups/structkit" | ||||||||||||
| structkit generate my-structure ./output | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ## Medium Priority Environment Variables | ||||||||||||
|
|
||||||||||||
| ### `STRUCTKIT_FILE_STRATEGY` | ||||||||||||
|
|
||||||||||||
| Sets the default strategy for handling existing files. | ||||||||||||
|
|
||||||||||||
| **CLI Equivalent:** `--file-strategy` / `-f` | ||||||||||||
|
|
||||||||||||
| **Valid Values:** `overwrite`, `skip`, `append`, `rename`, `backup` | ||||||||||||
|
|
||||||||||||
| **Default:** `overwrite` | ||||||||||||
|
|
||||||||||||
| **Use Case:** Let users set a preferred default strategy. Could prevent accidental data loss if set to 'skip' or 'backup' by default. | ||||||||||||
|
|
||||||||||||
| **Example:** | ||||||||||||
| ```bash | ||||||||||||
| export STRUCTKIT_FILE_STRATEGY="backup" | ||||||||||||
| structkit generate my-structure ./output | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ### `STRUCTKIT_NON_INTERACTIVE` | ||||||||||||
|
|
||||||||||||
| Enables or disables interactive mode for all commands. | ||||||||||||
|
|
||||||||||||
| **CLI Equivalent:** `--non-interactive` | ||||||||||||
|
|
||||||||||||
| **Valid Values:** `true`, `1`, `yes` (case-insensitive) for enabled; any other value for disabled | ||||||||||||
|
|
||||||||||||
| **Default:** `false` | ||||||||||||
|
|
||||||||||||
| **Use Case:** Boolean flag useful for CI/CD pipelines. Could be set in environment and applied across all commands. | ||||||||||||
|
|
||||||||||||
| **Example:** | ||||||||||||
| ```bash | ||||||||||||
| export STRUCTKIT_NON_INTERACTIVE=true | ||||||||||||
| structkit generate my-structure ./output | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ### `STRUCTKIT_OUTPUT_MODE` | ||||||||||||
|
|
||||||||||||
| Sets the default output mode for the generate command. | ||||||||||||
|
|
||||||||||||
| **CLI Equivalent:** `--output` / `-o` | ||||||||||||
|
|
||||||||||||
| **Valid Values:** `console`, `file` | ||||||||||||
|
|
||||||||||||
| **Default:** `file` | ||||||||||||
|
|
||||||||||||
| **Use Case:** Some users might prefer 'console' output by default. Useful for pipeline integration. | ||||||||||||
|
|
||||||||||||
| **Example:** | ||||||||||||
| ```bash | ||||||||||||
| export STRUCTKIT_OUTPUT_MODE="console" | ||||||||||||
| structkit generate my-structure ./output | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ## Shared Environment Variables | ||||||||||||
|
|
||||||||||||
| ### `STRUCTKIT_STRUCTURES_PATH` | ||||||||||||
|
|
||||||||||||
| Sets the path to custom structure definitions. | ||||||||||||
|
|
||||||||||||
| **CLI Equivalent:** `--structures-path` / `-s` | ||||||||||||
|
|
||||||||||||
| **Use Case:** Allows specifying custom structures directory that applies across all commands (generate, list, info, generate-schema). | ||||||||||||
|
|
||||||||||||
| **Example:** | ||||||||||||
| ```bash | ||||||||||||
| export STRUCTKIT_STRUCTURES_PATH="/home/user/my-structures" | ||||||||||||
| structkit list | ||||||||||||
| structkit generate my-custom-structure ./output | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ### `STRUCTKIT_LOG_LEVEL` | ||||||||||||
|
|
||||||||||||
| Sets the logging level for all commands. | ||||||||||||
|
|
||||||||||||
| **CLI Equivalent:** `--log` (generate command) | ||||||||||||
|
|
||||||||||||
| **Valid Values:** `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL` | ||||||||||||
|
|
||||||||||||
| **Default:** `INFO` | ||||||||||||
|
|
||||||||||||
| **Use Case:** Control verbosity of output for debugging or production deployments. | ||||||||||||
|
|
||||||||||||
| **Example:** | ||||||||||||
| ```bash | ||||||||||||
| export STRUCTKIT_LOG_LEVEL="DEBUG" | ||||||||||||
| structkit generate my-structure ./output | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ## Precedence Rules | ||||||||||||
|
|
||||||||||||
| Command-line arguments **always take precedence** over environment variables. This allows environment variables to set sensible defaults while maintaining the ability to override them when needed. | ||||||||||||
|
|
||||||||||||
| **Precedence Order (highest to lowest):** | ||||||||||||
| 1. Command-line arguments | ||||||||||||
| 2. Environment variables | ||||||||||||
| 3. Built-in defaults | ||||||||||||
|
|
||||||||||||
| **Example:** | ||||||||||||
| ```bash | ||||||||||||
| # Set default via environment variable | ||||||||||||
| export STRUCTKIT_FILE_STRATEGY="backup" | ||||||||||||
|
|
||||||||||||
| # Override with CLI argument | ||||||||||||
| structkit generate --file-strategy skip my-structure ./output | ||||||||||||
| # Uses 'skip', not 'backup' | ||||||||||||
|
|
||||||||||||
| # Use default from environment | ||||||||||||
| structkit generate my-structure ./output | ||||||||||||
| # Uses 'backup' from STRUCTKIT_FILE_STRATEGY | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ## Docker and Containerization | ||||||||||||
|
|
||||||||||||
| Environment variables are particularly useful when running StructKit in containers: | ||||||||||||
|
|
||||||||||||
| **Docker Example:** | ||||||||||||
| ```bash | ||||||||||||
| docker run \ | ||||||||||||
| -e STRUCTKIT_STRUCTURES_PATH=/custom/structures \ | ||||||||||||
| -e STRUCTKIT_NON_INTERACTIVE=true \ | ||||||||||||
| -e STRUCTKIT_FILE_STRATEGY=backup \ | ||||||||||||
| -v /custom/structures:/custom/structures \ | ||||||||||||
| -v $(pwd):/workdir \ | ||||||||||||
| ghcr.io/httpdss/structkit:main generate my-structure /workdir/output | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| **Docker Compose Example:** | ||||||||||||
| ```yaml | ||||||||||||
| version: '3' | ||||||||||||
| services: | ||||||||||||
| structkit: | ||||||||||||
| image: ghcr.io/httpdss/structkit:main | ||||||||||||
| environment: | ||||||||||||
| STRUCTKIT_STRUCTURES_PATH: /custom/structures | ||||||||||||
| STRUCTKIT_NON_INTERACTIVE: "true" | ||||||||||||
| STRUCTKIT_FILE_STRATEGY: backup | ||||||||||||
| STRUCTKIT_LOG_LEVEL: DEBUG | ||||||||||||
| volumes: | ||||||||||||
| - /custom/structures:/custom/structures | ||||||||||||
| - ./output:/workdir | ||||||||||||
| command: generate my-structure /workdir/output | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ## CI/CD Pipeline Integration | ||||||||||||
|
|
||||||||||||
| **GitHub Actions Example:** | ||||||||||||
| ```yaml | ||||||||||||
| name: Generate Project Structure | ||||||||||||
|
|
||||||||||||
| on: [push, pull_request] | ||||||||||||
|
|
||||||||||||
| jobs: | ||||||||||||
| generate: | ||||||||||||
| runs-on: ubuntu-latest | ||||||||||||
| env: | ||||||||||||
| STRUCTKIT_NON_INTERACTIVE: "true" | ||||||||||||
| STRUCTKIT_BACKUP_PATH: /tmp/backups | ||||||||||||
| STRUCTKIT_FILE_STRATEGY: backup | ||||||||||||
| steps: | ||||||||||||
| - uses: actions/checkout@v3 | ||||||||||||
|
|
||||||||||||
| - name: Install StructKit | ||||||||||||
| run: pip install structkit | ||||||||||||
|
|
||||||||||||
| - name: Generate structure | ||||||||||||
| run: structkit generate my-structure ./generated-project | ||||||||||||
|
|
||||||||||||
| - name: Upload generated files | ||||||||||||
| uses: actions/upload-artifact@v3 | ||||||||||||
| with: | ||||||||||||
| name: generated-project | ||||||||||||
| path: generated-project/ | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| **GitLab CI Example:** | ||||||||||||
| ```yaml | ||||||||||||
| generate_structure: | ||||||||||||
| image: python:3.11 | ||||||||||||
| script: | ||||||||||||
| - pip install structkit | ||||||||||||
| - structkit generate my-structure ./output | ||||||||||||
| artifacts: | ||||||||||||
| paths: | ||||||||||||
| - output/ | ||||||||||||
| only: | ||||||||||||
| - main | ||||||||||||
| environment: | ||||||||||||
| - STRUCTKIT_NON_INTERACTIVE: "true" | ||||||||||||
| - STRUCTKIT_FILE_STRATEGY: "backup" | ||||||||||||
| ``` | ||||||||||||
|
|
||||||||||||
| ## Best Practices | ||||||||||||
|
|
||||||||||||
| 1. **Use Environment Variables for Defaults** - Set environment variables for values that don't change frequently | ||||||||||||
| 2. **Override When Needed** - Use CLI arguments for one-off changes or specific use cases | ||||||||||||
| 3. **Document Configuration** - Document which environment variables are used in your project | ||||||||||||
| 4. **Sensitive Data** - Store sensitive data (like API keys) in environment variables, not in configuration files | ||||||||||||
| 5. **Validation** - Test environment variable configuration to ensure it works as expected | ||||||||||||
|
|
||||||||||||
| ## Troubleshooting | ||||||||||||
|
|
||||||||||||
| ### Environment variable not being picked up | ||||||||||||
|
|
||||||||||||
| 1. Verify the environment variable is set: `echo $VARIABLE_NAME` | ||||||||||||
| 2. Ensure you're using the correct variable name (case-sensitive on Linux/macOS) | ||||||||||||
| 3. If running in Docker, check that the environment variable is passed correctly with `-e` | ||||||||||||
| 4. Restart your terminal or shell session after setting the variable | ||||||||||||
|
|
||||||||||||
| ### CLI argument not overriding environment variable | ||||||||||||
|
|
||||||||||||
| This should not happen - CLI arguments always take precedence. If you're experiencing this: | ||||||||||||
| 1. Verify the CLI argument is correctly formatted | ||||||||||||
| 2. Check that you're using the correct argument name (e.g., `--file-strategy` not `--strategy`) | ||||||||||||
| 3. Ensure there are no spaces or special characters in the argument value | ||||||||||||
|
|
||||||||||||
| ### Boolean environment variables not working correctly | ||||||||||||
|
|
||||||||||||
| For `STRUCTKIT_NON_INTERACTIVE`, only `true`, `1`, and `yes` (case-insensitive) are recognized as true values. All other values are treated as false, including: | ||||||||||||
| - `"true "` (with trailing space) | ||||||||||||
| - `"True"` in mixed case | ||||||||||||
|
||||||||||||
| For `STRUCTKIT_NON_INTERACTIVE`, only `true`, `1`, and `yes` (case-insensitive) are recognized as true values. All other values are treated as false, including: | |
| - `"true "` (with trailing space) | |
| - `"True"` in mixed case | |
| For `STRUCTKIT_NON_INTERACTIVE`, only `true`, `1`, and `yes` (case-insensitive, e.g., `"True"`, `"TRUE"`, `"YeS"`) are recognized as true values. All other values are treated as false, including: | |
| - `"true "` (with trailing space) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The GitLab CI YAML syntax is incorrect. Environment variables should be defined under 'variables:' key, not 'environment:'. The current syntax will cause a pipeline configuration error.