jbangdev / jbang

Unleash the power of Java - JBang Lets Students, Educators and Professional Developers create, edit and run self-contained source-only Java programs with unprecedented ease.
https://jbang.dev
MIT License
1.43k stars 159 forks source link

Templates does not show template's catalog name #1309

Open nandorholozsnyak opened 2 years ago

nandorholozsnyak commented 2 years ago

Describe the bug Template catalog names when templates are being listed only being resolved from the implicit catalog, that only gets populated if (of course there are other possible methods, I recognized one) the jbang init with a template references a repository that has not been used earlier.

To Reproduce Here are a few steps to reproduce it: First step - add this catalog https://github.com/nandorholozsnyak/jbang-cloud/blob/main/jbang-catalog.json $ jbang catalog add --name cloud https://github.com/nandorholozsnyak/jbang-cloud/blob/main/jbang-catalog.json [jbang] Catalog 'cloud' added to '/home/nandorholozsnyak/.jbang/jbang-catalog.json' Second step - List the templates and validate that the templates from the earlier mentioned catalog are not being marked as coming from the cloud catalog as it was added.

$ jbang template list
agent = Agent template
cli = CLI template
githubbot = Example of making a github app
hello = Basic Hello World template
hello.groovy = Basic groovy Hello World template
hello.kt = Basic kotlin Hello World template
q-aws-lambda = Quarkus AWS Lambda template
q-aws-lambda-funq = Quarkus AWS Lambda template using Funq
q-aws-lambda-funq-tf = Quarkus AWS Lambda template using Funq. Use the -Dnative-function flag to have native image based Terraform resources
q-aws-lambda-s3 = Quarkus AWS Lambda template with incoming S3 event
q-aws-lambda-s3-funq = Quarkus AWS Lambda template with incoming S3 event using Funq
q-aws-lambda-sqs = Quarkus AWS Lambda template with incoming SQS event
q-aws-lambda-sqs-funq = Quarkus AWS Lambda template with incoming SQS event using Funq
q-aws-lambda-tf = Quarkus AWS Lambda template with Terraform template. Use the -Dnative-function flag to have native image based Terraform resources
qcli = Quarkus CLI template
qmetrics = Quarkus Metrics template
qrest = Quarkus REST template
readme.md = Basic markdown readme template

For example q-aws-lambda should be q-aws-lambda@cloud. If now we would try the jbang init -t=q-aws-lambda Lambda.java it will not work, but with jbang init -t=q-aws-lambda@cloud Lambda.java it would as it was mentioned.

Third step (if we want to FIX the naming but it would not name it as we want) - at this time the implicit-catalog.json is still empty, and the catalog name for templates are coming from this file, with a trick.

As a third step lets try to init with the following key q-aws-lambda@nandorholozsnyak/jbang-cloud.

$ jbang init -t=q-aws-lambda@nandorholozsnyak/jbang-cloud Lambda.java
[jbang] File initialized. You can now run it with 'jbang Lambda.java' or edit it using 'jbang edit --open=[editor] Lambda.java' where [editor] is your editor or IDE, e.g. 'eclipse'

In this case the content of the implicit-catalog.json is the following

{
  "catalogs": {
    "nandorholozsnyak/jbang-cloud": {
      "catalog-ref": "https://github.com/nandorholozsnyak/jbang-cloud/blob/HEAD/jbang-catalog.json"
    }
  },
  "aliases": {},
  "templates": {}
}

With that we would say, cool the catalog's name should be resolved when the templates are being listed, but not. In my local jbang-catalog.json the URL for the catalog is the following: https://github.com/nandorholozsnyak/jbang-cloud/blob/main/jbang-catalog.json but in the implicit-catalog.json we have this: https://github.com/nandorholozsnyak/jbang-cloud/blob/HEAD/jbang-catalog.json

The difference is the main <-> HEAD.

