Push command
Implement the push command
Exercise
We will implement the functionality of the push command. When run, it will read the contents of an .env file in the current directory and save it to the database. Optionally, you can pass a directory as argument to the command and it will read from that directory instead of the current one. You can also pass the βfilename option to specify a different filename than the default .env.
push
Pushes the contents of a local .env file to the database.
envtamer push [DIRECTORY_NAME] [-f|--filename <FILENAME>]
DIRECTORY_NAME: Optional. The directory containing the .env file. Defaults to the current working directory.-f|--filename: Optional. The name of the env file. Defaults to β.envβ.
This command reads the specified .env file and stores its contents in the database, associated with the given directory.
Go
To store the environment variables as key-value pairs you can use a map in Go. Both keys and values are strings. The keys represent the environment variable names, and the values represent their associated values.
envVars := make(map[string]string)
The bufio.Scanner is utilized to read the file line-by-line efficiently. This is particularly useful for processing text files like .env files, which contain environment variables listed line by line.
import (
"bufio"
)
scanner := bufio.NewScanner(file)
The strings packageβs functions are essential for text processing and manipulation in Go. They are highly optimized and provide robust support for handling UTF-8 encoded strings, making them indispensable tools for working with string data.
import (
"strings"
)
strings.TrimSpace(...)
strings.SplitN(...)
Python
In Python, you can use the os module to interact with the operating system and manage environment variables. The os.environ dictionary allows you to access and modify environment variables easily. We wonβt be using this in the push command, but itβs good to know.
We will be using an other function from the os module to read the contents of the file.
import os
with open(full_path, 'r', encoding='utf-8-sig') as file:
for line in file:
The open() function is used to open a file in Python. It takes the file path, mode (read, write, etc.), and encoding as arguments. The mode βrβ indicates that the file is opened for reading. The with statement is used to wrap the execution of a block with methods defined by a context manager. In this case, it is used to open the file and ensure that it is properly closed after its suite finishes, even if an exception is raised. It is similar to the try/finally block, but it is more concise and easier to read. The utf-8-sig encoding is used to read files that may contain a BOM (Byte Order Mark) at the beginning. The BOM is a special marker that indicates the fileβs encoding. By using utf-8-sig, Python will automatically handle the BOM if it exists, ensuring that the file is read correctly.
Create a new file named push_command.py in the envtamer folder. This file will contain the implementation of the push command. Create a new file named file_handler.py in the envtamer folder. This file will contain the code for reading and writing files.
your project structure should look like this:
βββ envtamer
βΒ Β βββ __init__.py
βΒ Β βββ cli.py
βΒ Β βββ file_handler.py
βΒ Β βββ init_command.py
βΒ Β βββ push_command.py
βββ envtamer_db
βΒ Β βββ __init__.py
βΒ Β βββ env_variable.py
βΒ Β βββ envtamer_db.py
βββ tests
βΒ Β βββ __init__.py
βββ poetry.lock
βββ pyproject.toml
βββ README.md
Solution
Solution
git clone --branch 05-push-command --single-branch https://github.com/XPRTZ/envtamer.git