mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
feat: Implement terraform module with workspace and version (#644)
This commit is contained in:
@@ -25,6 +25,7 @@ pub mod python;
|
||||
pub mod ruby;
|
||||
pub mod rust;
|
||||
mod starship_root;
|
||||
pub mod terraform;
|
||||
pub mod time;
|
||||
pub mod username;
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ impl<'a> RootModuleConfig<'a> for StarshipRootConfig<'a> {
|
||||
"python",
|
||||
"ruby",
|
||||
"rust",
|
||||
"terraform",
|
||||
// ↑ Toolchain version modules ↑
|
||||
"nix_shell",
|
||||
"conda",
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
|
||||
|
||||
use ansi_term::{Color, Style};
|
||||
use starship_module_config_derive::ModuleConfig;
|
||||
|
||||
#[derive(Clone, ModuleConfig)]
|
||||
pub struct TerraformConfig<'a> {
|
||||
pub symbol: SegmentConfig<'a>,
|
||||
pub workspace: SegmentConfig<'a>,
|
||||
pub version: SegmentConfig<'a>,
|
||||
pub style: Style,
|
||||
pub disabled: bool,
|
||||
pub show_version: bool,
|
||||
}
|
||||
|
||||
impl<'a> RootModuleConfig<'a> for TerraformConfig<'a> {
|
||||
fn new() -> Self {
|
||||
TerraformConfig {
|
||||
symbol: SegmentConfig::new("💠 "),
|
||||
workspace: SegmentConfig::default(),
|
||||
version: SegmentConfig::default(),
|
||||
style: Color::Fixed(105).bold(),
|
||||
disabled: false,
|
||||
show_version: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,7 @@ pub const ALL_MODULES: &[&str] = &[
|
||||
"ruby",
|
||||
"rust",
|
||||
"php",
|
||||
"terraform",
|
||||
"time",
|
||||
"username",
|
||||
];
|
||||
|
||||
@@ -25,6 +25,7 @@ mod php;
|
||||
mod python;
|
||||
mod ruby;
|
||||
mod rust;
|
||||
mod terraform;
|
||||
mod time;
|
||||
mod username;
|
||||
mod utils;
|
||||
@@ -68,6 +69,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
|
||||
"python" => python::module(context),
|
||||
"ruby" => ruby::module(context),
|
||||
"rust" => rust::module(context),
|
||||
"terraform" => terraform::module(context),
|
||||
"time" => time::module(context),
|
||||
"username" => username::module(context),
|
||||
_ => {
|
||||
|
||||
@@ -0,0 +1,125 @@
|
||||
use super::{Context, Module, RootModuleConfig};
|
||||
|
||||
use crate::configs::terraform::TerraformConfig;
|
||||
use crate::utils;
|
||||
use std::env;
|
||||
use std::io;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// Creates a module with the current Terraform version and workspace
|
||||
///
|
||||
/// Will display the Terraform version and workspace if any of the following criteria are met:
|
||||
/// - Current directory contains a `.terraform` directory
|
||||
/// - Current directory contains a file with the `.tf` extension
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let is_terraform_project = context
|
||||
.try_begin_scan()?
|
||||
.set_folders(&[".terraform"])
|
||||
.set_extensions(&["tf"])
|
||||
.is_match();
|
||||
|
||||
if !is_terraform_project {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut module = context.new_module("terraform");
|
||||
let config: TerraformConfig = TerraformConfig::try_load(module.config);
|
||||
|
||||
module.set_style(config.style);
|
||||
module.create_segment("symbol", &config.symbol);
|
||||
|
||||
if config.show_version {
|
||||
let terraform_version =
|
||||
format_terraform_version(&utils::exec_cmd("terraform", &["version"])?.stdout.as_str())?;
|
||||
module.create_segment("version", &config.version.with_value(&terraform_version));
|
||||
}
|
||||
|
||||
let terraform_workspace = &get_terraform_workspace(&context.current_dir)?;
|
||||
module.create_segment(
|
||||
"workspace",
|
||||
&config.workspace.with_value(&terraform_workspace),
|
||||
);
|
||||
|
||||
Some(module)
|
||||
}
|
||||
|
||||
// Determines the currently selected workspace (see https://github.com/hashicorp/terraform/blob/master/command/meta.go for the original implementation)
|
||||
fn get_terraform_workspace(cwd: &PathBuf) -> Option<String> {
|
||||
// Workspace can be explicitly overwritten by an env var
|
||||
let workspace_override = env::var("TF_WORKSPACE");
|
||||
if workspace_override.is_ok() {
|
||||
return workspace_override.ok();
|
||||
}
|
||||
|
||||
// Data directory containing current workspace can be overwritten by an env var
|
||||
let datadir = match env::var("TF_DATA_DIR") {
|
||||
Ok(s) => PathBuf::from(s),
|
||||
Err(_) => cwd.join(".terraform"),
|
||||
};
|
||||
match utils::read_file(datadir.join("environment")) {
|
||||
Err(ref e) if e.kind() == io::ErrorKind::NotFound => Some("default".to_string()),
|
||||
Ok(s) => Some(s),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn format_terraform_version(version: &str) -> Option<String> {
|
||||
// `terraform version` output looks like this
|
||||
// Terraform v0.12.14
|
||||
// With potential extra output if it detects you are not running the latest version
|
||||
Some(
|
||||
version
|
||||
.lines()
|
||||
.next()?
|
||||
.trim_start_matches("Terraform ")
|
||||
.trim()
|
||||
.to_owned()
|
||||
+ " ",
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_format_terraform_version_release() {
|
||||
let input = "Terraform v0.12.14";
|
||||
assert_eq!(
|
||||
format_terraform_version(input),
|
||||
Some("v0.12.14 ".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_terraform_version_prerelease() {
|
||||
let input = "Terraform v0.12.14-rc1";
|
||||
assert_eq!(
|
||||
format_terraform_version(input),
|
||||
Some("v0.12.14-rc1 ".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_terraform_version_development() {
|
||||
let input = "Terraform v0.12.14-dev (cca89f74)";
|
||||
assert_eq!(
|
||||
format_terraform_version(input),
|
||||
Some("v0.12.14-dev (cca89f74) ".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_terraform_version_multiline() {
|
||||
let input = "Terraform v0.12.13
|
||||
|
||||
Your version of Terraform is out of date! The latest version
|
||||
is 0.12.14. You can update by downloading from www.terraform.io/downloads.html
|
||||
|
||||
";
|
||||
assert_eq!(
|
||||
format_terraform_version(input),
|
||||
Some("v0.12.13 ".to_string())
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user