11///usr/bin/env jbang "$0" "$@" ; exit $?
22
33//DEPS info.picocli:picocli:4.6.3
4- //DEPS org. gitlab4j: gitlab4j-api:5.4.0
4+ //DEPS https://github.com/ gitlab4j/ gitlab4j-api/commit/5805b41bae64b6c287abdbd51f63ce84d1ec8b90
55//JAVA 17
66
77import java .io .FileInputStream ;
@@ -31,6 +31,9 @@ class FileUploadScript implements Callable<Integer> {
3131 @ Option (names = { "-p" , "--project" }, description = "project (id or path)" )
3232 private String project ;
3333
34+ @ Option (names = { "-g" , "--group" }, description = "group" )
35+ private String group ;
36+
3437 @ Option (names = { "-f" , "--file" }, description = "Path to the file to uplaod" )
3538 private String filePath ;
3639
@@ -40,6 +43,9 @@ class FileUploadScript implements Callable<Integer> {
4043 @ Option (names = { "-c" , "--config" }, description = "configuration file location" )
4144 String configFile ;
4245
46+ @ Option (names = { "-v" , "--verbose" }, description = "log http trafic" )
47+ Boolean logHttp ;
48+
4349 @ Override
4450 public Integer call () throws Exception {
4551 Path file ;
@@ -59,30 +65,40 @@ public Integer call() throws Exception {
5965 throw new IllegalStateException ("Can not find file: " + fileToUpload .toAbsolutePath ());
6066 }
6167
62- try (GitLabApi gitLabApi = new GitLabApi (gitLabUrl , gitLabAuthValue )) {
63- projectIdMandatory ();
64- // FileUpload uplaodedFile = gitLabApi.getProjectApi()
65- // .uploadFile(idOrPath(projectId), fileToUpload.toFile());
68+ try (GitLabApi gitLabApi = createGitLabApi (gitLabUrl , gitLabAuthValue )) {
6669 if (filename == null ) {
6770 filename = fileToUpload .getFileName ()
6871 .toString ();
6972 }
70- FileUpload uplaodedFile = gitLabApi .getProjectApi ()
71- .uploadFile (idOrPath (project ), Files .newInputStream (fileToUpload ), filename , null );
72- System .out .println (uplaodedFile );
73+ if (project == null && group == null ) {
74+ throw new IllegalStateException ("Project or group is mandatory" );
75+ } else if (project != null && group != null ) {
76+ throw new IllegalStateException ("Project and group can't be set at the same time" );
77+ } else if (project != null ) {
78+ FileUpload uplaodedFile = gitLabApi .getProjectApi ()
79+ .uploadFile (idOrPath (project ), Files .newInputStream (fileToUpload ), filename , null );
80+ System .out .println (uplaodedFile );
81+ } else if (group != null ) {
82+ FileUpload uplaodedFile = gitLabApi .getGroupApi ()
83+ .uploadFile (idOrPath (group ), Files .newInputStream (fileToUpload ), filename , null );
84+ System .out .println (uplaodedFile );
85+ }
7386 }
7487 return 0 ;
7588 }
7689
77- private void filePathMandatory () {
78- if (filePath == null ) {
79- throw new IllegalStateException ("File is mandatory" );
90+ private GitLabApi createGitLabApi (String gitLabUrl , String gitLabAuthValue ) {
91+ if (logHttp != null && logHttp ) {
92+ return new GitLabApi (gitLabUrl , gitLabAuthValue )
93+ .withRequestResponseLogging (java .util .logging .Level .INFO ) ;
8094 }
95+ return new GitLabApi (gitLabUrl , gitLabAuthValue );
8196 }
8297
83- private void projectIdMandatory () {
84- if (project == null ) {
85- throw new IllegalStateException ("Project is mandatory" );
98+
99+ private void filePathMandatory () {
100+ if (filePath == null ) {
101+ throw new IllegalStateException ("File is mandatory" );
86102 }
87103 }
88104
0 commit comments