-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlib.rs
More file actions
114 lines (94 loc) · 3.33 KB
/
lib.rs
File metadata and controls
114 lines (94 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(improper_ctypes)]
#![allow(non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
use std::ffi::{c_char, CStr, CString};
pub const HELM_ERROR_PREFIX: &str = "ERROR:";
pub fn install_helm_release(
release_name: &str,
chart_name: &str,
chart_version: &str,
values_yaml: &str,
namespace: &str,
suppress_output: bool,
) -> String {
let release_name = CString::new(release_name).unwrap();
let chart_name = CString::new(chart_name).unwrap();
let chart_version = CString::new(chart_version).unwrap();
let values_yaml = CString::new(values_yaml).unwrap();
let namespace = CString::new(namespace).unwrap();
unsafe {
let c = go_install_helm_release(
release_name.as_ptr() as *mut c_char,
chart_name.as_ptr() as *mut c_char,
chart_version.as_ptr() as *mut c_char,
values_yaml.as_ptr() as *mut c_char,
namespace.as_ptr() as *mut c_char,
suppress_output as u8,
);
cstr_ptr_to_string(c)
}
}
pub fn uninstall_helm_release(
release_name: &str,
namespace: &str,
suppress_output: bool,
) -> String {
let release_name = CString::new(release_name).unwrap();
let namespace = CString::new(namespace).unwrap();
unsafe {
let c = go_uninstall_helm_release(
release_name.as_ptr() as *mut c_char,
namespace.as_ptr() as *mut c_char,
suppress_output as u8,
);
cstr_ptr_to_string(c)
}
}
// TODO (@NickLarsenNZ): Add tracing to helm-sys, maybe?
// #[instrument]
pub fn check_helm_release_exists(release_name: &str, namespace: &str) -> bool {
let release_name = CString::new(release_name).unwrap();
let namespace = CString::new(namespace).unwrap();
unsafe {
go_helm_release_exists(
release_name.as_ptr() as *mut c_char,
namespace.as_ptr() as *mut c_char,
) != 0
}
}
pub fn list_helm_releases(namespace: &str) -> String {
let namespace = CString::new(namespace).unwrap();
unsafe {
let c = go_helm_list_releases(namespace.as_ptr() as *mut c_char);
cstr_ptr_to_string(c)
}
}
pub fn add_helm_repository(repository_name: &str, repository_url: &str) -> String {
let repository_name = CString::new(repository_name).unwrap();
let repository_url = CString::new(repository_url).unwrap();
unsafe {
let c = go_add_helm_repo(
repository_name.as_ptr() as *mut c_char,
repository_url.as_ptr() as *mut c_char,
);
cstr_ptr_to_string(c)
}
}
/// Checks if the result string is an error, and if so, returns the error message as a string.
pub fn to_helm_error(result: &str) -> Option<String> {
if !result.is_empty() && result.starts_with(HELM_ERROR_PREFIX) {
return Some(result.replace(HELM_ERROR_PREFIX, ""));
}
None
}
/// Converts a raw C string pointer into an owned Rust [`String`]. This function
/// also makes sure, that the pointer (and underlying memory) of the Go string is
/// freed. The pointer **cannot** be used afterwards.
unsafe fn cstr_ptr_to_string(c: *mut c_char) -> String {
let cstr = CStr::from_ptr(c);
let s = String::from_utf8_lossy(cstr.to_bytes()).to_string();
free_go_string(cstr.as_ptr() as *mut c_char);
s
}