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
202
203
204
205
206
207
mod sync;
mod k8;
mod context;
use sync::SyncCommand;
use structopt::StructOpt;
pub use k8::set_k8_context;
pub use k8::discover_fluvio_addr;
pub use context::set_local_context;
pub use sync::LocalOpt;
pub use sync::K8Opt;
use fluvio::config::ConfigFile;
use crate::Terminal;
use crate::error::CliError;
use crate::t_println;
use crate::t_print_cli_err;
use crate::profile::sync::process_sync;
#[derive(Debug, StructOpt)]
#[structopt(about = "Available Commands")]
pub enum ProfileCommand {
#[structopt(name = "current")]
DisplayCurrent,
#[structopt(name = "delete")]
Delete(DeleteOpt),
#[structopt(name = "delete-cluster")]
DeleteCluster(DeleteClusterOpt),
#[structopt(name = "switch")]
Switch(SwitchOpt),
#[structopt(name = "sync")]
Sync(SyncCommand),
#[structopt(name = "view")]
View,
}
#[derive(Debug, StructOpt)]
pub struct DeleteOpt {
#[structopt(value_name = "profile name")]
pub profile_name: String,
}
#[derive(Debug, StructOpt)]
pub struct DeleteClusterOpt {
#[structopt(value_name = "cluster name")]
pub cluster_name: String,
#[structopt(short, long)]
pub force: bool,
}
#[derive(Debug, StructOpt)]
pub struct SwitchOpt {
#[structopt(value_name = "profile name")]
pub profile_name: String,
}
pub async fn process_profile<O>(
out: std::sync::Arc<O>,
profile_command: ProfileCommand,
) -> eyre::Result<String>
where
O: Terminal,
{
use context::*;
match profile_command {
ProfileCommand::View => view_profile(out)?,
ProfileCommand::DisplayCurrent => display_current_profile(out),
ProfileCommand::Switch(profile) => {
process_switch(out, profile)?;
}
ProfileCommand::Delete(profile) => {
process_delete(out, profile)?;
}
ProfileCommand::DeleteCluster(cluster) => {
process_delete_cluster(out, cluster)?;
}
ProfileCommand::Sync(profile) => {
process_sync(out, profile).await?;
}
}
Ok("".to_owned())
}
pub fn process_switch<O>(out: std::sync::Arc<O>, opt: SwitchOpt) -> Result<String, CliError>
where
O: Terminal,
{
let profile_name = opt.profile_name;
match ConfigFile::load(None) {
Ok(mut config_file) => {
if !config_file.mut_config().set_current_profile(&profile_name) {
t_println!(out, "profile {} not found", &profile_name);
} else if let Err(err) = config_file.save() {
t_println!(out, "unable to save profile: {}", err);
}
}
Err(_) => t_print_cli_err!(out, "no profile can be found"),
}
Ok("".to_string())
}
pub fn process_delete<O>(out: std::sync::Arc<O>, opt: DeleteOpt) -> Result<String, CliError>
where
O: Terminal,
{
let profile_name = opt.profile_name;
match ConfigFile::load(None) {
Ok(mut config_file) => {
if !config_file.mut_config().delete_profile(&profile_name) {
t_println!(out, "profile {} not found", &profile_name);
} else if let Err(err) = config_file.save() {
t_println!(out, "unable to save profile: {}", err);
} else {
t_println!(out, "profile {} deleted", &profile_name);
if config_file.config().current_profile_name().is_none() {
t_println!(out,"warning: this removed your current profile, use 'config switch-profile to select a different one");
} else {
t_println!(out, "profile deleted");
}
}
}
Err(_) => t_print_cli_err!(out, "no profile can be found"),
}
Ok("".to_string())
}
pub fn process_delete_cluster<O>(
_out: std::sync::Arc<O>,
opt: DeleteClusterOpt,
) -> Result<String, CliError>
where
O: Terminal,
{
let cluster_name = opt.cluster_name;
let mut config_file = match ConfigFile::load(None) {
Ok(config_file) => config_file,
Err(e) => {
println!("No config can be found: {}", e);
return Ok("".to_string());
}
};
let config = config_file.mut_config();
if config.cluster(&cluster_name).is_none() {
println!("No profile named {} exists", &cluster_name);
return Ok("".to_string());
}
if !opt.force {
if let Err(profile_conflicts) = config.delete_cluster_check(&cluster_name) {
println!(
"The following profiles reference cluster {}:",
&cluster_name
);
for profile in profile_conflicts.iter() {
println!(" {}", profile);
}
println!("If you would still like to delete the cluster, use --force");
return Ok("".to_string());
}
}
let _deleted = match config.delete_cluster(&cluster_name) {
Some(deleted) => deleted,
None => {
println!("Cluster {} not found", &cluster_name);
return Ok("".to_string());
}
};
match config_file.save() {
Ok(_) => Ok(format!("Cluster {} deleted", &cluster_name)),
Err(e) => {
println!("Unable to save config file: {}", e);
Ok("".to_string())
}
}
}