blob: b23706effe238bfacd06aec8d437a9f96291659b [file] [log] [blame]
Yuxiao Zhang1e760602024-03-07 12:35:24 -08001#include "file-io.hpp"
2
3#include <stdplus/print.hpp>
4
5#include <cstring>
6#include <iostream>
7
8static void printUsage()
9{
10 stdplus::println(stderr, "Usage: update_dhcp_status <state> <message>");
11 stdplus::println(stderr,
12 "<state> is one of 'DONE', 'POWERCYCLE' or 'ONGOING'");
13}
14
15static int genStatusCode(char* state)
16{
17 if (std::strcmp(state, "DONE") == 0)
18 {
19 return 0;
20 }
21 else if (std::strcmp(state, "POWERCYCLE") == 0)
22 {
23 return 1;
24 }
25 else if (std::strcmp(state, "ONGOING") == 0)
26 {
27 return 2;
28 }
29
30 return -1;
31}
32
33int main(int argc, char* argv[])
34{
35 if (argc != 3)
36 {
37 printUsage();
38 return 1;
39 }
40
41 int statusCode = genStatusCode(argv[1]);
42
43 if (statusCode == -1)
44 {
45 printUsage();
46 return 1;
47 }
48
49 try
50 {
51 std::string status;
52 status.push_back(statusCode);
53 status.append(argv[2]);
54 fileWrite(statusFile, status);
55 }
56 catch (const std::exception& e)
57 {
58 stdplus::println(stderr, "Failed to update status file {}", e.what());
59 return 1;
60 }
61
62 return 0;
63}