Skip to content
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

Smaller fixes to support upcoming commits #1664

Merged
merged 3 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 42 additions & 34 deletions src/client_shared/conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,38 @@ enum class ArgumentsMode {
StopAtBareArguments,
};

struct CliArgument {
string name;
bool mandatory;
};

struct CliOption {
string long_option;
string short_option;
string description;
string default_value;
string parameter;
};

struct CliCommand {
string name;
string description;
optional<CliArgument> argument;
vector<CliOption> options;
};

struct CliApp {
string name;
string short_description;
string long_description;
vector<CliCommand> commands;
};

const OptsSet GlobalOptsSetWithValue();
const OptsSet GlobalOptsSetWithoutValue();
const OptsSet CommandOptsSetWithValue(const vector<CliOption> &options);
const OptsSet CommandOptsSetWithoutValue(const vector<CliOption> &options);

class CmdlineOptionsIterator {
public:
CmdlineOptionsIterator(
Expand All @@ -79,7 +111,16 @@ class CmdlineOptionsIterator {
start_ {start},
end_ {end},
opts_with_value_ {opts_with_value},
opts_wo_value_ {opts_without_value} {};
opts_wo_value_ {opts_without_value} {
}
CmdlineOptionsIterator(
vector<string>::const_iterator start,
vector<string>::const_iterator end,
const vector<CliOption> &opts) :
CmdlineOptionsIterator(
start, end, CommandOptsSetWithValue(opts), CommandOptsSetWithoutValue(opts)) {
}

ExpectedOptionValue Next();

size_t GetPos() const {
Expand Down Expand Up @@ -244,45 +285,12 @@ class Paths {
}
};

struct CliArgument {
string name;
bool mandatory;
};

struct CliOption {
string long_option;
string short_option;
string description;
string default_value;
string parameter;
};

struct CliCommand {
string name;
string description;
optional<CliArgument> argument;
vector<CliOption> options_w_values;
vector<CliOption> options;
};

struct CliApp {
string name;
string short_description;
string long_description;
vector<CliCommand> commands;
};

bool FindCmdlineHelpArg(vector<string>::const_iterator start, vector<string>::const_iterator end);

void PrintCliHelp(const CliApp &cli, ostream &stream = std::cout);
void PrintCliCommandHelp(
const CliApp &cli, const string &command_name, ostream &stream = std::cout);

const OptsSet GlobalOptsSetWithValue();
const OptsSet GlobalOptsSetWithoutValue();
const OptsSet CommandOptsSetWithValue(const vector<CliOption> &options);
const OptsSet CommandOptsSetWithoutValue(const vector<CliOption> &options);

class MenderConfig : public cfg_parser::MenderConfigFromFile {
public:
Paths paths {};
Expand Down
6 changes: 1 addition & 5 deletions src/mender-auth/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ static ExpectedActionPtr ParseAuthArguments(
string passphrase = "";
bool forcebootstrap = false;
if (start[0] == "bootstrap" || start[0] == "daemon") {
conf::CmdlineOptionsIterator opts_iter(
start + 1,
end,
conf::CommandOptsSetWithValue(opts_bootstrap_daemon),
conf::CommandOptsSetWithoutValue(opts_bootstrap_daemon));
conf::CmdlineOptionsIterator opts_iter(start + 1, end, opts_bootstrap_daemon);
auto ex_opt_val = opts_iter.Next();

while (ex_opt_val
Expand Down
60 changes: 21 additions & 39 deletions src/mender-update/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,19 @@ const conf::CliCommand cmd_check_update {
};

const conf::CliOption opt_stop_after {
conf::CliOption {
.long_option = "stop-after",
.description =
"Stop after the given state has completed. "
"Choices are `Download`, `ArtifactInstall` and `ArtifactCommit`. "
"You can later resume the installation by using the `resume` command. "
"Note that the client always stops after `ArtifactInstall` if the update module supports rollback.",
.parameter = "STATE",
},
.long_option = "stop-after",
.description =
"Stop after the given state has completed. "
"Choices are `Download`, `ArtifactInstall` and `ArtifactCommit`. "
"You can later resume the installation by using the `resume` command. "
"Note that the client always stops after `ArtifactInstall` if the update module supports rollback.",
.parameter = "STATE",
};

const conf::CliCommand cmd_commit {
.name = "commit",
.description = "Commit current Artifact. Returns (2) if no update in progress",
.options_w_values =
.options =
{
opt_stop_after,
},
Expand All @@ -81,41 +79,35 @@ const conf::CliCommand cmd_install {
.name = "artifact",
.mandatory = true,
},
.options_w_values =
{
opt_stop_after,
},
.options =
{
conf::CliOption {
.long_option = "reboot-exit-code",
.description =
"Return exit code 4 if a manual reboot is required after the Artifact installation.",
},
opt_stop_after,
},
};

const conf::CliCommand cmd_resume {
.name = "install",
.name = "resume",
.description = "Resume an interrupted installation",
.options_w_values =
{
opt_stop_after,
},
.options =
{
conf::CliOption {
.long_option = "reboot-exit-code",
.description =
"Return exit code 4 if a manual reboot is required after the Artifact installation.",
},
opt_stop_after,
},
};

const conf::CliCommand cmd_rollback {
.name = "rollback",
.description = "Rollback current Artifact. Returns (2) if no update in progress",
.options_w_values =
.options =
{
opt_stop_after,
},
Expand Down Expand Up @@ -216,27 +208,23 @@ ExpectedActionPtr ParseUpdateArguments(
}

if (start[0] == "show-artifact") {
conf::CmdlineOptionsIterator iter(start + 1, end, {}, {});
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_show_artifact.options);
auto arg = iter.Next();
if (!arg) {
return expected::unexpected(arg.error());
}

return make_shared<ShowArtifactAction>();
} else if (start[0] == "show-provides") {
conf::CmdlineOptionsIterator iter(start + 1, end, {}, {});
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_show_provides.options);
auto arg = iter.Next();
if (!arg) {
return expected::unexpected(arg.error());
}

return make_shared<ShowProvidesAction>();
} else if (start[0] == "install") {
conf::CmdlineOptionsIterator iter(
start + 1,
end,
conf::CommandOptsSetWithValue(cmd_install.options_w_values),
conf::CommandOptsSetWithoutValue(cmd_install.options));
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_install.options);
iter.SetArgumentsMode(conf::ArgumentsMode::AcceptBareArguments);

string filename;
Expand All @@ -252,11 +240,7 @@ ExpectedActionPtr ParseUpdateArguments(
install_action->SetStopAfter(std::move(stop_after));
return install_action;
} else if (start[0] == "resume") {
conf::CmdlineOptionsIterator iter(
start + 1,
end,
conf::CommandOptsSetWithValue(cmd_resume.options_w_values),
conf::CommandOptsSetWithoutValue(cmd_resume.options));
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_resume.options);

bool reboot_exit_code = false;
vector<string> stop_after;
Expand All @@ -270,8 +254,7 @@ ExpectedActionPtr ParseUpdateArguments(
resume_action->SetStopAfter(std::move(stop_after));
return resume_action;
} else if (start[0] == "commit") {
conf::CmdlineOptionsIterator iter(
start + 1, end, conf::CommandOptsSetWithValue(cmd_commit.options_w_values), {});
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_commit.options);

vector<string> stop_after;
auto err = CommonInstallFlagsHandler(iter, nullptr, nullptr, &stop_after);
Expand All @@ -283,8 +266,7 @@ ExpectedActionPtr ParseUpdateArguments(
commit_action->SetStopAfter(std::move(stop_after));
return commit_action;
} else if (start[0] == "rollback") {
conf::CmdlineOptionsIterator iter(
start + 1, end, conf::CommandOptsSetWithValue(cmd_rollback.options_w_values), {});
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_rollback.options);

vector<string> stop_after;
auto err = CommonInstallFlagsHandler(iter, nullptr, nullptr, &stop_after);
Expand All @@ -296,23 +278,23 @@ ExpectedActionPtr ParseUpdateArguments(
rollback_action->SetStopAfter(std::move(stop_after));
return rollback_action;
} else if (start[0] == "daemon") {
conf::CmdlineOptionsIterator iter(start + 1, end, {}, {});
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_daemon.options);
auto arg = iter.Next();
if (!arg) {
return expected::unexpected(arg.error());
}

return make_shared<DaemonAction>();
} else if (start[0] == "send-inventory") {
conf::CmdlineOptionsIterator iter(start + 1, end, {}, {});
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_send_inventory.options);
auto arg = iter.Next();
if (!arg) {
return expected::unexpected(arg.error());
}

return make_shared<SendInventoryAction>();
} else if (start[0] == "check-update") {
conf::CmdlineOptionsIterator iter(start + 1, end, {}, {});
conf::CmdlineOptionsIterator iter(start + 1, end, cmd_check_update.options);
auto arg = iter.Next();
if (!arg) {
return expected::unexpected(arg.error());
Expand Down