xref: /aosp_15_r20/external/sandboxed-api/contrib/jsonnet/examples/jsonnet_yaml_stream_example.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <cstdlib>
16 #include <iostream>
17 
18 #include "absl/flags/parse.h"
19 #include "absl/log/check.h"
20 #include "absl/log/globals.h"
21 #include "absl/log/initialize.h"
22 #include "contrib/jsonnet/jsonnet_base_sandbox.h"
23 #include "sandboxed_api/util/fileops.h"
24 #include "sandboxed_api/util/path.h"
25 
JsonnetMain(std::string in_file,std::string out_file)26 absl::Status JsonnetMain(std::string in_file, std::string out_file) {
27   using sapi::file::JoinPath;
28   using sapi::file_util::fileops::Basename;
29 
30   // Initialize sandbox.
31   JsonnetBaseSandbox sandbox(in_file, out_file);
32   SAPI_RETURN_IF_ERROR(sandbox.Init());
33 
34   JsonnetApi api(&sandbox);
35 
36   // Initialize library's main structure.
37   SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make());
38   sapi::v::RemotePtr vm_pointer(jsonnet_vm);
39 
40   // Read input file.
41   std::string in_file_in_sandboxee(JoinPath("/input", Basename(in_file)));
42   sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
43   SAPI_ASSIGN_OR_RETURN(char* input,
44                         api.c_read_input(false, in_file_var.PtrBefore()));
45 
46   // Process jsonnet data.
47   sapi::v::RemotePtr input_pointer(input);
48   sapi::v::Int error;
49   SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet_stream(
50                                           &vm_pointer, in_file_var.PtrBefore(),
51                                           &input_pointer, error.PtrAfter()));
52   CHECK(!error.GetValue())
53       << "Jsonnet code evaluation failed: " << error.GetValue() << "\n"
54       << "Make sure all files used by your jsonnet file are in the same "
55          "directory as your file.";
56 
57   // Write data to file.
58   std::string out_file_in_sandboxee(JoinPath("/output", Basename(out_file)));
59   sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
60   sapi::v::RemotePtr output_pointer(output);
61 
62   SAPI_ASSIGN_OR_RETURN(
63       bool success,
64       api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()));
65   CHECK(success) << "Writing to output file failed: " << success;
66 
67   // Clean up.
68   SAPI_ASSIGN_OR_RETURN(char* result,
69                         api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0));
70   SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer));
71   SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer));
72 
73   return absl::OkStatus();
74 }
75 
main(int argc,char * argv[])76 int main(int argc, char* argv[]) {
77   using sapi::file_util::fileops::Basename;
78 
79   absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
80   absl::ParseCommandLine(argc, argv);
81   absl::InitializeLog();
82 
83   if (argc == 3) {
84     std::cerr << "Usage:\n"
85               << Basename(argv[0])
86               << " /absolute/path/to/INPUT.jsonnet /absolute/path/to/OUTPUT\n";
87     return EXIT_FAILURE;
88   }
89 
90   std::string in_file(argv[1]);
91   std::string out_file(argv[2]);
92 
93   absl::Status status = JsonnetMain(in_file, out_file);
94   if (!status.ok()) {
95     LOG(ERROR) << "Failed: " << status.ToString();
96     return EXIT_FAILURE;
97   }
98 
99   return EXIT_SUCCESS;
100 }
101