PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 1 | #include "vpd_tool_impl.hpp" |
| 2 | |
| 3 | #include <CLI/CLI.hpp> |
| 4 | #include <fstream> |
| 5 | #include <iostream> |
| 6 | |
| 7 | using namespace CLI; |
| 8 | using namespace std; |
| 9 | |
| 10 | int main(int argc, char** argv) |
| 11 | { |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 12 | int rc = 0; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 13 | App app{"VPD Command line tool to dump the inventory and to read and " |
| 14 | "update the keywords"}; |
| 15 | |
| 16 | string objectPath{}; |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 17 | string recordName{}; |
| 18 | string keyword{}; |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 19 | string val{}; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 20 | |
| 21 | auto object = |
| 22 | app.add_option("--object, -O", objectPath, "Enter the Object Path"); |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 23 | auto record = |
| 24 | app.add_option("--record, -R", recordName, "Enter the Record Name"); |
| 25 | auto kw = app.add_option("--keyword, -K", keyword, "Enter the Keyword"); |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 26 | auto valOption = app.add_option( |
| 27 | "--value, -V", val, |
| 28 | "Enter the value. The value to be updated should be either in ascii or " |
| 29 | "in hex. ascii eg: 01234; hex eg: 0x30313233"); |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 30 | |
| 31 | auto dumpObjFlag = |
| 32 | app.add_flag("--dumpObject, -o", |
| 33 | "Dump the given object from the inventory. { " |
| 34 | "vpd-tool-exe --dumpObject/-o --object/-O object-name }") |
| 35 | ->needs(object); |
| 36 | |
| 37 | auto dumpInvFlag = app.add_flag( |
| 38 | "--dumpInventory, -i", "Dump all the inventory objects. { vpd-tool-exe " |
| 39 | "--dumpInventory/-i }"); |
| 40 | |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 41 | auto readFlag = |
| 42 | app.add_flag("--readKeyword, -r", |
| 43 | "Read the data of the given keyword. { " |
| 44 | "vpd-tool-exe --readKeyword/-r --object/-O " |
| 45 | "\"object-name\" --record/-R \"record-name\" --keyword/-K " |
| 46 | "\"keyword-name\" }") |
| 47 | ->needs(object) |
| 48 | ->needs(record) |
| 49 | ->needs(kw); |
| 50 | |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 51 | auto writeFlag = |
| 52 | app.add_flag( |
| 53 | "--writeKeyword, -w, --updateKeyword, -u", |
| 54 | "Update the value. { vpd-tool-exe " |
| 55 | "--writeKeyword/-w/--updateKeyword/-u " |
| 56 | "--object/-O object-name --record/-R record-name --keyword/-K " |
| 57 | "keyword-name --value/-V value-to-be-updated }") |
| 58 | ->needs(object) |
| 59 | ->needs(record) |
| 60 | ->needs(kw) |
| 61 | ->needs(valOption); |
| 62 | |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 63 | auto forceResetFlag = app.add_flag( |
| 64 | "--forceReset, -f, -F", "Force Collect for Hardware. { vpd-tool-exe " |
| 65 | "--forceReset/-f/-F }"); |
| 66 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 67 | CLI11_PARSE(app, argc, argv); |
| 68 | |
| 69 | ifstream inventoryJson(INVENTORY_JSON); |
| 70 | auto jsObject = json::parse(inventoryJson); |
| 71 | |
| 72 | try |
| 73 | { |
| 74 | if (*dumpObjFlag) |
| 75 | { |
| 76 | VpdTool vpdToolObj(move(objectPath)); |
| 77 | vpdToolObj.dumpObject(jsObject); |
| 78 | } |
| 79 | |
| 80 | else if (*dumpInvFlag) |
| 81 | { |
| 82 | VpdTool vpdToolObj; |
| 83 | vpdToolObj.dumpInventory(jsObject); |
| 84 | } |
| 85 | |
PriyangaRamasamy | 02d4d4e | 2020-02-24 14:54:45 +0530 | [diff] [blame] | 86 | else if (*readFlag) |
| 87 | { |
| 88 | VpdTool vpdToolObj(move(objectPath), move(recordName), |
| 89 | move(keyword)); |
| 90 | vpdToolObj.readKeyword(); |
| 91 | } |
| 92 | |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 93 | else if (*writeFlag) |
| 94 | { |
| 95 | VpdTool vpdToolObj(move(objectPath), move(recordName), |
| 96 | move(keyword), move(val)); |
| 97 | rc = vpdToolObj.updateKeyword(); |
| 98 | } |
| 99 | |
PriyangaRamasamy | 0407b17 | 2020-03-31 13:57:18 +0530 | [diff] [blame] | 100 | else if (*forceResetFlag) |
| 101 | { |
| 102 | VpdTool vpdToolObj; |
| 103 | vpdToolObj.forceReset(jsObject); |
| 104 | } |
| 105 | |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 106 | else |
| 107 | { |
| 108 | throw runtime_error("One of the valid options is required. Refer " |
| 109 | "--help for list of options."); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | catch (exception& e) |
| 114 | { |
| 115 | cerr << e.what(); |
| 116 | } |
| 117 | |
PriyangaRamasamy | d09d2ec | 2020-03-12 14:11:50 +0530 | [diff] [blame] | 118 | return rc; |
PriyangaRamasamy | 1f0b1e6 | 2020-02-20 20:48:25 +0530 | [diff] [blame] | 119 | } |