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 "contrib/jsonnet/jsonnet_base_transaction.h"
16
17 #include "absl/flags/parse.h"
18 #include "absl/log/check.h"
19 #include "absl/log/globals.h"
20 #include "absl/log/initialize.h"
21 #include "sandboxed_api/util/fileops.h"
22 #include "sandboxed_api/util/path.h"
23
Main()24 absl::Status JsonnetTransaction::Main() {
25 using sapi::file::JoinPath;
26 using sapi::file_util::fileops::Basename;
27
28 JsonnetApi api(sandbox());
29
30 // Initialize library's main structure.
31 SAPI_ASSIGN_OR_RETURN(JsonnetVm * jsonnet_vm, api.c_jsonnet_make());
32 sapi::v::RemotePtr vm_pointer(jsonnet_vm);
33
34 // Read input file.
35 std::string in_file_in_sandboxee(JoinPath("/input", Basename(in_file_)));
36 sapi::v::ConstCStr in_file_var(in_file_in_sandboxee.c_str());
37 SAPI_ASSIGN_OR_RETURN(char* input,
38 api.c_read_input(false, in_file_var.PtrBefore()));
39
40 // Process jsonnet data.
41 sapi::v::RemotePtr input_pointer(input);
42 sapi::v::Int error;
43 SAPI_ASSIGN_OR_RETURN(char* output, api.c_jsonnet_evaluate_snippet(
44 &vm_pointer, in_file_var.PtrBefore(),
45 &input_pointer, error.PtrAfter()));
46 TRANSACTION_FAIL_IF_NOT(error.GetValue() == 0,
47 "Jsonnet code evaluation failed.");
48
49 // Write data to file.
50 std::string out_file_in_sandboxee(JoinPath("/output", Basename(out_file_)));
51 sapi::v::ConstCStr out_file_var(out_file_in_sandboxee.c_str());
52 sapi::v::RemotePtr output_pointer(output);
53 SAPI_ASSIGN_OR_RETURN(
54 bool success,
55 api.c_write_output_file(&output_pointer, out_file_var.PtrBefore()));
56 TRANSACTION_FAIL_IF_NOT(success, "Writing to output file failed.");
57
58 // Clean up.
59 SAPI_ASSIGN_OR_RETURN(char* result,
60 api.c_jsonnet_realloc(&vm_pointer, &output_pointer, 0));
61
62 SAPI_RETURN_IF_ERROR(api.c_jsonnet_destroy(&vm_pointer));
63 SAPI_RETURN_IF_ERROR(api.c_free_input(&input_pointer));
64
65 return absl::OkStatus();
66 }
67
main(int argc,char * argv[])68 int main(int argc, char* argv[]) {
69 using sapi::file_util::fileops::Basename;
70
71 absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
72 absl::ParseCommandLine(argc, argv);
73 absl::InitializeLog();
74
75 if (argc != 3) {
76 std::cerr << "Usage:\n"
77 << Basename(argv[0])
78 << " /absolute/path/to/INPUT.jsonnet /absolute/path/to/OUTPUT\n";
79 return EXIT_FAILURE;
80 }
81
82 std::string in_file(argv[1]);
83 std::string out_file(argv[2]);
84
85 JsonnetTransaction jsonnet_transaction(in_file, out_file);
86
87 auto result = jsonnet_transaction.Run();
88
89 LOG(INFO) << "Transaction result: " << result.message();
90 CHECK(result.ok());
91
92 return EXIT_SUCCESS;
93 }
94