Skip to content

Commit 3a66405

Browse files
authored
Add Argcomplete for Command-Line Auto-Completion & Completer Functions (#10)
**Overview:** This pull request introduces command-line auto-completion for the `struct` script using the `argcomplete` library, along with some refactoring and enhancements in the command structure. Additionally, completer functions are implemented to streamline argument handling. **Changes:** - Added `argcomplete` for auto-completion in the `Dockerfile` and Python scripts. - Updated the `README.md` with instructions for enabling auto-completion. - Modified YAML template files to use a consistent syntax for variables (`{{@ var @}}`). - Integrated command-line completers for options such as `log_level` and `file_strategy` in the `struct_module`. - Updated the `requirements.txt` to include `argcomplete`. - Created tests for filter functions, focusing on `get_latest_release` and `stringify`. **Justification:** The inclusion of command-line auto-completion improves the developer experience by allowing faster and more accurate command entry. The added completers make commands more user-friendly and reduce errors during input. YAML files have been refactored for better consistency. **Impact:** - Users will now benefit from auto-completion when using the `struct` script. - The new completion functionality will require reloading of shell configuration for it to take effect. - Refactored YAML templates and improved testing coverage enhance maintainability and consistency.
1 parent 80dceb0 commit 3a66405

13 files changed

Lines changed: 126 additions & 33 deletions

.github/workflows/test-script.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ on:
44
push:
55
branches:
66
- main
7-
# pull_request:
8-
# branches:
9-
# - main
7+
pull_request:
8+
branches:
9+
- main
1010

1111
jobs:
1212
build:

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ COPY requirements.txt .
1010
# Install any needed packages specified in requirements.txt
1111
RUN pip install --no-cache-dir -r requirements.txt
1212

13+
# Install argcomplete and activate global completion
14+
RUN pip install argcomplete
15+
1316
# Copy the rest of the working directory contents into the container at /app
1417
COPY . .
1518

19+
# Register the script for auto-completion
20+
RUN echo 'eval "$(register-python-argcomplete struct)"' >> /etc/bash.bashrc
21+
1622
# Run your script when the container launches
1723
ENTRYPOINT ["python", "struct_module/main.py"]

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,40 @@ pip install -r requirements.txt
208208
pip install -r requirements.dev.txt
209209
```
210210

211+
## 🛠️ Command-Line Auto-Completion
212+
213+
This project uses [argcomplete](https://kislyuk.github.io/argcomplete/) to provide command-line auto-completion for the `struct` script. Follow these steps to enable auto-completion:
214+
215+
1. **Install `argcomplete`**:
216+
217+
```sh
218+
pip install argcomplete
219+
```
220+
221+
2. **Enable global completion** for your shell. This step is usually done once:
222+
223+
```sh
224+
activate-global-python-argcomplete
225+
```
226+
227+
3. **Register the script for auto-completion**. Add the following line to your shell's configuration file (e.g., `.bashrc`, `.zshrc`):
228+
229+
```sh
230+
eval "$(register-python-argcomplete struct)"
231+
```
232+
233+
4. **Reload your shell configuration**:
234+
235+
```sh
236+
source ~/.bashrc # or source ~/.zshrc for Zsh users
237+
```
238+
239+
After completing these steps, you should have auto-completion enabled for the `struct` script. You can test it by typing part of a command and pressing `Tab` to see the available options.
240+
241+
```sh
242+
struct <Tab>
243+
```
244+
211245
## 📜 License
212246

213247
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

contribs/documentation-template.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
structure:
22
- README.md:
33
content: |
4-
# {{% project_name %}}
4+
# {{@ project_name @}}
55
66
Brief description of the project.
77
@@ -18,7 +18,7 @@ structure:
1818
Guidelines for contributing to the project.
1919
- CONTRIBUTING.md:
2020
content: |
21-
# Contributing to {{% project_name %}}
21+
# Contributing to {{@ project_name @}}
2222
2323
## How to Contribute
2424
Guidelines for how to contribute to the project.

contribs/kubernetes-manifests.yaml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ structure:
44
apiVersion: apps/v1
55
kind: Deployment
66
metadata:
7-
name: {{% deployment_name | stringify %}}
7+
name: {{@ deployment_name | stringify @}}
88
labels:
9-
app: {{% app_name | stringify %}}
9+
app: {{@ app_name | stringify @}}
1010
spec:
1111
replicas: 2
1212
selector:
1313
matchLabels:
14-
app: {{% app_name | stringify %}}
14+
app: {{@ app_name | stringify @}}
1515
template:
1616
metadata:
1717
labels:
18-
app: {{% app_name | stringify %}}
18+
app: {{@ app_name | stringify @}}
1919
spec:
2020
containers:
21-
- name: {{% app_name | stringify %}}
22-
image: {{% image_name %}}:{{% image_tag %}}
21+
- name: {{@ app_name | stringify @}}
22+
image: {{@ image_name @}}:{{@ image_tag @}}
2323
ports:
2424
- containerPort: 80
2525
- service.yaml:
@@ -30,7 +30,7 @@ structure:
3030
name: example-service
3131
spec:
3232
selector:
33-
app: {{% app_name | stringify %}}
33+
app: {{@ app_name | stringify @}}
3434
ports:
3535
- protocol: TCP
3636
port: 80
@@ -84,18 +84,18 @@ structure:
8484
```
8585
8686
variables:
87-
app_name:
88-
description: "The name of the application."
89-
type: string
90-
default: "example-app"
91-
deployment_name:
92-
description: "The name of the deployment"
93-
type: string
94-
default: "example-app-deployment"
95-
image_name:
96-
description: "The name of the Docker image."
97-
type: string
98-
image_tag:
99-
description: "The tag of the Docker image."
100-
type: string
101-
default: "latest"
87+
- app_name:
88+
description: "The name of the application."
89+
type: string
90+
default: "example-app"
91+
- deployment_name:
92+
description: "The name of the deployment"
93+
type: string
94+
default: "example-app-deployment"
95+
- image_name:
96+
description: "The name of the Docker image."
97+
type: string
98+
- image_tag:
99+
description: "The tag of the Docker image."
100+
type: string
101+
default: "latest"

contribs/terraform-app.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ structure:
22
- main.tf:
33
content: |
44
# This is the main Terraform app main file.
5-
touch_file: {{% now().strftime('%Y-%m-%d') %}}
5+
touch_file: {{@ now().strftime('%Y-%m-%d') @}}
66
- variables.tf:
77
content: "# This is the Terraform variables file."
88
- outputs.tf:

contribs/terraform-module.yaml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@ structure:
1919
}
2020
- README.md:
2121
content: |
22-
# {{% module_name %}}
22+
# {{@ module_name @}}
2323
This module provisions an EC2 instance on AWS.
2424
## Usage
2525
```hcl
2626
module "example" {
27-
source = "./path/to/module/{{% module_name | stringify %}}"
27+
source = "./path/to/module/{{@ module_name | stringify @}}"
2828
instance_type = "t2.micro"
2929
}
3030
```
31+
variables:
32+
- module_name:
33+
description: "The name of the module."
34+
type: string
35+
default: "example-module"

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ openai
44
python-dotenv
55
jinja2
66
PyGithub
7+
argcomplete

