-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontext.rs
156 lines (129 loc) · 4.76 KB
/
context.rs
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
// Copyright (c) The Amphitheatre Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashMap;
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use super::Credentials;
/// The `Cluster` is used to store the server address and access token of the cluster.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct Cluster {
/// the name of the cluster
pub title: String,
/// the server address of the cluster
pub server: String,
/// the optional access token of the cluster
pub token: Option<String>,
/// the optional credentials used in the cluster
pub credentials: Option<Credentials>,
}
impl Default for Cluster {
fn default() -> Self {
Self {
title: String::from("default"),
server: String::from("http://localhost:8170"),
token: None,
credentials: None,
}
}
}
/// `ContextConfiguration` is used to store the configuration of the context.
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ContextConfiguration {
/// the selected context name of the cluster
current: Option<String>,
/// the list of connectable clusters
clusters: HashMap<String, Cluster>,
}
impl ContextConfiguration {
/// Get the context by name.
pub fn get(&self, name: &str) -> Option<&Cluster> {
self.clusters.get(name)
}
/// Get the current context with name.
pub fn current(&self) -> Option<(String, Cluster)> {
if let Some(name) = &self.current {
return Some((name.clone(), self.clusters.get(name).cloned().unwrap_or_default()));
}
None
}
// Remove a context from the list of contexts by name and save the configuration.
pub fn delete(&mut self, name: &str) -> Result<()> {
if !self.clusters.contains_key(name) {
return Err(anyhow!("Context with name `{}` does not exist", name));
}
self.clusters.remove(name);
println!("Deleted context with name `{}`", name);
Ok(())
}
/// impl iter method for ContextConfiguration
pub fn iter(&self) -> impl Iterator<Item = (&String, &Cluster)> {
self.clusters.iter()
}
/// Get the list of clusters.
pub fn clusters(&self) -> &HashMap<String, Cluster> {
&self.clusters
}
/// Check if the context with the given name exists.
pub fn exists(&self, name: &str) -> bool {
self.clusters.contains_key(name)
}
/// Set the current context by name
pub fn select(&mut self, name: &str) -> Result<()> {
if !self.clusters.contains_key(name) {
return Err(anyhow!("Context with name `{}` does not exist", name));
}
self.current = Some(name.to_owned());
println!("Set current context to `{}`", name);
Ok(())
}
/// Add a new context to the list of contexts and set it as the current context.
pub fn add(&mut self, name: &str, cluster: Cluster) -> Result<()> {
if self.clusters.contains_key(name) {
return Err(anyhow!("Context with name `{}` already exists", name));
}
self.clusters.insert(name.to_owned(), cluster);
self.current = Some(name.to_owned());
println!("Added context with name `{}`", name);
Ok(())
}
}
impl Default for ContextConfiguration {
fn default() -> Self {
Self {
current: Some(String::from("default")),
clusters: HashMap::from([(String::from("default"), Cluster::default())]),
}
}
}
mod test {
#[test]
fn test_cluster_default() {
use super::Cluster;
let cluster = Cluster::default();
assert_eq!(cluster.title, String::from("default"));
assert_eq!(cluster.server, String::from("http://localhost:8170"));
assert_eq!(cluster.token, None);
}
#[test]
fn test_context_configuration_default() {
use super::ContextConfiguration;
let context_configuration = ContextConfiguration::default();
assert_eq!(context_configuration.current, Some(String::from("default")));
assert_eq!(context_configuration.clusters.len(), 1);
assert_eq!(
context_configuration.clusters.get("default").unwrap().title,
String::from("default")
);
}
}