In today's post we will be talking about my experience
working in a cli project for the past few days.
One of the things that I wanted to focus this year was Java
so after completing some challenges of advent of code and doing
a little bit of practice in the general language I decide to start
creating a little project to learn more about the language and to have
a more close experience with the general development of a java application.
So let's beggin.
What we are building
We are building a simple cli application that emulates three comands of the
unix system, this are ls
, mv
and ifconfig
the reason for choose these three
where the next.
- I want to work with files
- I want to know more about how ifconfig is build under the hook
Building the project
The first step that I take was trying a tool to manage all of my code,
so after a quick search I found that maven
is one of the options in the java
ecosystem for this task. So I quickly follow the basic getting started guide in
the docs, and get the basic skeleton of my project.
Then I decide to spent sometime trying to figure out how to do testing in java with
JUnit
however, after a while, found a bit difficult to test the cli, in part for a lack
of experience in the general ecosystem and more specially in the cli word.
For overcoming this, the decision was really simple, focus on building first the commands
and later on, how I grow and learn more about java and their ecosystem came back and add
them to the project.
Creating LS
So the first command that I create was LS
, I won't lie, the first thing that I did was
asking to chatgpt how is does this in java, I was expecting for my experience with javascript
to use classes and methods from the IO
(fs in javascript). I never though that the File
class, has a build in method for listing the files inside a directory and that for that
to work we need to create a new instance of file with the option .
.
File directory = new File(".");
File[] files = directory.listFiles();
I also add a simple text indicating if the file was a Folder
, Hidden
or a File
Creating mv
Creating this command was in general really similar to the ls
the main difference is that
I start creating a simple function that move a specif file and then later on I add
the posibility to pass two arguments the originalPath
and the destinyPath
for the files.
Creating ifconfig
For creating this command I follow the same process that in the pass two, I ask chatgpt to
generate a basic code for the command and then either I add some more steps in front of What
chatgpt gives to me, or I went to the java docs to learn about the classes that where being
used in the command. For example here I learn more about the java.net
package.
And a general overview of how to work with that package and Enumeration.
Conclusion
I think that in the next projects I will be focusing in another things to start to understand
java better, for example the next one is a simple connection to a database using a JDBC
Here is the code for the project junix
Top comments (0)