Skip to content

Support single command app #755

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

Closed
jvalkeal opened this issue May 26, 2023 · 2 comments
Closed

Support single command app #755

jvalkeal opened this issue May 26, 2023 · 2 comments
Assignees
Labels
branch/3.1.x Issue for a branch for/backport For backporting type/enhancement Is an enhancement request
Milestone

Comments

@jvalkeal
Copy link
Contributor

There are times when user wants to create an app which:

  1. Don't have interactive mode
  2. Has only one command

Currently you're forced to have something like myapp mycommand --option xxx. It'd be convenient to be able to have an app which works as myapp --option xxx.

@jvalkeal jvalkeal added the type/enhancement Is an enhancement request label May 26, 2023
@jvalkeal
Copy link
Contributor Author

jvalkeal commented Jun 1, 2023

While looking into this realised that there is already a hack to do this in not so obvious way. This issue should be changed to make this easy to config.

Define a command:

@Command
public class DemoCommand {

	@Command
	public String hi(@Option String arg) {
		return String.format("hi arg='%s'", arg);
	}
}

Customise NonInteractiveShellRunner to tweak command in use:

@SpringBootApplication
@CommandScan
public class DemoApplication {

    private static final String SINGLE_QUOTE = "\'";
    private static final String DOUBLE_QUOTE = "\"";

	private static boolean isQuoted(String str) {
		if (str == null) {
			return false;
		}
		return str.startsWith(SINGLE_QUOTE) && str.endsWith(SINGLE_QUOTE)
				|| str.startsWith(DOUBLE_QUOTE) && str.endsWith(DOUBLE_QUOTE);
	}

	private Function<ApplicationArguments, List<String>> commandsFromInputArgs = args -> {
		if (args.getSourceArgs().length == 0) {
			Collections.singletonList("hi");
		}
		// re-quote if needed having whitespace
		String raw = Arrays.stream(args.getSourceArgs())
			.map(a -> {
				if (!isQuoted(a) && StringUtils.containsWhitespace(a)) {
					return "\"" + a + "\"";
				}
				return a;
			})
			.collect(Collectors.joining(" "));
		return Collections.singletonList("hi " + raw);
	};

	@Bean
	public NonInteractiveShellRunnerCustomizer runnerCustomizer() {
		return runner -> {
			runner.setCommandsFromInputArgs(commandsFromInputArgs);
		};
	}

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

Disable interactive/script runners:

spring:
  shell:
    interactive:
      enabled: false
    script:
      enabled: false
$ java -jar build/libs/demo-0.0.1-SNAPSHOT.jar --arg xxx

hi arg='xxx'

@jvalkeal jvalkeal added this to the 3.2.0-M1 milestone Jul 3, 2023
@jvalkeal jvalkeal self-assigned this Jul 3, 2023
@jvalkeal jvalkeal added for/backport For backporting branch/3.1.x Issue for a branch labels Jul 3, 2023
jvalkeal added a commit to jvalkeal/spring-shell that referenced this issue Jul 5, 2023
- NonInteractiveShellRunner can now shortcircuit into primary command
  just running it and passing args.
- Add hooks into autoconfig so that this is easy to configure.
- Relates spring-projects#755
jvalkeal added a commit to jvalkeal/spring-shell that referenced this issue Jul 5, 2023
jvalkeal added a commit to jvalkeal/spring-shell that referenced this issue Jul 5, 2023
- New spring-shell-sample-catalog which is a skeleton meant for
  future view overhaul work.
- Relates spring-projects#755
jvalkeal added a commit that referenced this issue Jul 7, 2023
- NonInteractiveShellRunner can now shortcircuit into primary command
  just running it and passing args.
- Add hooks into autoconfig so that this is easy to configure.
- Backport #755
- Relates #799
jvalkeal added a commit that referenced this issue Jul 7, 2023
- Backport #755
- Relates #799
@jvalkeal jvalkeal closed this as completed Jul 7, 2023
@jvalkeal
Copy link
Contributor Author

jvalkeal commented Jul 7, 2023

Current main branch has a skeleton sample https://github.com/spring-projects/spring-shell/tree/main/spring-shell-samples/spring-shell-sample-catalog. Branch 3.1.x don't have split sample structure so it can't be shown there but concept on main is same.

Docs for this are here https://github.com/spring-projects/spring-shell/blob/main/spring-shell-docs/src/main/asciidoc/using-shell-customization-singlecommand.adoc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
branch/3.1.x Issue for a branch for/backport For backporting type/enhancement Is an enhancement request
Projects
None yet
Development

No branches or pull requests

1 participant