Introduction:
What happens when we try to connect a remote repository to a local repository? We usually write several lines of commands to connect them before we can push our project.
I built a script using Go **and **Shell Scripting that requires only a GitHub repository URL.
Story:
I started learning Go for the purpose of understanding concurrency, low-level programming and backend engineering. It offers powerful features while maintaining a relatively simple syntax. Then I came to a video about making containers using Go, I learned how to make one, which also motivated me to learn Shell Scripting.
As an engineer, creating scripts is a great skill. They can automate software testing, project execution, log management, and many other tasks. But a doubt spawned in my head, "what should I make?", and then I deeply thought of an idea, I even asked ChatGPT for ideas, but nothing really clicked.
After a day, I created a repo for a project and saw an underlying issue, I had to copy and run the same commands whenever I want to connect to remote repo. Therefore, I worked on making a script which takes just a GitHub repository URL, validates it and creates a command.
Logics:
The projects consisted of five files :
📦 project-name
├── 📄 main.go
├── 📄 go.mod
├── 📜 basic.sh
├── 📖 README.md
└── 📄 script
Now after we move on to the functionality , do follow me on GitHub 👉 Github.
Usage of files:
main.go:
- This file validates the URL and exits if an error occurs.
- It also executes the commands required to connect the local repository to the remote repository. The code blocks are :
1) Validation:
func check_link_validity(link string) int{
// Validation Block
cmd := exec.Command("git", "ls-remote", "--exit-code", link)
_, err := cmd.CombinedOutput()
if err != nil {
os.Exit(1)
return 0
}
return 1
}
2) Commands:
cmd := exec.Command("git", "init")
cmd.Run()
cmd = exec.Command("git", "add", ".")
cmd.Run()
cmd = exec.Command("git", "commit", "-m", "Initial commit")
cmd.Run()
cmd = exec.Command("git", "branch", "-M", "main")
cmd.Run()
cmd = exec.Command("git", "remote", "add", "origin", link)
cmd.Run()
cmd = exec.Command("git", "push", "-u", "origin", "main")
cmd.Run()
basic.sh:
The script takes two inputs:
1) One of two options, a or b. Choosing a starts the repository connection process. Choosing b will provide access to other features that I plan to add in the future.
2) URL of GitHub Repo. The link will be attached as an argument to 'scripts' command ('scripts' is a compiled version of main.go).
The basic code block is:
echo "What do you want to do $USER?"
echo "a) Create Repo link"
echo "b) etc."
read -p "==>>" IN
if [ "$IN" == "b" ]; then
echo "Working on system"
elif [ "$IN" == "a" ]; then
# Read the repo link of the user and store it in REPO variable
read -p "Paste Repo Link: " REPO
# Github link verification command
# go run scripts/main.go $REPO
sleep 1s
./scripts $REPO
fi
End:
Finally, ending my very first blog feels a bit weird, but it also makes me happy. Documenting this project is another small step toward my goals.
---------------------------Thank You----------------------------
Top comments (0)