I recently had the opportunity to contribute to my friend's git repository for Lab 4 of my OSD600 course, and it involved adding support for TOML config files to make users' lives easier. In this blog post, I'll walk you through the process of implementing this new feature and share some insights into the Git workflow.
The Code Journey
The goal was to eliminate the need for users to constantly input command line arguments by allowing them to declare all of their choices in a TOML written configuration file. The command would be greatly simplified by this feature, which would also provide it more versatility.
I started by modifying the codebase, specifically the main() function, to recognize the "-c" or "--config" flags and read the config file. This part went relatively smoothly, and I used a well-maintained TOML library to parse the config file. The code was tested to ensure that it could handle missing or improperly formatted config files and that config options would override command line arguments.
This is the part I have contributed the most:
// Main function
function main() {
try {
const args = process.argv.slice(2);
let lang = "en-CA"; // Default language
let outputDir = "./til"; // Default output directory
// Parse command line arguments
for (let i = 0; i < args.length; i++) {
if (args[i] === "-c" || args[i] === "--config") {
// Use config file
const configFile = args[i + 1];
const configData = fs.readFileSync(configFile, "utf-8");
const config = toml.parse(configData);
if (config.output) {
outputDir = config.output;
}
if (config.lang) {
lang = config.lang;
}
break; // Config file overrides other options
} else if (args[i] === "-l" || args[i] === "--lang") {
lang = args[i + 1] || "en-CA";
// Skip the next argument as it has been processed as the language
i++;
}
}
const inputPath = args.find((arg) => !arg.startsWith("-"));
if (fs.existsSync(inputPath)) {
if (fs.lstatSync(inputPath).isDirectory()) {
processDirectory(inputPath, outputDir, lang);
} else {
processFile(inputPath, outputDir, lang);
}
console.log("Conversion completed successfully.");
process.exit(0); // Exit with success code
} else {
console.error("Error: Input file or directory not found.");
process.exit(1); // Exit with an error code
}
} catch (error) {
console.error(`Unexpected Error: ${error.message}`);
process.exit(1); // Exit with an error code
}
}
Git Collaboration
Working with Git remotes, in this case, my friend's repository, had its share of challenges. While it's a powerful tool for collaboration, keeping the repository clean and ensuring a seamless integration of new features can be tricky.
My pull request can be found here #10. It was essential to make my changes in a separate branch and maintain clear commit messages to make the code review process as smooth as possible. In this case, it worked well, and my pull request was eventually merged.
What I learned
From this experience, I learned the importance of a structured Git workflow. Creating a feature branch, committing regularly, and writing informative pull requests are essential for effective collaboration. In the future, I would focus on making smaller, more focused commits to make the code review process even more manageable.
Top comments (0)