|
| 1 | +// See README.txt for information and build instructions. |
| 2 | + |
| 3 | +#include <fstream> |
| 4 | +#include <iostream> |
| 5 | +#include <string> |
| 6 | + |
| 7 | +#include <google/protobuf/descriptor.h> |
| 8 | +#include <google/protobuf/util/json_util.h> |
| 9 | +#include <google/protobuf/util/type_resolver.h> |
| 10 | +#include <google/protobuf/util/type_resolver_util.h> |
| 11 | + |
| 12 | +#include "config.pb.h" |
| 13 | + |
| 14 | +using namespace std; |
| 15 | + |
| 16 | +static const char kTypeUrlPrefix[] = "type.googleapis.com"; |
| 17 | + |
| 18 | +static string GetTypeUrl(const google::protobuf::Descriptor* message) { |
| 19 | + return string(kTypeUrlPrefix) + "/" + message->full_name(); |
| 20 | +} |
| 21 | + |
| 22 | +static bool ReadMessage(string path, google::protobuf::Message *message) { |
| 23 | + string binary; |
| 24 | + ifstream input(path.c_str(), ios::in | ios::binary); |
| 25 | + if (!input) { |
| 26 | + cout << path << ": File not found." << endl; |
| 27 | + return false; |
| 28 | + } |
| 29 | + string json( (istreambuf_iterator<char>(input)), |
| 30 | + istreambuf_iterator<char>() ); |
| 31 | + google::protobuf::scoped_ptr<google::protobuf::util::TypeResolver> resolver; |
| 32 | + resolver.reset(google::protobuf::util::NewTypeResolverForDescriptorPool( |
| 33 | + kTypeUrlPrefix, |
| 34 | + google::protobuf::DescriptorPool::generated_pool())); |
| 35 | + GOOGLE_CHECK_OK(google::protobuf::util::JsonToBinaryString( |
| 36 | + resolver.get(), |
| 37 | + GetTypeUrl(message->GetDescriptor()), |
| 38 | + json, |
| 39 | + &binary)); |
| 40 | + return message->ParseFromString(binary); |
| 41 | +} |
| 42 | + |
| 43 | +static bool WriteMessage(const google::protobuf::Message& message) { |
| 44 | + string json; |
| 45 | + google::protobuf::util::JsonOptions options; |
| 46 | + google::protobuf::scoped_ptr<google::protobuf::util::TypeResolver> resolver; |
| 47 | + resolver.reset(google::protobuf::util::NewTypeResolverForDescriptorPool( |
| 48 | + kTypeUrlPrefix, |
| 49 | + google::protobuf::DescriptorPool::generated_pool())); |
| 50 | + options.add_whitespace = true; |
| 51 | + GOOGLE_CHECK_OK(google::protobuf::util::BinaryToJsonString( |
| 52 | + resolver.get(), |
| 53 | + GetTypeUrl(message.GetDescriptor()), |
| 54 | + message.SerializeAsString(), |
| 55 | + &json, |
| 56 | + options)); |
| 57 | + cout << json; |
| 58 | + return true; |
| 59 | +} |
| 60 | + |
| 61 | +// Main function: Reads the config from a file and writes it to stdout. |
| 62 | +int main(int argc, char* argv[]) { |
| 63 | + // Verify that the version of the library that we linked against is |
| 64 | + // compatible with the version of the headers we compiled against. |
| 65 | + GOOGLE_PROTOBUF_VERIFY_VERSION; |
| 66 | + |
| 67 | + oci::Spec config; |
| 68 | + |
| 69 | + if (!ReadMessage("config.json", &config)) { |
| 70 | + cerr << "config.json: Failed to load." << endl; |
| 71 | + return -1; |
| 72 | + } |
| 73 | + |
| 74 | +/* |
| 75 | + oci::LinuxUser user; |
| 76 | + user.set_uid(1); |
| 77 | + user.set_gid(1); |
| 78 | + user.add_additional_gids(5); |
| 79 | + user.add_additional_gids(6); |
| 80 | + config.mutable_process()->mutable_user()->PackFrom(user); |
| 81 | +*/ |
| 82 | + |
| 83 | + if (!WriteMessage(config)) { |
| 84 | + cerr << "config.json: Failed to write to stdout." << endl; |
| 85 | + return -1; |
| 86 | + } |
| 87 | + |
| 88 | + // Optional: Delete all global objects allocated by libprotobuf. |
| 89 | + google::protobuf::ShutdownProtobufLibrary(); |
| 90 | + |
| 91 | + return 0; |
| 92 | +} |
0 commit comments