feat: Trigger tedge multicall on symlinks as well as with an explicit subcommand parameter#3311
feat: Trigger tedge multicall on symlinks as well as with an explicit subcommand parameter#3311didier-wenzek wants to merge 4 commits intothin-edge:mainfrom
Conversation
The following are now equivalent: - `tedge mapper c8y` - `tedge-mapper c8y` where `tedge-mapper` is a copy or a symlink of `tedge` The former `tedge` cli can be invoked as before or with the new sub-command approach: - `tedge cert show` - `tedge cli cert show` - `tedge-cli cert show` where `tedge-cli` is a copy or a symlink of `tedge` Signed-off-by: Didier Wenzek <didier.wenzek@free.fr>
Codecov ReportAttention: Patch coverage is
Additional details and impacted files📢 Thoughts on this report? Let us know! |
| #[clap(alias = "cli")] | ||
| TedgeCli(CliOpt), | ||
|
|
||
| #[clap(alias = "mapper")] |
There was a problem hiding this comment.
The following would improve tedge --help using the short names without the tedge- prefix, which is more convenient on the command line (but might misguide users as the prefix is desirable for tedge subcommand symlinks).
| #[clap(alias = "mapper")] | |
| #[clap(name = "mapper", alias = "tedge-mapper")] |
There was a problem hiding this comment.
The following would improve tedge --help using the short names without the tedge- prefix
Actually I'm seeing the exact opposite when I use the help:
root@666b6db5f6ae:/setup# tedge mapper --help
tedge-mapper is the mapper that translates thin-edge.io data model to c8y/az data model.
Usage: tedge tedge-mapper [OPTIONS] <COMMAND>
As you can see, the subcommand also has the tedge- prefix.
There was a problem hiding this comment.
Maybe my comment was not clear enough.
By applying the above suggestion (i.e. name = "mapper", alias = "tedge-mapper"), the help will be:
$ tedge mapper help
tedge-mapper is the mapper that translates thin-edge.io data model to c8y/az data model.
Usage: tedge mapper [OPTIONS] <COMMAND>
|
|
||
| let mut get_device_id_cmd = | ||
| tedge_command_with_test_home(["--config-dir", home_dir, "config", "get", "device.id"])?; | ||
| tedge_command_with_test_home(["config", "get", "device.id", "--config-dir", home_dir])?; |
There was a problem hiding this comment.
I have for now no nice solution to accept the old syntax with --config-dir set before the subcommand.
There was a problem hiding this comment.
There was a problem hiding this comment.
I'm actually confused with that doc. The following statement: "In effect, this means one should define all global arguments at the top level, however it doesn’t matter where the user uses the global argument." seems to indicate that the options marked as global can be used anywhere, which is exactly the old behaviour that we want. So, why isn't that working I wonder.
Robot Results
|
Signed-off-by: Didier Wenzek <didier.wenzek@free.fr>
albinsuresh
left a comment
There was a problem hiding this comment.
If using the global parameter isn't working as desired, I still wonder if reverting one change that removed the nested Tedge struct would restore the old behaviour.
| Tedge { | ||
| #[clap(subcommand)] | ||
| cmd: TEdgeOpt, | ||
| #[clap(alias = "cli")] | ||
| TedgeCli(CliOpt), | ||
|
|
||
| #[command(flatten)] | ||
| common: CommonArgs, | ||
| }, | ||
|
|
||
| #[clap(flatten)] | ||
| Component(Component), | ||
| } |
There was a problem hiding this comment.
Removing this additional nested level is what caused the global argument ordering issue, right? Was that necessary? Couldn't we have achieved the same by retaining this "flattened" Component struct here, with CliOpt being the new enum variant in it?
There was a problem hiding this comment.
Removing this additional nested level is what caused the global argument ordering issue, right?
No, the behavior is exactly the same with two enum levels, the inner being flatten.
However, having two levels helps when the first one is a struct:
pub struct TEdgeOptMulticall {
#[command(flatten)]
pub common: CommonArgs,
#[clap(subcommand)]
pub component: TEdgeComponent,
}
#[derive(clap::Parser, Debug)]
pub enum TEdgeComponent {
#[clap(flatten)]
Tedge(TEdgeOpt),
#[clap(alias = "mapper")]
TedgeMapper(MapperOpt),
// ..
}This requires some other minor changes (to move out the common args from TedgeOpt, MapperOpt ... to EdgeOptMulticall), but then all the following variants are accepted:
tedge mapper c8ytedge --config-dir /tmp mapper c8ytedge mapper --config-dir /tmp c8ytedge mapper c8y --config-dir /tmp
Unfortunately this not working when used à la busybox:
$ cp tedge tedge-mapper
$ tedge-mapper c8y
thread 'main' panicked at /home/didier/.cargo/registry/src/index.crates.io-6f17d22bba15001f/clap_builder-4.4.18/src/builder/debug_asserts.rs:60:9:
Command : Arguments like config_dir cannot be set on a multicall command
The error is really surprising: the config_dir argument is rejected even if not provided.
There was a problem hiding this comment.
Unfortunately this not working when used à la busybox
Indeed not supported yet: clap-rs/clap#4521
The error is really surprising: the config_dir argument is rejected even if not provided.
Even if not directly related, this comment clap-rs/clap#5525 (comment) explains why this is rejected at runtime and not compile time: "The challenge with #[command(flatten, from_global)] is that proc macros can't talk to each other at compile time. We'd have to implement this at runtime through the code we generate."
| #[clap(subcommand)] | ||
| pub cmd: TEdgeOpt, | ||
|
|
||
| #[command(flatten)] |
There was a problem hiding this comment.
I thought you were gonna mark this as global to get around the common argument ordering issue as the clap doc describes: "In effect, this means one should define all global arguments at the top level, however it doesn’t matter where the user uses the global argument"
There was a problem hiding this comment.
This is not necessary and even rejected by clap, global being set for all the arguments of the flatten struct.
| #[clap(alias = "cli")] | ||
| TedgeCli(CliOpt), | ||
|
|
||
| #[clap(alias = "mapper")] |
There was a problem hiding this comment.
The following would improve tedge --help using the short names without the tedge- prefix
Actually I'm seeing the exact opposite when I use the help:
root@666b6db5f6ae:/setup# tedge mapper --help
tedge-mapper is the mapper that translates thin-edge.io data model to c8y/az data model.
Usage: tedge tedge-mapper [OPTIONS] <COMMAND>
As you can see, the subcommand also has the tedge- prefix.
|
|
||
| let mut get_device_id_cmd = | ||
| tedge_command_with_test_home(["--config-dir", home_dir, "config", "get", "device.id"])?; | ||
| tedge_command_with_test_home(["config", "get", "device.id", "--config-dir", home_dir])?; |
There was a problem hiding this comment.
I'm actually confused with that doc. The following statement: "In effect, this means one should define all global arguments at the top level, however it doesn’t matter where the user uses the global argument." seems to indicate that the options marked as global can be used anywhere, which is exactly the old behaviour that we want. So, why isn't that working I wonder.
|
This PR is a dead end and has been superseded by #3313 |
Proposed changes
removemain.rsfor the agent and the mapper, and all the plugins that can be launched using a symlink ontedgetegde_write, which makes sense astegde_writeis used in combination withsudo.The following are now equivalent:
tedge mapper c8ytedge-mapper c8ywheretedge-mapperis a copy or a symlink oftedgetedge agenttedge-agent c8ywheretedge-agentis a copy or a symlink oftedgeThe former
tedgecli can be invoked as before or with the new sub-command approach:tedge cert showtedge cli cert showtedge-cli cert showwheretedge-cliis a copy or a symlink oftedgeTypes of changes
Paste Link to the issue
#2696
Checklist
cargo fmtas mentioned in CODING_GUIDELINEScargo clippyas mentioned in CODING_GUIDELINESFurther comments
The following are also accepted (as a side effect of using aliases for the sub-command names):
tedge tedge-cli cert showcli cert showwherecliis a copy or a symlink oftedgetedge tedge-mapper c8ymapper c8ywheremapperis a copy or a symlink oftedge