Skip to content
This repository was archived by the owner on Aug 4, 2025. It is now read-only.

Commit ce7c07e

Browse files
Merge pull request #54 from renanfranca/7-allow-external-configuration-file
load hidden resources configuration from external file
2 parents 80b6d01 + af89f7a commit ce7c07e

File tree

7 files changed

+236
-59
lines changed

7 files changed

+236
-59
lines changed

documentation/Commands.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This document provides an overview of the JHipster Lite CLI commands available i
1212
- [Project Creation Workflow Example](#project-creation-workflow-example)
1313
- [Options and Parameters](#options-and-parameters)
1414
- [Parameters Reuse](#parameter-reuse)
15+
- [External Configuration](#external-configuration)
1516

1617
## Getting Started
1718

@@ -141,3 +142,46 @@ jhlite apply maven-java --package-name com.example.myproject
141142
```
142143

143144
The CLI will automatically reuse the `project-name`, `base-name`, and `node-package-manager` values from your previous command.
145+
146+
## External Configuration
147+
148+
JHipster Lite CLI supports external configuration files to customize its behavior. The CLI automatically looks for a configuration file at:
149+
150+
```
151+
~/.config/jhlite-cli.yml
152+
```
153+
154+
If this file exists, it will be loaded automatically when the CLI starts.
155+
156+
### Configuration Options
157+
158+
#### Hidden Resources
159+
160+
You can hide specific modules from being displayed in the `list` command and prevent them from being applied. This is useful for customizing which modules are available in different environments.
161+
162+
Create a `~/.config/jhlite-cli.yml` file with the following structure:
163+
164+
```yaml
165+
jhlite:
166+
hidden-resources:
167+
slugs:
168+
- gradle-java
169+
- module-slug-to-hide
170+
tags:
171+
- setup
172+
- tag-to-hide
173+
```
174+
175+
**Configuration properties:**
176+
177+
- `slugs`: List of specific module slugs to hide
178+
- `tags`: List of module tags to hide (hides all modules with these tags)
179+
180+
**Effects of hidden resources:**
181+
182+
- Hidden modules will not appear in the output of `jhlite list`
183+
- Hidden modules cannot be applied using `jhlite apply <hidden-module>`
184+
- Attempting to apply a hidden module will result in an "Unmatched arguments" error
185+
186+
**Example:**
187+
If you hide the `gradle-java` module, running `jhlite list` will not show it in the available modules, and running `jhlite apply gradle-java` will fail with an error.

src/main/java/tech/jhipster/lite/cli/JHLiteCliApp.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package tech.jhipster.lite.cli;
22

3+
import java.nio.file.Files;
4+
import java.nio.file.Path;
35
import org.springframework.boot.Banner;
46
import org.springframework.boot.SpringApplication;
57
import org.springframework.boot.WebApplicationType;
@@ -13,12 +15,34 @@
1315
@ExcludeFromGeneratedCodeCoverage(reason = "Not testing logs")
1416
public class JHLiteCliApp {
1517

18+
private static final String CONFIG_FILE_NAME = "/.config/jhlite-cli.yml";
19+
private static final String SPRING_CONFIG_LOCATION_PROPERTY = "spring.config.location";
20+
private static final String SPRING_CONFIG_LOCATION_VALUE = "classpath:/config/,file:";
21+
1622
public static void main(String[] args) {
17-
ConfigurableApplicationContext context = new SpringApplicationBuilder(JHLiteCliApp.class)
23+
ConfigurableApplicationContext context = loadExternalConfigFile(createApplicationBuilder()).run(args);
24+
25+
System.exit(SpringApplication.exit(context));
26+
}
27+
28+
private static SpringApplicationBuilder createApplicationBuilder() {
29+
return new SpringApplicationBuilder(JHLiteCliApp.class)
1830
.bannerMode(Banner.Mode.OFF)
1931
.web(WebApplicationType.NONE)
20-
.lazyInitialization(true)
21-
.run(args);
22-
System.exit(SpringApplication.exit(context));
32+
.lazyInitialization(true);
33+
}
34+
35+
private static SpringApplicationBuilder loadExternalConfigFile(SpringApplicationBuilder builder) {
36+
String configPath = getConfigPath();
37+
38+
if (Files.exists(Path.of(configPath))) {
39+
builder.properties(SPRING_CONFIG_LOCATION_PROPERTY + "=" + SPRING_CONFIG_LOCATION_VALUE + configPath);
40+
}
41+
42+
return builder;
43+
}
44+
45+
private static String getConfigPath() {
46+
return System.getProperty("user.home") + CONFIG_FILE_NAME;
2347
}
2448
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package tech.jhipster.lite.cli.command.infrastructure.primary;
2+
3+
import static tech.jhipster.lite.TestProjects.newTestFolder;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.util.List;
9+
import picocli.CommandLine;
10+
import tech.jhipster.lite.module.application.JHipsterModulesApplicationService;
11+
import tech.jhipster.lite.module.infrastructure.secondary.git.GitTestUtil;
12+
import tech.jhipster.lite.project.application.ProjectsApplicationService;
13+
14+
class CliFixture {
15+
16+
static Path setupProjectTestFolder() throws IOException {
17+
String projectFolder = newTestFolder();
18+
Path projectPath = Path.of(projectFolder);
19+
Files.createDirectories(projectPath);
20+
loadGitConfig(projectPath);
21+
22+
return projectPath;
23+
}
24+
25+
static void loadGitConfig(Path project) {
26+
GitTestUtil.execute(project, "init");
27+
GitTestUtil.execute(project, "config", "init.defaultBranch", "main");
28+
GitTestUtil.execute(project, "config", "user.email", "\"[email protected]\"");
29+
GitTestUtil.execute(project, "config", "user.name", "\"Test\"");
30+
}
31+
32+
static CommandLine commandLine(JHipsterModulesApplicationService modules, ProjectsApplicationService projects) {
33+
ListModulesCommand listModulesCommand = new ListModulesCommand(modules);
34+
ApplyModuleSubCommandsFactory subCommandsFactory = new ApplyModuleSubCommandsFactory(modules, projects);
35+
ApplyModuleCommand applyModuleCommand = new ApplyModuleCommand(modules, subCommandsFactory);
36+
37+
JHLiteCommandsFactory jhliteCommandsFactory = new JHLiteCommandsFactory(List.of(listModulesCommand, applyModuleCommand), "1", "2");
38+
39+
return new CommandLine(jhliteCommandsFactory.buildCommandSpec());
40+
}
41+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package tech.jhipster.lite.cli.command.infrastructure.primary;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static tech.jhipster.lite.cli.command.infrastructure.primary.CliFixture.commandLine;
5+
import static tech.jhipster.lite.cli.command.infrastructure.primary.CliFixture.setupProjectTestFolder;
6+
7+
import java.io.IOException;
8+
import java.nio.file.Path;
9+
import org.junit.jupiter.api.Test;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.core.env.Environment;
12+
import org.springframework.test.context.TestPropertySource;
13+
import tech.jhipster.lite.cli.IntegrationTest;
14+
import tech.jhipster.lite.module.application.JHipsterModulesApplicationService;
15+
import tech.jhipster.lite.project.application.ProjectsApplicationService;
16+
17+
@IntegrationTest
18+
@TestPropertySource(properties = { "spring.config.location=classpath:/cli/config/jhlite-cli-hidden-resources.yml" })
19+
class ExternalConfigTest {
20+
21+
@Autowired
22+
private ProjectsApplicationService projects;
23+
24+
@Autowired
25+
private JHipsterModulesApplicationService modules;
26+
27+
@Autowired
28+
private Environment environment;
29+
30+
@Test
31+
void shouldLoadExternalConfiguration() {
32+
assertThat(environment.getProperty("jhlite.hidden-resources.slugs[0]")).isEqualTo("gradle-java");
33+
}
34+
35+
@Test
36+
void shouldHideModuleWhenLoadExternalConfigurationFile() {
37+
String[] args = { "list" };
38+
39+
try (SystemOutputCaptor outputCaptor = new SystemOutputCaptor()) {
40+
int exitCode = commandLine(modules, projects).execute(args);
41+
42+
assertThat(exitCode).isZero();
43+
assertThat(outputCaptor.getOutput()).doesNotContain("gradle-java");
44+
}
45+
}
46+
47+
@Test
48+
void shouldHideApplyCommandWhenLoadExternalConfigurationFile() throws IOException {
49+
Path projectPath = setupProjectTestFolder();
50+
String[] args = { "apply", "gitpod", "--project-path", projectPath.toString() };
51+
52+
try (SystemOutputCaptor outputCaptor = new SystemOutputCaptor()) {
53+
int exitCode = commandLine(modules, projects).execute(args);
54+
55+
assertThat(exitCode).isEqualTo(2);
56+
assertThat(outputCaptor.getOutput()).contains("Unmatched arguments from index");
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)