Skip to content

Commit

Permalink
Add option to omit the project name (#495)
Browse files Browse the repository at this point in the history
Add option to omit the project name
  • Loading branch information
fawind authored May 6, 2020
1 parent 98a743c commit f9dbe04
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-495.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Add option to omit the project name
links:
- https://github.com/palantir/docker-compose-rule/pull/495
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.collect.ImmutableList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.regex.Pattern;
Expand All @@ -31,39 +32,54 @@
public abstract class ProjectName {

@Parameter
protected abstract String projectName();
protected abstract Optional<String> projectName();

@Check
protected void validate() {
checkState(projectName().trim().length() > 0, "ProjectName must not be blank.");
if (!projectName().isPresent()) {
return;
}

checkState(validCharacters(),
"ProjectName '%s' not allowed, please use lowercase letters and numbers only.", projectName());
checkState(
projectName().get().trim().length() > 0,
"ProjectName must not be blank. If you want to omit the project name, use ProjectName.omit()");

checkState(validCharacters(projectName().get()),
"ProjectName '%s' not allowed, please use lowercase letters and numbers only.", projectName().get());
}

// Only allows strings that docker-compose-cli would not modify
// https://github.com/docker/compose/blob/85e2fb63b3309280a602f1f76d77d3a82e53b6c2/compose/cli/command.py#L84
protected boolean validCharacters() {
protected boolean validCharacters(String projectName) {
Predicate<String> illegalCharacters = Pattern.compile("[^a-z0-9]").asPredicate();
return !illegalCharacters.test(projectName());
return !illegalCharacters.test(projectName);
}

public String asString() {
return projectName();
return projectName().orElseThrow(() -> new IllegalStateException(
"Cannot get the ProjectName as string if the ProjectName is omitted"));
}

public List<String> constructComposeFileCommand() {
return ImmutableList.of("--project-name", projectName());
return projectName().map(projectName -> ImmutableList.of("--project-name", projectName))
.orElseGet(ImmutableList::of);
}

public static ProjectName random() {
return ImmutableProjectName.of(UUID.randomUUID().toString().substring(0, 8));
return ImmutableProjectName.of(Optional.of(UUID.randomUUID().toString().substring(0, 8)));
}

/**
* A name consisting of lowercase letters and numbers only.
*/
public static ProjectName fromString(String name) {
return ImmutableProjectName.of(name);
return ImmutableProjectName.of(Optional.of(name));
}

/**
* Omit the project name which makes docker-compose use its current directory as project name.
*/
public static ProjectName omit() {
return ImmutableProjectName.of(Optional.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
Expand Down Expand Up @@ -66,6 +67,12 @@ public void should_disallow_names_in_from_string_factory() {
assertThat(command, contains("--project-name", "projectname"));
}

@Test
public void return_empty_command_when_omitted() {
List<String> command = ProjectName.omit().constructComposeFileCommand();
assertThat(command, empty());
}

@Test
public void reject_blanks_in_from_string() {
exception.expect(IllegalStateException.class);
Expand All @@ -76,7 +83,8 @@ public void reject_blanks_in_from_string() {
@Test
public void match_validation_behavior_of_docker_compose_cli() {
exception.expect(IllegalStateException.class);
exception.expectMessage("ProjectName 'Crazy#Proj ect!Name' not allowed, please use lowercase letters and numbers only.");
exception.expectMessage(
"ProjectName 'Crazy#Proj ect!Name' not allowed, please use lowercase letters and numbers only.");
ProjectName.fromString("Crazy#Proj ect!Name");
}

Expand Down

0 comments on commit f9dbe04

Please sign in to comment.