Skip to content

Commit 447fa72

Browse files
added enums for manutentibility of the code and navigation command and check-file command
1 parent 17feaf2 commit 447fa72

4 files changed

Lines changed: 158 additions & 11 deletions

File tree

src/main/java/com/francescodisalesdev/gitcli/GitCli.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.francescodisalesdev.gitcli;
22

3+
import com.francescodisalesdev.gitcli.utility.ErrorMessages;
34
import org.json.simple.parser.ParseException;
5+
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;
46
import org.springframework.shell.standard.ShellComponent;
57
import org.springframework.shell.standard.ShellMethod;
68
import com.francescodisalesdev.gitcli.service.GitService;
@@ -31,8 +33,8 @@ public void query(String repository,@ShellOption(defaultValue = "1") int page)
3133
}
3234
catch(IOException | ParseException e)
3335
{
34-
System.out.println("something bad happened while processing the request");
35-
e.printStackTrace();
36+
System.out.println(ErrorMessages.SOMETHING_BAD);
37+
System.out.println(e.getMessage());
3638
}
3739

3840
}
@@ -48,7 +50,8 @@ public void getPages(String repository)
4850
}
4951
catch(IOException e)
5052
{
51-
System.out.println("something bad happened while processing the request");
53+
System.out.println(ErrorMessages.SOMETHING_BAD);
54+
System.out.println(e.getMessage());
5255
}
5356
}
5457

@@ -63,7 +66,8 @@ public void clone(String repository,String localPath)
6366
}
6467
catch(IOException | InterruptedException e)
6568
{
66-
System.out.println("something bad happened");
69+
System.out.println(ErrorMessages.SOMETHING_BAD);
70+
System.out.println(e.getMessage());
6771
}
6872

6973
}
@@ -79,7 +83,8 @@ public void cloneBranch(String repository,String localPath,String branch)
7983
}
8084
catch(InterruptedException | IOException e)
8185
{
82-
System.out.println("Something bad happened");
86+
System.out.println(ErrorMessages.SOMETHING_BAD);
87+
System.out.println(e.getMessage());
8388
}
8489

8590
}
@@ -95,7 +100,41 @@ public void info(String repository)
95100
}
96101
catch (IOException e)
97102
{
98-
System.out.println("Something bad happened");
103+
System.out.println(ErrorMessages.SOMETHING_BAD);
104+
System.out.println(e.getMessage());
105+
}
106+
107+
}
108+
109+
@ShellMethod("go to a specific folder")
110+
public void navigate(String path)
111+
{
112+
GitService gitService = new GitService();
113+
114+
try
115+
{
116+
gitService.navigateService(path);
117+
}
118+
catch (IOException e)
119+
{
120+
System.out.println(ErrorMessages.SOMETHING_BAD);
121+
System.out.println(e.getMessage());
122+
}
123+
124+
}
125+
@ShellMethod("check a file")
126+
public void checkFile(String path)
127+
{
128+
GitService gitService = new GitService();
129+
130+
try
131+
{
132+
gitService.checkFileService(path);
133+
}
134+
catch(IOException e)
135+
{
136+
System.out.println(ErrorMessages.SOMETHING_BAD);
137+
System.out.println(e.getMessage());
99138
}
100139

101140
}

src/main/java/com/francescodisalesdev/gitcli/service/GitService.java

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.francescodisalesdev.gitcli.service;
22

33
import com.francescodisalesdev.gitcli.utility.ConditionChecker;
4+
import com.francescodisalesdev.gitcli.utility.ErrorMessages;
5+
import com.francescodisalesdev.gitcli.utility.SystemMessages;
46
import org.json.simple.JSONObject;
57
import org.json.simple.parser.JSONParser;
68
import org.json.simple.parser.ParseException;
@@ -85,7 +87,7 @@ public void filterPage(String param,List<String> response) throws IOException
8587
System.out.println(responseIterator.next());
8688
}
8789

