-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace.rs
More file actions
135 lines (126 loc) · 4.33 KB
/
Copy pathworkspace.rs
File metadata and controls
135 lines (126 loc) · 4.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
use crate::api::ApiClient;
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>,
}
pub fn set(workspace_id: Option<&str>) {
if std::env::var("HOTDATA_SANDBOX").is_ok()
|| crate::sandbox::find_sandbox_run_ancestor().is_some()
{
eprintln!("error: workspace cannot be changed inside a sandbox");
std::process::exit(1);
}
let api = ApiClient::new(None);
let body: ListResponse = api.get("/workspaces");
let workspaces = body.workspaces;
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);
}
if !crate::util::is_interactive() {
eprintln!(
"error: stdin is not a TTY; cannot prompt for selection. \
Run 'hotdata workspaces list' to see available IDs, \
then 'hotdata workspaces set <workspace_id>'."
);
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 default_id = std::env::var("HOTDATA_WORKSPACE").unwrap_or_else(|_| {
profile_config
.workspaces
.first()
.map(|w| w.public_id.clone())
.unwrap_or_default()
});
let api = ApiClient::new(None);
let body: ListResponse = api.get("/workspaces");
match format {
"json" => {
println!(
"{}",
serde_json::to_string_pretty(&body.workspaces).unwrap()
);
}
"yaml" => {
print!("{}", serde_yaml::to_string(&body.workspaces).unwrap());
}
"table" => {
if body.workspaces.is_empty() {
use crossterm::style::Stylize;
eprintln!("{}", "No workspaces found.".dark_grey());
} else {
let rows: Vec<Vec<String>> = body
.workspaces
.iter()
.map(|w| {
let marker = if w.public_id == default_id { "*" } else { "" };
vec![
marker.to_string(),
w.public_id.clone(),
w.name.clone(),
w.provision_status.clone(),
]
})
.collect();
crate::table::print(&["DEFAULT", "PUBLIC_ID", "NAME", "PROVISION_STATUS"], &rows);
}
}
_ => unreachable!(),
}
}