$ jbang template list 
agent = Agent template
cli = CLI template
githubbot = Example of making a github app
hello = Basic Hello World template
hello.groovy = Basic groovy Hello World template
hello.kt = Basic kotlin Hello World template
q-aws-lambda = Quarkus AWS Lambda template
q-aws-lambda-funq = Quarkus AWS Lambda template using Funq
q-aws-lambda-funq-tf = Quarkus AWS Lambda template using Funq. Use the -Dnative-function flag to have native image based Terraform resources
q-aws-lambda-s3 = Quarkus AWS Lambda template with incoming S3 event
q-aws-lambda-s3-funq = Quarkus AWS Lambda template with incoming S3 event using Funq
q-aws-lambda-sqs = Quarkus AWS Lambda template with incoming SQS event
q-aws-lambda-sqs-funq = Quarkus AWS Lambda template with incoming SQS event using Funq
q-aws-lambda-tf = Quarkus AWS Lambda template with Terraform template. Use the -Dnative-function flag to have native image based Terraform resources
qcli = Quarkus CLI template
qmetrics = Quarkus Metrics template
qrest = Quarkus REST template
readme.md = Basic markdown readme template

For fixing it we either have to change the catalog's ref in the jbang-catalog.json or we have to make a new init with the following key: -t=q-aws-lambda@nandorholozsnyak/jbang-cloud/main

❯ jbang init -t=q-aws-lambda@nandorholozsnyak/jbang-cloud/main Lambda.java
[jbang] File initialized. You can now run it with 'jbang Lambda.java' or edit it using 'jbang edit --open=[editor] Lambda.java' where [editor] is your editor or IDE, e.g. 'eclipse'

The content of the implicit-catalog.json is the following:

{
  "catalogs": {
    "nandorholozsnyak/jbang-cloud": {
      "catalog-ref": "https://github.com/nandorholozsnyak/jbang-cloud/blob/HEAD/jbang-catalog.json"
    },
    "nandorholozsnyak/jbang-cloud/main": {
      "catalog-ref": "https://github.com/nandorholozsnyak/jbang-cloud/blob/main/jbang-catalog.json"
    }
  },
  "aliases": {},
  "templates": {}
}

And with the jbang template list the output is the following

❯ jbang template list                                                     
agent = Agent template
cli = CLI template
githubbot@nandorholozsnyak/jbang-cloud/main = Example of making a github app
hello = Basic Hello World template
hello.groovy = Basic groovy Hello World template
hello.kt = Basic kotlin Hello World template
q-aws-lambda@nandorholozsnyak/jbang-cloud/main = Quarkus AWS Lambda template
q-aws-lambda-funq@nandorholozsnyak/jbang-cloud/main = Quarkus AWS Lambda template using Funq
q-aws-lambda-funq-tf@nandorholozsnyak/jbang-cloud/main = Quarkus AWS Lambda template using Funq. Use the -Dnative-function flag to have native image based Terraform resources
q-aws-lambda-s3@nandorholozsnyak/jbang-cloud/main = Quarkus AWS Lambda template with incoming S3 event
q-aws-lambda-s3-funq@nandorholozsnyak/jbang-cloud/main = Quarkus AWS Lambda template with incoming S3 event using Funq
q-aws-lambda-sqs@nandorholozsnyak/jbang-cloud/main = Quarkus AWS Lambda template with incoming SQS event
q-aws-lambda-sqs-funq@nandorholozsnyak/jbang-cloud/main = Quarkus AWS Lambda template with incoming SQS event using Funq
q-aws-lambda-tf@nandorholozsnyak/jbang-cloud/main = Quarkus AWS Lambda template with Terraform template. Use the -Dnative-function flag to have native image based Terraform resources
qcli = Quarkus CLI template
qmetrics = Quarkus Metrics template
qrest = Quarkus REST template
readme.md = Basic markdown readme template

What I think would be good to see is the following

