blob: 9129eca231599a6c78db3557e2279f7ecb28fbba [file] [log] [blame]
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +05301#include "vpd_tool_impl.hpp"
2
3#include <CLI/CLI.hpp>
4#include <fstream>
5#include <iostream>
6
7using namespace CLI;
8using namespace std;
9
10int main(int argc, char** argv)
11{
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053012 int rc = 0;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053013 App app{"VPD Command line tool to dump the inventory and to read and "
14 "update the keywords"};
15
16 string objectPath{};
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053017 string recordName{};
18 string keyword{};
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053019 string val{};
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053020
21 auto object =
22 app.add_option("--object, -O", objectPath, "Enter the Object Path");
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053023 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");
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053026 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");
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053030
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
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053041 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
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053051 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
PriyangaRamasamy0407b172020-03-31 13:57:18 +053063 auto forceResetFlag = app.add_flag(
64 "--forceReset, -f, -F", "Force Collect for Hardware. { vpd-tool-exe "
65 "--forceReset/-f/-F }");
66
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053067 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
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053086 else if (*readFlag)
87 {
88 VpdTool vpdToolObj(move(objectPath), move(recordName),
89 move(keyword));
90 vpdToolObj.readKeyword();
91 }
92
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053093 else if (*writeFlag)
94 {
95 VpdTool vpdToolObj(move(objectPath), move(recordName),
96 move(keyword), move(val));
97 rc = vpdToolObj.updateKeyword();
98 }
99
PriyangaRamasamy0407b172020-03-31 13:57:18 +0530100 else if (*forceResetFlag)
101 {
102 VpdTool vpdToolObj;
103 vpdToolObj.forceReset(jsObject);
104 }
105
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530106 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
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530118 return rc;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530119}