feat(openstack): Add module for OpenStack (#1664)

* first (crude) attempt to implement the openstack module

* Attempt to follow OpenStack standards for clouds.yaml location

* fmt

* provide unittest

* add documentation

* fix fmt

* fix clippy

* fix clippy

* fix fmt

* fix small nitpicks

* expand openstack explanation

* load config file using find_map

* fix fmt

* add test for valid config, plus fix test for invalid config

* fix fmt

* re-add forgotten comment
This commit is contained in:
Mauricio Teixeira 2020-10-24 05:46:43 -04:00 committed by GitHub
parent 745a554fdc
commit eec961caaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 210 additions and 0 deletions

View File

@ -223,6 +223,7 @@ $conda\
$memory_usage\
$aws\
$gcloud\
$openstack\
$env_var\
$crystal\
$cmd_duration\
@ -1786,6 +1787,44 @@ The module will be shown if any of the following conditions are met:
format = "via [🐪 $version]($style) "
```
## OpenStack
The `openstack` module shows the current OpenStack cloud and project. The module
only active when the `OS_CLOUD` env var is set, in which case it will read
`clouds.yaml` file from any of the [default locations](https://docs.openstack.org/python-openstackclient/latest/configuration/index.html#configuration-files).
to fetch the current project in use.
### Options
| Option | Default | Description |
| ---------------- | ------------------------------------------------ | --------------------------------------------------------------- |
| `format` | `"on [$symbol$cloud(\\($project\\))]($style) "` | The format for the module. |
| `symbol` | `"☁️ "` | The symbol used before displaying the current OpenStack cloud. |
| `style` | `"bold yellow"` | The style for the module. |
| `disabled` | `false` | Disables the `OpenStack` module. |
### Variables
| Variable | Example | Description |
| -------- | ---------- | ------------------------------------ |
| cloud | `corp` | The current OpenStack cloud |
| project | `dev` | The current OpenStack project |
| symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |
\*: This variable can only be used as a part of a style string
### Example
```toml
# ~/.config/starship.toml
[openstack]
format = "on [$symbol$cloud(\\($project\\))]($style) "
style = "bold yellow"
symbol = "☁️ "
```
## Perl
The `perl` module shows the currently installed version of Perl.

View File

@ -32,6 +32,7 @@ pub mod nim;
pub mod nix_shell;
pub mod nodejs;
pub mod ocaml;
pub mod openstack;
pub mod package;
pub mod perl;
pub mod php;

21
src/configs/openstack.rs Normal file
View File

@ -0,0 +1,21 @@
use crate::config::{ModuleConfig, RootModuleConfig};
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig)]
pub struct OspConfig<'a> {
pub format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
}
impl<'a> RootModuleConfig<'a> for OspConfig<'a> {
fn new() -> Self {
OspConfig {
format: "on [$symbol$cloud(\\($project\\))]($style) ",
symbol: "☁️ ",
style: "bold yellow",
disabled: false,
}
}
}

View File

@ -56,6 +56,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"memory_usage",
"aws",
"gcloud",
"openstack",
"env_var",
"crystal",
"cmd_duration",

View File

@ -43,6 +43,7 @@ pub const ALL_MODULES: &[&str] = &[
"nix_shell",
"nodejs",
"ocaml",
"openstack",
"package",
"perl",
"purescript",

View File

@ -33,6 +33,7 @@ mod nim;
mod nix_shell;
mod nodejs;
mod ocaml;
mod openstack;
mod package;
mod perl;
mod php;
@ -99,6 +100,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"nix_shell" => nix_shell::module(context),
"nodejs" => nodejs::module(context),
"ocaml" => ocaml::module(context),
"openstack" => openstack::module(context),
"package" => package::module(context),
"perl" => perl::module(context),
"php" => php::module(context),
@ -172,6 +174,7 @@ pub fn description(module: &str) -> &'static str {
"nix_shell" => "The nix-shell environment",
"nodejs" => "The currently installed version of NodeJS",
"ocaml" => "The currently installed version of OCaml",
"openstack" => "The current OpenStack cloud and project",
"package" => "The package version of the current directory's project",
"perl" => "The currently installed version of Perl",
"php" => "The currently installed version of PHP",

144
src/modules/openstack.rs Normal file
View File

@ -0,0 +1,144 @@
use yaml_rust::YamlLoader;
use super::{Context, Module, RootModuleConfig};
use crate::configs::openstack::OspConfig;
use crate::formatter::StringFormatter;
use crate::utils;
type Cloud = String;
type Project = String;
fn get_osp_project_from_config(context: &Context, osp_cloud: Option<&str>) -> Option<Project> {
// Attempt to follow OpenStack standards for clouds.yaml location:
// 1st = $PWD/clouds.yaml, 2nd = $HOME/.config/openstack/clouds.yaml, 3rd = /etc/openstack/clouds.yaml
let config = vec![
utils::read_file(context.get_env("PWD").unwrap() + "/clouds.yaml"),
utils::read_file(dirs_next::home_dir()?.join(".config/openstack/clouds.yaml")),
utils::read_file("/etc/openstack/clouds.yaml"),
];
let clouds =
YamlLoader::load_from_str(&config.into_iter().find_map(Result::ok).unwrap()).ok()?;
let project = &clouds[0]["clouds"][osp_cloud.unwrap()]["auth"]["project_name"]
.as_str()
.unwrap_or("");
if project.is_empty() {
return None;
}
Some(project.to_string())
}
fn get_osp_cloud_and_project(context: &Context) -> (Option<Cloud>, Option<Project>) {
match (
context.get_env("OS_CLOUD"),
context.get_env("OS_PROJECT_NAME"),
) {
(Some(p), Some(r)) => (Some(p), Some(r)),
(None, Some(r)) => (None, Some(r)),
(Some(ref p), None) => (
Some(p.to_owned()),
get_osp_project_from_config(context, Some(p)),
),
(None, None) => (None, None),
}
}
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("openstack");
let config: OspConfig = OspConfig::try_load(module.config);
let (osp_cloud, osp_project) = get_osp_cloud_and_project(context);
osp_cloud.as_ref()?;
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|variable, _| match variable {
"symbol" => Some(config.symbol),
_ => None,
})
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
_ => None,
})
.map(|variable| match variable {
"cloud" => osp_cloud.as_ref().map(Ok),
"project" => osp_project.as_ref().map(Ok),
_ => None,
})
.parse(None)
});
module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::error!("Error in module `openstack`: \n{}", error);
return None;
}
});
Some(module)
}
#[cfg(test)]
mod tests {
use crate::test::ModuleRenderer;
use ansi_term::Color;
use std::fs::File;
use std::io::{self, Write};
#[test]
fn parse_valid_config() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let config_path = dir.path().join("clouds.yaml");
let mut file = File::create(&config_path)?;
file.write_all(
b"---
clouds:
corp:
auth:
auth_url: https://overcloud.example.com:13000/v3
project_name: testproject
identity_api_version: '3'
interface: public
",
)?;
let actual = ModuleRenderer::new("openstack")
.env("PWD", dir.path().to_str().unwrap())
.env("OS_CLOUD", "corp")
.config(toml::toml! {
[openstack]
})
.collect();
let expected = Some(format!(
"on {} ",
Color::Yellow.bold().paint("☁️ corp(testproject)")
));
assert_eq!(actual, expected);
dir.close()
}
#[test]
fn parse_broken_config() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let config_path = dir.path().join("clouds.yaml");
let mut file = File::create(&config_path)?;
file.write_all(
b"---
dummy_yaml
",
)?;
let actual = ModuleRenderer::new("openstack")
.env("PWD", dir.path().to_str().unwrap())
.env("OS_CLOUD", "test")
.config(toml::toml! {
[openstack]
})
.collect();
let expected = Some(format!("on {} ", Color::Yellow.bold().paint("☁️ test")));
assert_eq!(actual, expected);
dir.close()
}
}