blob: 0514cc5df1c36f90991e80e32cf7364c0ca60faf [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
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053063 CLI11_PARSE(app, argc, argv);
64
65 ifstream inventoryJson(INVENTORY_JSON);
66 auto jsObject = json::parse(inventoryJson);
67
68 try
69 {
70 if (*dumpObjFlag)
71 {
72 VpdTool vpdToolObj(move(objectPath));
73 vpdToolObj.dumpObject(jsObject);
74 }
75
76 else if (*dumpInvFlag)
77 {
78 VpdTool vpdToolObj;
79 vpdToolObj.dumpInventory(jsObject);
80 }
81
PriyangaRamasamy02d4d4e2020-02-24 14:54:45 +053082 else if (*readFlag)
83 {
84 VpdTool vpdToolObj(move(objectPath), move(recordName),
85 move(keyword));
86 vpdToolObj.readKeyword();
87 }
88
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +053089 else if (*writeFlag)
90 {
91 VpdTool vpdToolObj(move(objectPath), move(recordName),
92 move(keyword), move(val));
93 rc = vpdToolObj.updateKeyword();
94 }
95
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +053096 else
97 {
98 throw runtime_error("One of the valid options is required. Refer "
99 "--help for list of options.");
100 }
101 }
102
103 catch (exception& e)
104 {
105 cerr << e.what();
106 }
107
PriyangaRamasamyd09d2ec2020-03-12 14:11:50 +0530108 return rc;
PriyangaRamasamy1f0b1e62020-02-20 20:48:25 +0530109}