Saturday, 9 October 2021

How To Get The List Of All Environment Variables Key Used In A Golang Module/Project.

In this Articles We are going to cover up a real problem. When ever we separate independent modules from a big project to small it becomes hard to measure every aspects of the code for example how many external files we're using for that module, which db connection we're using for the module and also the most important part how many Environment Variables or we can call env variables we're using for that module.

These are real world problems and doing these all things manually can be pain in ass, So for avoiding these kind of problems we're going to implement a program/Tool, Which is going to extract all the environment variables(env) key used in a golang module.

How We're going to achieve that, for that we needed a flow to understand.

In Golang we uses os.Getenv("key") to get the environment variable value so we've got the pattern to achieve that.

1. Pattern

we have to check each file inside a module/project for os.Getenv("key") if we found any pattern while checking the golang file we're going to extract that element from that file or from line with the help of regular expression.


2. Regular Expression(regexp)

we're about to use regular expression to check the pattern in each file's each line to get our results. The function from regexp package we're going to use to extract information is FindlString.


3. FindString function Defination

FindString returns a string holding the text of the leftmost match in s of the regular expression. If there is no match, the return value is an empty string, but it will also be empty if the regular expression successfully matches an empty string. Use FindStringIndex or FindStringSubmatch if it is necessary to distinguish these cases.

So now we've know everything we needed to know before writing program let's start writing code


4. Directory Path Command


5. Walking On Directory/Module Recursivly to Get All Golang Files.

we're going to use walk function of filepath package to get all the files which have .go extension

Walk is less efficient than WalkDir, introduced in Go 1.16,
which avoids calling os.Lstat on every visited file or directory.

So wer're checking for each in this block to get the all .go extension files one by one.

6. Iterating Over All Files One By One

This portion of the code is the most important part and in this we're reading each file and after reading doing parsing see the code below.

Full Code

Labels: , ,

Wednesday, 6 October 2021

How to Read XML Files From A Directory Golang

Getting Specific Files from a directory by manually can be hard but it can be more hard when you know that there is thousands of and you have to choose only specific extension file. So How do we make it simple to file specific files from directory, So there is the simple answer programming. How? Just Write an script which reads the top level files from a directory and check the extensions of each file, If that file contains the same extension as you wanted then here you go. It's easy to say it in words, so let's start writing some code so you guys can have idea of it..



1. Load Required Flags/Arguments.

We're about to use few flags for this program which is help us to use targeted repository/direcotry full path. We're going to use flags package.

1. First we've loaded the package we're not using go mod so that why we're using direct main package
2. We've created a struct which will going to hold the path of the directory from which we've to extract all the XML files.
3. Created a function loadFlags which have flags package used and that function return the Flag struct with the path value in it.
3. From Line Number 23-30 calling the main function and in the main function we're calling loadFlags function which returns the Flag struct



2. Open Direcotry.

For Opening Folder We're going to use the os package which is inbuild package of Golang

In OpenDir function we're opening the directory by calling the os.Open function if it fails then it will going to return the error other the *os.File which holds information about the file and folders.



3. Get Files and Folder From Directory.

In checkFilesinDir function we're checking for the files and folder inside root path and then after getting any file and folder we're directly appending that value inside a array/slice and then reutring it.



4. Look for Xml files.

Next Step is to check for the xml files array

We've created another function which iterates the loop over the slice of string and check for the .xml extension if any file contains .xml that values will store in the xmlfiles variable, and then returning that back



5. Combine All Functions

Combining all the function into single function and then will going to call this function in the main function.



6. Full Code:

OUTPUT:

Labels: , , , ,