struct_module/commands/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from struct_module.completers import log_level_completer
23

34
# Base command class
45
class Command:
@@ -8,7 +9,7 @@ def __init__(self, parser):
89
self.add_common_arguments()
910

1011
def add_common_arguments(self):
11-
self.parser.add_argument('-l', '--log', type=str, default='INFO', help='Set the logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)')
12+
self.parser.add_argument('-l', '--log', type=str, default='INFO', help='Set the logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)').completer = log_level_completer
1213
self.parser.add_argument('-c', '--config-file', type=str, help='Path to a configuration file')
1314
self.parser.add_argument('-i', '--log-file', type=str, help='Path to a log file')
1415

struct_module/commands/generate.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
import yaml
44
from struct_module.file_item import FileItem
5+
from struct_module.completers import file_strategy_completer
56

67
# Generate command class
78
class GenerateCommand(Command):
@@ -13,7 +14,7 @@ def __init__(self, parser):
1314
parser.add_argument('-d', '--dry-run', action='store_true', help='Perform a dry run without creating any files or directories')
1415
parser.add_argument('-v', '--vars', type=str, help='Template variables in the format KEY1=value1,KEY2=value2')
1516
parser.add_argument('-b', '--backup', type=str, help='Path to the backup folder')
16-
parser.add_argument('-f', '--file-strategy', type=str, choices=['overwrite', 'skip', 'append', 'rename', 'backup'], default='overwrite', help='Strategy for handling existing files')
17+
parser.add_argument('-f', '--file-strategy', type=str, choices=['overwrite', 'skip', 'append', 'rename', 'backup'], default='overwrite', help='Strategy for handling existing files').completer = file_strategy_completer
1718
parser.add_argument('-p', '--global-system-prompt', type=str, help='Global system prompt for OpenAI')
1819
parser.set_defaults(func=self.execute)
1920

0 commit comments

Comments
 (0)