kakawait / picocli-spring-boot-starter

Spring boot starter for Picocli command line parser that will simplify your CommandLineRunner
MIT License
51 stars 6 forks source link

Add support for Spring Boot 2.0 #4

Open bobtiernay-okta opened 6 years ago

bobtiernay-okta commented 6 years ago

This would be great for folks on the newest versions of Spring Boot.

kakawait commented 6 years ago

I didn't test yet. Could you please share with me any issues you get with Spring boot 2

rome-legacy commented 5 years ago

Hi Thibaud, thx for your work. i wrote you an email regarding this problem and then i saw this open issue. so maybe i have the same problem because i'm using spring-boot 2.0.6.

i have a strange behaviour where i try to inject a @service annotated class into (sub) command. the service is ok in my @PostConstruct function but when call() is executed its null.

@Component
@Command(name = "module", description="create module")
public class CmdModule extends HelpAwarePicocliCommand {
    private static final Logger logger = Logger.getLogger(CmdModule.class.getName());

    @Autowired
    private FileSystemService fss;
    private FileSystem fileSystem;

    @Option(names={ "-i", "--init" }, description="init new module in current directory")
    private boolean initModule;

    @Option(names={ "-d", "--disable" }, description="disable module and all commands")
    private boolean disableModule;

    @Option(names={ "-e", "--enable" }, description="enable module and all commands")
    private boolean enableModule;

    @Parameters(index="0", paramLabel="<module-name>", description="human readable module name")
    private String moduleName;

    @PostConstruct
    private void postConstruct() {
        requireNonNull(fss, "filesystem service not injected");
        fileSystem = fss.getFileSystem();
    }

    /* (non-Javadoc)
     * @see java.util.concurrent.Callable#call()
     */
    @Override
    public ExitStatus call() throws Exception {
        logger.info("user.dir: " + System.getProperty("user.dir")); // does it!
//      Path root = fileSystem.getPath("/");
//      DirectoryStream<Path> subdirs = Files.newDirectoryStream(root);
        fileSystem.getRootDirectories().forEach(dir -> logger.info("subdir: " + dir.toAbsolutePath().toString()));

        if (!(initModule ^ disableModule ^ enableModule) ^ (initModule && disableModule && enableModule)) {
//          throw new Exception (String.format("only one option of [%s, %s, %s] is possible", "-i", "-d", "-e"));
            throw new Exception ("only one option is possible");
        }

        if (initModule) {
            initModule(moduleName);
        }

        if (disableModule) {
            disableModule(moduleName);
        }

        if (enableModule) {
            enableModule(moduleName);
        }

        return ExitStatus.TERMINATION;
    }

    /**
     * @param name
     */
    private void enableModule(String name) {
        System.out.println(String.format("enable module «%s»", name));
    }

    /**
     * @param name
     */
    private void disableModule(String name) {
        System.out.println(String.format("disable module «%s»", name));
    }

    private void initModule(String name) {
        System.out.println(String.format("init new module «%s»", name));
        Path path = Paths.get("");
        String s = path.toAbsolutePath().toString();
//      System.out.println("current path = " + s);
    }
}

in postconstruct fss is not null and the fileSystem can be retrieved, but in fileSystem.getRootDirectories() fileSystem is null and so is fss.

any ideas? regards

kakawait commented 5 years ago

@rome-legacy Ok I think I understand your issue, I'll checkout asap

rome-legacy commented 5 years ago

Hi @kakawait, i've tried to downgrade the libs to spring boot 1 and have the same problem. am i doing something wrong?

kakawait commented 5 years ago

@rome-legacy will be fixed on next version 1.0.0-beta-1