The DeepL CLI project is a command-line utility developed in Go (Golang) that allows users to translate text by leveraging the DeepL API. The main objective is to provide a simple, fast, and configurable tool for text translations directly from the terminal.
The project's architecture is designed to be modular and easy to maintain, separating concerns between command-line management, configuration, and interaction with the external API.
cmd/deepl/main.goMain entry point of the application. It initializes and executes the root command defined by Cobra.
cmd/deepl/cmd/root.goContains the definition of the root command (deepl) and handles the parsing of command-line arguments (-s, -t, text to translate). It also orchestrates the loading of the configuration and the call to the translation function.
internal/config/config.goManages the loading and validation of the .deepl.toml configuration file. This file is located in the user's ~/.deepl/ directory and contains the DeepL API key, as well as the default source and target languages.
translateText function in root.go)This function is responsible for constructing the HTTP request to the DeepL API, sending the request, handling responses (success or errors), and extracting the translated text.
deepl command with the text to translate and optional flags (source language, target language).main.go module launches the root command.root.go) uses Cobra to parse CLI arguments..deepl.toml via config.go.translateText function is called with the text, languages (source and target), and the API key.translateText constructs an HTTP POST request to the DeepL API, including the text, languages, and API key (via the Authorization header).The .deepl.toml configuration file must be manually created by the user in ~/.deepl/. It must contain at least the DeepL API key and the default target language. Example:
api_key = "YOUR_DEEPL_API_KEY"
target_lang = "en"
source_lang = "fr" # Optional
The tool uses the DeepL REST API. Requests are made to https://api-free.deepl.com/v2/translate using the POST method with a JSON request body. Authentication is done via the Authorization header with the format DeepL-Auth-Key YOUR_API_KEY.