-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathintegrations.rs
More file actions
201 lines (192 loc) · 6.86 KB
/
integrations.rs
File metadata and controls
201 lines (192 loc) · 6.86 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use crate::cli::{
show_values, FORMAT_OPT, GET_SUBCMD, INTEGRATION_NAME_ARG, JMES_PATH_ARG, LIST_SUBCMD,
RAW_FLAG, SECRETS_FLAG, SHOW_TIMES_FLAG,
};
use crate::database::{Integrations, OpenApiConfig};
use crate::table::Table;
use crate::utils::{error_message, warn_missing_subcommand, warning_message};
use clap::ArgMatches;
use color_eyre::eyre::Result;
use indoc::printdoc;
use std::process;
pub fn integration_not_found_message(integ_name: &str) -> String {
format!("Integration '{integ_name}' not found")
}
fn proc_integ_explore(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
integrations: &Integrations,
) -> Result<()> {
let fqn = subcmd_args.value_of("FQN");
let jmes = subcmd_args.value_of(JMES_PATH_ARG);
let show_raw = subcmd_args.is_present(RAW_FLAG);
let show_secrets = subcmd_args.is_present(SECRETS_FLAG);
let show_values = show_values(subcmd_args);
let fmt = subcmd_args.value_of(FORMAT_OPT).unwrap();
let nodes = integrations.get_integration_nodes(rest_cfg, fqn, jmes)?;
let indent = " ";
let raw_types: &[&str] = &["File", "Value"];
if nodes.is_empty() {
if let Some(fqn) = fqn {
error_message(format!("Nothing found for FQN '{fqn}'!"));
} else {
error_message("No integrations found.".to_string());
}
} else if show_raw {
if nodes.len() > 1 {
warning_message(format!(
"Raw content only works for a single file -- specified FQN has {} nodes.",
nodes.len()
));
} else if !raw_types.contains(&nodes[0].node_type.as_str()) {
warning_message(format!(
"Raw content only works for a single file -- specified FQN is {} type.",
nodes[0].node_type.to_lowercase()
));
} else if nodes[0].secret && !show_secrets {
warning_message("Must specify --secrets to display secret content".to_string());
} else {
println!("{}", &nodes[0].content_data);
}
} else if !show_values {
for node in nodes {
println!("{}", node.name);
let mut indent2 = indent.to_string();
if !node.jmes_path.is_empty() {
println!("{}{{{{ {} }}}}", indent, node.jmes_path);
indent2 = indent.repeat(2);
}
for key in node.content_keys {
println!("{indent2}{{{{ {key} }}}}");
}
}
} else {
let mut table = Table::new("integration");
table.set_header(&["Name", "FQN"]);
for node in nodes {
// add the node itself
table.add_row(vec![node.name, node.fqn.clone()]);
let mut indent2 = indent.to_string();
if !node.jmes_path.is_empty() {
let jmes_name = format!("{}{{{{ {} }}}}", indent, node.jmes_path);
table.add_row(vec![jmes_name, node.fqn.clone()]);
indent2 = indent.repeat(2);
}
for key in node.content_keys {
let entry_name = format!("{indent2}{{{{ {key} }}}}");
table.add_row(vec![entry_name, node.fqn.clone()]);
}
}
table.render(fmt)?;
}
Ok(())
}
fn proc_integ_get(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
integrations: &Integrations,
) -> Result<()> {
let integ_name = subcmd_args.value_of(INTEGRATION_NAME_ARG).unwrap();
let integ_resp = integrations.get_details_by_name(rest_cfg, integ_name)?;
if let Some(details) = integ_resp {
printdoc!(
r#"
Name: {}
Provider: {}
FQN: {}
Description: {}
ID: {}
Created At: {}
Modified At: {}
Status:
Value: {}
Details: {}
Updated At: {}
"#,
details.name,
details.provider,
details.fqn,
details.description,
details.id,
details.created_at,
details.modified_at,
details.status,
details.status_detail,
details.status_time,
);
} else {
error_message(integration_not_found_message(integ_name));
process::exit(32);
}
Ok(())
}
fn proc_integ_list(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
integrations: &Integrations,
) -> Result<()> {
let details = integrations.get_integration_details(rest_cfg)?;
let show_times = subcmd_args.is_present(SHOW_TIMES_FLAG);
let show_values = show_values(subcmd_args);
let fmt = subcmd_args.value_of(FORMAT_OPT).unwrap();
if details.is_empty() {
println!("No integrations found");
} else if !show_values {
let list = details
.iter()
.map(|d| d.name.clone())
.collect::<Vec<String>>();
println!("{}", list.join("\n"))
} else {
let mut hdr = vec!["Name", "FQN", "Status", "Updated", "Description"];
let mut properties = vec!["name", "fqn", "status", "status-time", "description"];
if show_times {
hdr.push("Created At");
hdr.push("Modified At");
properties.push("created-at");
properties.push("modified-at");
}
let mut table = Table::new("integration");
table.set_header(&hdr);
for entry in details {
table.add_row(entry.get_properties(&properties));
}
table.render(fmt)?;
}
Ok(())
}
fn proc_integ_refresh(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
integrations: &Integrations,
) -> Result<()> {
let integ_name = subcmd_args.value_of(INTEGRATION_NAME_ARG).unwrap();
let integ_resp = integrations.get_id(rest_cfg, integ_name)?;
if let Some(integ_id) = integ_resp {
integrations.refresh_connection(rest_cfg, &integ_id)?;
println!("Refreshed integration '{integ_name}'");
} else {
error_message(integration_not_found_message(integ_name));
process::exit(32);
}
Ok(())
}
/// Process the 'integrations' sub-command
pub fn process_integrations_command(
subcmd_args: &ArgMatches,
rest_cfg: &OpenApiConfig,
) -> Result<()> {
let integrations = Integrations::new();
if let Some(subcmd_args) = subcmd_args.subcommand_matches("explore") {
proc_integ_explore(subcmd_args, rest_cfg, &integrations)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches(GET_SUBCMD) {
proc_integ_get(subcmd_args, rest_cfg, &integrations)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches(LIST_SUBCMD) {
proc_integ_list(subcmd_args, rest_cfg, &integrations)?;
} else if let Some(subcmd_args) = subcmd_args.subcommand_matches("refresh") {
proc_integ_refresh(subcmd_args, rest_cfg, &integrations)?;
} else {
warn_missing_subcommand("integrations");
}
Ok(())
}