❯ jbang template list                                                     
agent = Agent template
cli = CLI template
githubbot@cloud = Example of making a github app
hello = Basic Hello World template
hello.groovy = Basic groovy Hello World template
hello.kt = Basic kotlin Hello World template
q-aws-lambda@cloud = Quarkus AWS Lambda template
q-aws-lambda-funq@cloud = Quarkus AWS Lambda template using Funq
q-aws-lambda-funq-tf@cloud = Quarkus AWS Lambda template using Funq. Use the -Dnative-function flag to have native image based Terraform resources
q-aws-lambda-s3@cloud = Quarkus AWS Lambda template with incoming S3 event
q-aws-lambda-s3-funq@cloud = Quarkus AWS Lambda template with incoming S3 event using Funq
q-aws-lambda-sqs@cloud = Quarkus AWS Lambda template with incoming SQS event
q-aws-lambda-sqs-funq@cloud = Quarkus AWS Lambda template with incoming SQS event using Funq
q-aws-lambda-tf@cloud = Quarkus AWS Lambda template with Terraform template. Use the -Dnative-function flag to have native image based Terraform resources
qcli = Quarkus CLI template
qmetrics = Quarkus Metrics template
qrest = Quarkus REST template
readme.md = Basic markdown readme template

Because the following commands works like charm if the jbang-catalog.json has the catalog entry cloud:

❯ jbang init -t=q-aws-lambda@cloud Lambda.java
[jbang] File initialized. You can now run it with 'jbang Lambda.java' or edit it using 'jbang edit --open=[editor] Lambda.java' where [editor] is your editor or IDE, e.g. 'eclipse'

Expected behavior Show catalog's name after the template as shown above.

JBang version 0.90.1

quintesse commented 2 years ago

If I remember correctly, the desired behaviour is that locally defined catalogs are searched without needing to be qualified (ie no @name necessary).

Meaning that you can do jbang catalog add --name foo http://url/to/some/catalog and then either use jbang analias@foo or simply jbang analias to run any alias in that catalog.

The same is not true for "implicit" catalogs, the ones that get added automatically when you run something like jbang hello@jbangdev. (You can still get the same behaviour if you manually add that catalog using jbang catalog add).

But it seems that behaviour is missing, as you've shown jbang analias doesn't work anymore. So it's not jbang template/alias list that's wrong it's run and init. So we have a regression.

@maxandersen can you confirm that this was indeed the desired behaviour?

nandorholozsnyak commented 2 years ago

If that is the desired workflow than okay, I thought the catalog's name should be appended behind it in the template list. I saw the tests you written, were they working?

maxandersen commented 2 years ago

Yeah so catalog "linked" in was meant to be available "via" it's hosted catalog.

nandorholozsnyak commented 2 years ago

I do not really understand it.

But, how would jbang handle the name collisions if two different catalogs are having the same template name? I

maxandersen commented 2 years ago

Order matters. First one wins.

maxandersen commented 2 years ago

Remember you can always use the explicit form.

nandorholozsnyak commented 2 years ago

Remember you can always use the explicit form.

What do you mean by using explicit form? <template-name>@<catalog-name> or <template-name>@<user>/<repo>

To be honest if I would add one catalog to my local jbang-catalog, I would expect that it would give enough information for me about how to use a specific template (name@catalog-name), and for example if you have a tons of catalogs in with a tons of templates then it will be just a mess. I would say it is okay, right now my use cases and its templates are known by me, but for other newcomers they may not use it as easy we do.

maxandersen commented 2 years ago

Either works. Both are explicit.

The implicit one is which will be searched first locally and then in any linked catalogs.

quintesse commented 2 years ago

I'd go with the terms "qualified" vs "unqualified", where "qualified" means anything with a "@" in it, while "unqualified" means something that a simple name, like "foo", "myscript", "test123", etc.

nandorholozsnyak commented 2 years ago

Okay cool, but as I said, I would be happy to see the "qualified" names in the template list output if a given templates is coming from an external catalog :D But if the use cases, expectations are not that in case of this feature, then I understand it and we can close it.

quintesse commented 2 years ago

I think we can at least do better than we do now in giving the user a good idea of what aliases are available and how to run them. I'm working on a PR for this so definitely don't close this 🙂