-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace.rs
More file actions
171 lines (155 loc) · 5.34 KB
/
workspace.rs
File metadata and controls
171 lines (155 loc) · 5.34 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
use crate::config;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize)]
struct Workspace {
public_id: String,
name: String,
active: bool,
favorite: bool,
provision_status: String,
}
#[derive(Deserialize)]
struct ListResponse {
workspaces: Vec<Workspace>,
}
fn load_client() -> (reqwest::blocking::Client, String, String) {
let profile_config = match config::load("default") {
Ok(c) => c,
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
};
let api_key = match &profile_config.api_key {
Some(key) if key != "PLACEHOLDER" => key.clone(),
_ => {
eprintln!("error: not authenticated. Run 'hotdata auth login' to log in.");
std::process::exit(1);
}
};
let api_url = profile_config.api_url.to_string();
(reqwest::blocking::Client::new(), api_key, api_url)
}
fn fetch_all_workspaces(client: &reqwest::blocking::Client, api_key: &str, api_url: &str) -> Vec<Workspace> {
let url = format!("{api_url}/workspaces");
let resp = match client
.get(&url)
.header("Authorization", format!("Bearer {api_key}"))
.send()
{
Ok(r) => r,
Err(e) => {
eprintln!("error connecting to API: {e}");
std::process::exit(1);
}
};
if !resp.status().is_success() {
eprintln!("error: {}", crate::util::api_error(resp.text().unwrap_or_default()));
std::process::exit(1);
}
match resp.json::<ListResponse>() {
Ok(b) => b.workspaces,
Err(e) => {
eprintln!("error parsing response: {e}");
std::process::exit(1);
}
}
}
pub fn set(workspace_id: Option<&str>) {
let (client, api_key, api_url) = load_client();
let workspaces = fetch_all_workspaces(&client, &api_key, &api_url);
let chosen = match workspace_id {
Some(id) => {
match workspaces.iter().find(|w| w.public_id == id) {
Some(w) => config::WorkspaceEntry { public_id: w.public_id.clone(), name: w.name.clone() },
None => {
eprintln!("error: workspace '{id}' not found or you don't have access to it.");
std::process::exit(1);
}
}
}
None => {
if workspaces.is_empty() {
eprintln!("error: no workspaces available.");
std::process::exit(1);
}
let options: Vec<String> = workspaces.iter()
.map(|w| format!("{} ({})", w.name, w.public_id))
.collect();
let selection = match inquire::Select::new("Select default workspace:", options.clone()).prompt() {
Ok(s) => s,
Err(_) => std::process::exit(1),
};
let idx = options.iter().position(|o| o == &selection).unwrap();
let w = &workspaces[idx];
config::WorkspaceEntry { public_id: w.public_id.clone(), name: w.name.clone() }
}
};
if let Err(e) = config::save_default_workspace("default", chosen.clone()) {
eprintln!("error saving config: {e}");
std::process::exit(1);
}
use crossterm::style::Stylize;
println!("{}", "Default workspace updated".green());
println!("id: {}", chosen.public_id);
println!("name: {}", chosen.name);
}
pub fn list(format: &str) {
let profile_config = match config::load("default") {
Ok(c) => c,
Err(e) => {
eprintln!("{e}");
std::process::exit(1);
}
};
let api_key = match &profile_config.api_key {
Some(key) if key != "PLACEHOLDER" => key.clone(),
_ => {
eprintln!("error: not authenticated. Run 'hotdata auth login' to log in.");
std::process::exit(1);
}
};
let default_id = profile_config.workspaces.first().map(|w| w.public_id.as_str()).unwrap_or("").to_string();
let url = format!("{}/workspaces", profile_config.api_url);
let client = reqwest::blocking::Client::new();
let resp = match client
.get(&url)
.header("Authorization", format!("Bearer {api_key}"))
.send()
{
Ok(r) => r,
Err(e) => {
eprintln!("error connecting to API: {e}");
std::process::exit(1);
}
};
if !resp.status().is_success() {
eprintln!("error: {}", crate::util::api_error(resp.text().unwrap_or_default()));
std::process::exit(1);
}
let body: ListResponse = match resp.json() {
Ok(b) => b,
Err(e) => {
eprintln!("error parsing response: {e}");
std::process::exit(1);
}
};
match format {
"json" => {
println!("{}", serde_json::to_string_pretty(&body.workspaces).unwrap());
}
"yaml" => {
print!("{}", serde_yaml::to_string(&body.workspaces).unwrap());
}
"table" => {
let mut table = crate::util::make_table();
table.set_header(["DEFAULT", "PUBLIC_ID", "NAME", "PROVISION_STATUS"]);
for w in &body.workspaces {
let marker = if w.public_id == default_id { "*" } else { "" };
table.add_row([marker, &w.public_id, &w.name, &w.provision_status]);
}
println!("{table}");
}
_ => unreachable!(),
}
}