Skip to content

Commit

Permalink
Add the git commands, Bug fix the output log
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Nov 18, 2019
1 parent 4b3e605 commit 04e6468
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 20 deletions.
30 changes: 25 additions & 5 deletions src/main/java/org/lorislab/samo/CommonCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,22 @@ abstract class CommonCommand implements Callable<Integer> {
)
private File pom;

/**
* The log info message
*
* @param message the message.
*/
void output(String message) {
System.out.println(message);
}

/**
* The log info message
*
* @param message the message.
*/
void logInfo(String message) {
System.out.println("SAMO >> " + message);
output("SAMO >> " + message);
}

/**
Expand All @@ -79,7 +88,7 @@ void logInfo(String message) {
*/
void logVerbose(String message) {
if (verbose) {
System.out.println("SAMO >> " + message);
output("SAMO >> " + message);
}
}

Expand Down Expand Up @@ -117,9 +126,9 @@ static class Return {
/**
* Call command line
*
* @param command
* @param errorMessage
* @return
* @param command the command.
* @param errorMessage the error message.
* @return the command line response.
*/
Return cmd(String command, String errorMessage) {
final Return result = new Return();
Expand Down Expand Up @@ -177,4 +186,15 @@ private static CompletableFuture<String> readOutStream(InputStream is) {
});
}

String gitBranch() {
Return r = cmd("git rev-parse --abbrev-ref HEAD", "Error git branch name");
logVerbose("Git branch: " + r.response);
return r.response;
}

String gitHash(int length) {
Return r = cmd("git rev-parse --short=" + length + " HEAD", "Error git hash");
logVerbose("Git hash: " + r.response);
return r.response;
}
}
10 changes: 4 additions & 6 deletions src/main/java/org/lorislab/samo/DockerCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,10 @@ public static class Release extends CommonCommand {
public Integer call() throws Exception {
MavenProject project = getMavenProject();

Return r = cmd("git rev-parse --short=" + length + " HEAD", "Error git sha");
logVerbose("Git hash: " + r.response);
String hash = gitHash(length);

Version version = Version.valueOf(project.id.version.value);
Version pullVersion = version.setPreReleaseVersion(r.response);
Version pullVersion = version.setPreReleaseVersion(hash);

// set the docker release repository
if (releaseRepository == null || releaseRepository.isEmpty()) {
Expand Down Expand Up @@ -218,9 +217,8 @@ public Integer call() throws Exception {

String branchTag = "";
if (branch) {
Return r = cmd("git rev-parse --abbrev-ref HEAD", "Error git branch name");
logVerbose("Git branch: " + r.response);
branchTag = repository + "/" + image + ":" + r.response;
String branchName = gitBranch();
branchTag = repository + "/" + image + ":" + branchName;
sb.append(" -t ").append(branchTag);
log.append(",").append(branchTag);
}
Expand Down
99 changes: 99 additions & 0 deletions src/main/java/org/lorislab/samo/GitCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2019 lorislab.org.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.lorislab.samo;

import picocli.CommandLine;

import java.util.concurrent.Callable;

/**
* The git command.
*/
@CommandLine.Command(name = "git",
description = "Git commands",
subcommands = {
GitCommand.Branch.class,
GitCommand.Hash.class
}
)
public class GitCommand implements Callable<Integer> {

/**
* The command specification.
*/
@CommandLine.Spec
CommandLine.Model.CommandSpec spec;

/**
* Show help of the maven commands.
*
* @return the exit code.
*/
@Override
public Integer call() {
spec.commandLine().usage(System.out);
return CommandLine.ExitCode.OK;
}

/**
* The maven version command.
*/
@CommandLine.Command(name = "branch", description = "Show current branch")
public static class Branch extends CommonCommand {

/**
* Returns the current version of the maven project.
*
* @return the exit code.
*/
@Override
public Integer call() {
output(gitBranch());
return CommandLine.ExitCode.OK;
}
}

/**
* Gets the git hash.
*/
@CommandLine.Command(name = "hash", description = "Show the git hash")
public static class Hash extends CommonCommand {

/**
* The length of the git sha
*/
@CommandLine.Option(
names = {"-l", "--length"},
paramLabel = "LENGTH",
defaultValue = "7",
required = true,
description = "the git hash length"
)
int length;

/**
* Sets the maven project to release version.
*
* @return the exit code.
*/
@Override
public Integer call() {
output(gitHash(length));
return CommandLine.ExitCode.OK;
}
}

}
18 changes: 9 additions & 9 deletions src/main/java/org/lorislab/samo/MavenCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.lorislab.samo;

import com.github.zafarkhaja.semver.Version;
import org.lorislab.samo.data.MavenProject;
import picocli.CommandLine;

Expand All @@ -26,7 +27,7 @@
@CommandLine.Command(name = "maven",
description = "Maven version commands",
subcommands = {
MavenCommand.Version.class,
MavenCommand.MavenVersion.class,
MavenCommand.Release.class,
MavenCommand.Git.class,
MavenCommand.Snapshot.class
Expand Down Expand Up @@ -55,7 +56,7 @@ public Integer call() {
* The maven version command.
*/
@CommandLine.Command(name = "version", description = "Show current maven version")
public static class Version extends CommonCommand {
public static class MavenVersion extends CommonCommand {

/**
* Returns the current version of the maven project.
Expand All @@ -66,7 +67,7 @@ public static class Version extends CommonCommand {
@Override
public Integer call() throws Exception {
MavenProject project = getMavenProject();
logInfo(project.id.version.value);
output(project.id.version.value);
return CommandLine.ExitCode.OK;
}
}
Expand All @@ -86,7 +87,7 @@ public static class Release extends CommonCommand {
@Override
public Integer call() throws Exception {
MavenProject project = getMavenProject();
com.github.zafarkhaja.semver.Version version = com.github.zafarkhaja.semver.Version.valueOf(project.id.version.value);
Version version = Version.valueOf(project.id.version.value);
String releaseVersion = version.getNormalVersion();
logVerbose(releaseVersion);
project.setVersion(releaseVersion);
Expand All @@ -110,7 +111,7 @@ public static class Snapshot extends CommonCommand {
@Override
public Integer call() throws Exception {
MavenProject project = getMavenProject();
com.github.zafarkhaja.semver.Version version = com.github.zafarkhaja.semver.Version.valueOf(project.id.version.value);
Version version = Version.valueOf(project.id.version.value);
version = version.setPreReleaseVersion(SNAPSHOT);
String releaseVersion = version.toString();
logVerbose(releaseVersion);
Expand Down Expand Up @@ -148,12 +149,11 @@ public static class Git extends CommonCommand {
@Override
public Integer call() throws Exception {
MavenProject project = getMavenProject();
com.github.zafarkhaja.semver.Version version = com.github.zafarkhaja.semver.Version.valueOf(project.id.version.value);
Version version = Version.valueOf(project.id.version.value);

Return r = cmd("git rev-parse --short=" + length + " HEAD", "Error git sha");
logVerbose("Git hash: " + r.response);
String hash = gitHash(length);

version = version.setPreReleaseVersion(r.response);
version = version.setPreReleaseVersion(hash);
String releaseVersion = version.toString();
logVerbose(releaseVersion);

Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/lorislab/samo/Samo.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
MavenCommand.class,
CreateCommand.class,
DockerCommand.class,
GitCommand.class,
CommandLine.HelpCommand.class
}
)
Expand Down

0 comments on commit 04e6468

Please sign in to comment.