Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add picocli command interface/logic #13

Merged
merged 18 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions .github/workflows/builder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
name: "build-docker-image"

on:
pull_request:

push:
tags:
- "v*"

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
platform: [ linux/amd64, linux/arm64 ]

steps:
- name: "Checkout the repository"
uses: actions/checkout@v4

- name: "Extract metadata for the Docker image"
if: github.event_name == 'push' && github.ref_type == 'tag' # Only extract metadata if we push a tag
id: metadata_extraction
uses: docker/metadata-action@v5
with:
images: ghcr.io/${{ github.repository }}

- name: "Login to GitHub Container Registry"
if: github.event_name == 'push' && github.ref_type == 'tag' # Only login if we push a tag
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: "Set up Docker Buildx"
uses: docker/setup-buildx-action@v3

- name: "Build and eventually push container image"
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
platforms: ${{ matrix.platform }}
tags: ${{ steps.metadata_extraction.outputs.tags }}
labels: ${{ steps.metadata_extraction.outputs.labels }}
push: ${{ github.event_name == 'push' && github.ref_type == 'tag' }} # Only push built image if we push a tag
18 changes: 18 additions & 0 deletions .github/workflows/commit-checks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
name: "commits-checks"

on:
pull_request:

jobs:
commits-checks:
runs-on: ubuntu-latest

steps:
- name: "Check out the repository"
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history for all tags and branches

- name: "Check commit messages"
uses: docker://aevea/commitsar
19 changes: 19 additions & 0 deletions .github/workflows/release-please.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
name: "release-please"

on:
push:
branches:
- main

jobs:
release-please:
runs-on: ubuntu-latest
outputs:
releases_created: ${{ steps.release-please.outputs.releases_created }}
steps:
- uses: google-github-actions/release-please-action@v4
id: release-please
with:
token: ${{ secrets.GITHUB_TOKEN }}
release-type: maven
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
.idea/jarRepositories.xml
.idea/modules.xml
.idea/workspace.xml
.idea/uiDesigner.xml
*.iws
*.iml
*.ipr
Expand Down
4 changes: 4 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
".": "1.0.0"
}
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /project
COPY . .
RUN if [ -d "target" ]; then rm -rf target; fi; \
./mvnw dependency:go-offline clean compile package; \
rm -rf target/original-*.jar # Remove original jar otherwise it will be copied to the final image.

