Contributing to Open Source : Week 5
(This story is part of the weekly assignments for my internship at Nearsoft. I hope that some of the insights I learned this week can help others in their learning journey.)
Issues and pull requests
This week, I worked on two issues:
╔═══════════╦═══════════════════════════════════╦═══════════════╗
║ project ║ issue ║ status ║
╠═══════════╬═══════════════════════════════════╬═══════════════╣
║ smug ║ Add `create` and `edit` commands ║ Doing ║
║ open-desk ║ New Site UI calling backend ║ Merged ║
╚═══════════╩═══════════════════════════════════╩═══════════════╝
How I was trying to contribute
For the open-desk issue (Javascript + React), I had to connect the UI to the backend by calling the /site
endpoint with the following request:
{
"orgId": "string",
"id": "string",
"name": "string",
"location": "string",
"floors": [
{
"floorId": "string",
"name": "string",
"openDesk": "string",
"reservedDesk": "string"
}
]
}
For the smug issue (Go), I had to add the ability to create and edit configuration files within smug
command.
For example, smug <edit|create> <project>
should open ~/.config/smug/project.yml
in the $EDITOR
.
Any positive or negative results
For the open-desk issue (Javascript + React), I successfully connected the UI with the backend and received a successful response from the API. The most difficult part was to work on the logic for the input of the dynamically added <Add Floor/>
component.
To solve this, I used the onBlur event in the input button, which would trigger a function to update the floor array as follows:
const onBlurFloor = (e, id) => {
console.log(floor)
if (floor.length > id) {
floor[id].name = e.target.value
}
else {
if (e.target.value != '') {
floor.push({
name: e.target.value
})
}
}
}
For the smug issue (Go), I created the Edit function which gets the full executable path for the editor and opens it.
func (smug Smug) Edit(filename string) error {
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vim"
}
executable, err := exec.LookPath(editor)
if err != nil {
return err
}
cmd := exec.Command(executable, filename)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
However, I still need to pass the filename argument into the function in order to edit or create a new file if it doesn’t exist. Also, I need to create the tests for the function and implement the error handling.
What did I learn this week?
A lot! 🙂 I learned about Error Handling and Go. I also started to study Grokking Algorithms by Aditya Y. Bhargava in order to review Data Structures and Algorithms. Last, I learned about SOLID Principles.
In Javascript, Error Handling looks like this:
try {
firstFunction();
secondFunction();
thirdFunction();
} catch (e) {
console.error(e);
}
However, in Go it looks like this:
func Open(name string) (file *File, err error){
f, err := os.Open("filename.ext")
if err != nil {
log.Fatal(err)
}
// do something with the open *File f
}