88-
System.out.println("total pages for this search:");
90+
System.out.println(SystemMessages.TOTAL_PAGES.toString());
8991
this.getPages(param);
9092

9193
}
@@ -98,10 +100,10 @@ public void cloneRepository(String repository,String localPath) throws IOExcepti
98100
processBuilder.directory(new File(localPath));
99101
Process process = processBuilder.start();
100102

101-
System.out.println("cloning ...");
103+
System.out.println(SystemMessages.CLONING_PROCESS);
102104
process.waitFor();
103105

104-
System.out.println("cloning done!");
106+
System.out.println(SystemMessages.CLONING_DONE);
105107

106108
}
107109

@@ -113,10 +115,10 @@ public void cloneRepositoryBranch(String repository,String localPath,String bran
113115
processBuilder.directory(new File(localPath));
114116
Process process = processBuilder.start();
115117

116-
System.out.println("cloning ...");
118+
System.out.println(SystemMessages.CLONING_PROCESS);
117119
process.waitFor();
118120

119-
System.out.println("cloning done!");
121+
System.out.println(SystemMessages.CLONING_DONE);
120122

121123

122124
}
@@ -145,5 +147,57 @@ public void getInfoRepository(String repository) throws IOException
145147

146148
}
147149

150+
public void navigateService(String path)throws IOException
151+
{
152+
if(path.startsWith("/"))
153+
path = path.substring(1);
154+
155+
if(!path.contains("blob") && path.contains("tree"))
156+
{
157+
Document document = Jsoup.connect("https://github.com/"+path).get();
158+
Elements elements = document.select("a");
159+
160+
for(Element element : elements)
161+
{
162+
if(!element.toString().isBlank() && !(element.toString().contains("https")||element.toString().contains("http")))
163+
if(element.toString().contains("blob") || element.toString().contains("tree"))
164+
System.out.println(element.attr("href"));
165+
}
166+
}
167+
else
168+
{
169+
if(path.contains("blob"))
170+
System.out.println(ErrorMessages.BLOB_ERROR);
171+
else
172+
System.out.println(ErrorMessages.INVALID_PATH);
173+
}
174+
175+
}
176+
177+
public void checkFileService(String path) throws IOException
178+
{
179+
if(path.startsWith("/"))
180+
path = path.substring(1);
181+
182+
if(path.contains("blob") && !path.contains("tree"))
183+
{
184+
Document document = Jsoup.connect("https://github.com/"+path).get();
185+
Elements elements = document.select("td");
186+
187+
for(Element element : elements)
188+
{
189+
if(!element.toString().isBlank() && !(element.toString().contains("https")||element.toString().contains("http")))
190+
if(element.attr("class").toString().contains("blob-code"))
191+
{
192+
System.out.println(element.text());
193+
}
194+
195+
}
196+
}
197+
else
198+
{
199+
System.out.println(ErrorMessages.INVALID_PATH);
200+
}
201+
}
148202

149203
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.francescodisalesdev.gitcli.utility;
2+
3+
public enum ErrorMessages
4+
{
5+
INVALID_PATH
6+
{
7+
@Override
8+
public String toString() {
9+
return "Invalid path";
10+
}
11+
},
12+
BLOB_ERROR
13+
{
14+
@Override
15+
public String toString() {
16+
return "Your request was a blob request. Use check-file command to check the file";
17+
}
18+
},
19+
SOMETHING_BAD
20+
{
21+
@Override
22+
public String toString() {
23+
return "Something bad happened";
24+
}
25+
26+
27+
}
28+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.francescodisalesdev.gitcli.utility;
2+
3+
public enum SystemMessages
4+
{
5+
CLONING_PROCESS
6+
{
7+
@Override
8+
public String toString() {
9+
return "Cloning ...";
10+
}
11+
},
12+
CLONING_DONE
13+
{
14+
@Override
15+
public String toString() {
16+
return "Clone done!";
17+
}
18+
},
19+
TOTAL_PAGES
20+
{
21+
@Override
22+
public String toString() {
23+
return "Total pages for this search:";
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)