FROM eclipse-temurin:21-jre
COPY --from=build /project/target/*.jar /app/shadow.jar
WORKDIR /data
ENTRYPOINT ["java", "-jar", "/app/shadow.jar"]
CMD ["--help"]
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# DAI-2024-PW1

This repository contains the code for our first practical work in the DAI course of HEIG-VD during 2024.

// TODO Add comment saying that files are different because hash is different
// TODO Same as before but to say message before and after has the same hash
4 changes: 2 additions & 2 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ch.heigvd.dai</groupId>
<artifactId>DAI-2425-PW1</artifactId>
<version>1.0-SNAPSHOT</version>
<artifactId>shadow</artifactId>
<version>0.0.1</version>
<build>
<plugins>
<plugin>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<modelVersion>4.0.0</modelVersion>

<groupId>ch.heigvd.dai</groupId>
<artifactId>DAI-2425-PW1</artifactId>
<version>1.0-SNAPSHOT</version>
<artifactId>shadow</artifactId>
<version>0.0.1</version>

<properties>
<maven.compiler.source>21</maven.compiler.source>
Expand Down
26 changes: 26 additions & 0 deletions release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"packages": {
".": {
"release-type": "maven"
}
},
"changelog-sections": [
{ "type": "feat", "section": "Features" },
{ "type": "feature", "section": "Features" },
{ "type": "fix", "section": "Bug Fixes" },
{ "type": "perf", "section": "Performance Improvements" },
{ "type": "revert", "section": "Reverts" },
{ "type": "docs", "section": "Documentation" },
{ "type": "style", "section": "Styles" },
{ "type": "chore", "section": "Miscellaneous Chores" },
{ "type": "refactor", "section": "Code Refactoring" },
{ "type": "test", "section": "Tests" },
{ "type": "build", "section": "Build System" },
{ "type": "ci", "section": "Continuous Integration" }
],
"bump-minor-pre-major": true,
"bump-patch-for-minor-pre-major": true,
"include-v-in-tag": true,
"include-component-in-tag": false,
"pull-request-title-pattern": "chore: release ${version}"
}
26 changes: 25 additions & 1 deletion src/main/java/ch/heigvd/dai/Main.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,31 @@
package ch.heigvd.dai;

import ch.heigvd.dai.commands.Root;
import java.io.File;
import picocli.CommandLine;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
// Example from class -
// https://github.com/heig-vd-dai-course/heig-vd-dai-course-java-ios-practical-content-template/blob/778e1934a64f338e93613afbb31dd9e92356d7c4/src/main/java/ch/heigvd/dai/Main.java#L10
// Define command name - source: https://stackoverflow.com/a/11159435
String jarFilename =
new File(Main.class.getProtectionDomain().getCodeSource().getLocation().getPath())
lentidas marked this conversation as resolved.
Show resolved Hide resolved
.getName();

// Create root command and CommandLine
CommandLine command = new CommandLine(new Root());
command.setCommandName(jarFilename).setCaseInsensitiveEnumValuesAllowed(true);

// Calculate execution time for root command and its subcommands
Long start = System.nanoTime();
int exitCode = command.execute(args);
Long end = System.nanoTime();

if (exitCode == 0) {
System.out.println("Execution time: " + (end - start) / (1000 * 1000) + " ms");
}

System.exit(exitCode);
}
}
103 changes: 0 additions & 103 deletions src/main/java/ch/heigvd/dai/MessageManipulator.java

This file was deleted.

64 changes: 64 additions & 0 deletions src/main/java/ch/heigvd/dai/commands/Expose.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package ch.heigvd.dai.commands;
PedroAS7 marked this conversation as resolved.
Show resolved Hide resolved

import ch.heigvd.dai.utilities.BmpFile;
import ch.heigvd.dai.utilities.FileManipulator;
import ch.heigvd.dai.utilities.MessageManipulator;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.concurrent.Callable;
import picocli.CommandLine;

@CommandLine.Command(
name = "expose",
description = "Expose an hidden file/message from a BMP image.")
public class Expose implements Callable<Integer> {
@CommandLine.ParentCommand private Root parent;

@Override
public Integer call() {
if (Files.exists(Paths.get(parent.getFilenameMessage()))) {
if (Files.isDirectory(Paths.get(parent.getFilenameMessage()))) {
lentidas marked this conversation as resolved.
Show resolved Hide resolved
System.err.println(
"The path for the message output is a directory.\n"
+ "Please provide a different path.");
return 1;
PedroAS7 marked this conversation as resolved.
Show resolved Hide resolved
}
if (parent.forceDisabled()) {
System.err.println(
"A file already exists on the output path provided.\n"
+ "If you are sure you want to overwrite that file, enable the '--force' flag.");
return 1;
PedroAS7 marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (!parent.isBmpFileValid()) {
System.err.println(
"BMP file provided either does not exist or is a directory.\n"
+ "Please provide a path to a valid BMP file.");
return 1;
PedroAS7 marked this conversation as resolved.
Show resolved Hide resolved
}

try {
BmpFile bmpFile = new BmpFile(parent.getFilenameBmpImage());

if (!bmpFile.hasMessage()) {
System.err.println(
"BMP file provided does not have a message hidden inside.\n"
+ "Please provide a path to a BMP file containing a message.");
return 1;
PedroAS7 marked this conversation as resolved.
Show resolved Hide resolved
}

byte[] message = new byte[bmpFile.getMessageLength()];
FileManipulator fileManipulator = new FileManipulator(parent.getFilenameMessage(), true);
MessageManipulator messageManipulator = new MessageManipulator();

messageManipulator.exposeMessage(bmpFile, message);
fileManipulator.writeBytesToFile(message);
} catch (Exception e) {
System.err.println("Error while hiding message! Exception message:\n" + e.getMessage());
return 1;
PedroAS7 marked this conversation as resolved.
Show resolved Hide resolved
}

return 0;
PedroAS7 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading