power-utils: Add --compare option
This option is to get a latest version from a list of PSU versions.
Due to the --compare option requires a list of strings and only one
option is supported at the same time, it's easier to switch to CLI11 to
parse the arguments.
Also add --raw option that outputs the text without linefeed.
Tested: Verify both --get-version and --compare works on Witherspoon.
Signed-off-by: Lei YU <mine260309@gmail.com>
Change-Id: Idec75e3a5699eba8ba587e74824431993fe10c4c
diff --git a/tools/power-utils/version.cpp b/tools/power-utils/version.cpp
index 1acbbf2..12bb7c9 100644
--- a/tools/power-utils/version.cpp
+++ b/tools/power-utils/version.cpp
@@ -74,6 +74,21 @@
}
return std::make_tuple(*devicePath, type, versionStr);
}
+
+// A default implemention compare the string itself
+std::string getLatestDefault(const std::vector<std::string>& versions)
+{
+ std::string latest;
+ for (const auto& version : versions)
+ {
+ if (latest < version)
+ {
+ latest = version;
+ }
+ }
+ return latest;
+}
+
} // namespace utils
namespace version
@@ -100,4 +115,25 @@
return version;
}
+std::string getLatest(const std::vector<std::string>& versions)
+{
+ // TODO: when multiple PSU/Machines are supported, add configuration options
+ // to implement machine-specific logic.
+ // For now IBM AC Servers and Inspur FP5280G2 are supported.
+ //
+ // IBM AC servers' PSU version has two types:
+ // * XXXXYYYYZZZZ: XXXX is the primary version
+ // YYYY is the secondary version
+ // ZZZZ is the communication version
+ //
+ // * XXXXYYYY: XXXX is the primary version
+ // YYYY is the seconday version
+ //
+ // Inspur FP5280G2 PSU version is human readable text and a larger string
+ // means a newer version.
+ //
+ // So just compare by strings is OK for these cases
+ return utils::getLatestDefault(versions);
+}
+
} // namespace version