summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas O'Donnell <andytom@users.noreply.github.com>2019-09-26 04:55:47 +0200
committerKevin Song <chipbuster@users.noreply.github.com>2019-09-25 21:55:47 -0500
commitb050c597081967cbe7135d294f0cf43c2ec7608d (patch)
treef352a022b49ad9fb1b5d5fed1effe549b3e08e2f
parent80a9e7b9a638e4e695be8280a9baff3550037111 (diff)
feat: Add AWS module (#419)
Adds a module for displaying the current AWS profile based on the AWS_PROFILE envar.
-rw-r--r--docs/config/README.md24
-rw-r--r--src/module.rs1
-rw-r--r--src/modules/aws.rs29
-rw-r--r--src/modules/mod.rs2
-rw-r--r--src/print.rs1
-rw-r--r--tests/testsuite/aws.rs25
-rw-r--r--tests/testsuite/main.rs1
7 files changed, 83 insertions, 0 deletions
diff --git a/docs/config/README.md b/docs/config/README.md
index be0bce93a..d21df11b1 100644
--- a/docs/config/README.md
+++ b/docs/config/README.md
@@ -84,6 +84,7 @@ prompt_order = [
"username",
"hostname",
"directory",
+ "aws",
"git_branch",
"git_state",
"git_status",
@@ -104,6 +105,29 @@ prompt_order = [
]
```
+## AWS
+
+The `aws` module shows the current AWS profile. This is based on the
+`AWS_PROFILE` env var.
+
+### Options
+
+| Variable | Default | Description |
+| ---------- | --------------- | ---------------------------------------------------- |
+| `disabled` | `false` | Disables the `AWS` module |
+| `style` | `"bold yellow"` | The style used for the module |
+| `symbol` | `"☁️ "` | The symbol before displaying the current AWS profile |
+
+### Example
+
+```toml
+# ~/.config/starship.toml
+
+[aws]
+style = "bold blue"
+symbol = "🅰 "
+```
+
## Battery
The `battery` module shows how charged the device's battery is and its current charging status.
diff --git a/src/module.rs b/src/module.rs
index 2cda82a08..47c5eabfe 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -6,6 +6,7 @@ use std::fmt;
// List of all modules
pub const ALL_MODULES: &[&str] = &[
+ "aws",
#[cfg(feature = "battery")]
"battery",
"character",
diff --git a/src/modules/aws.rs b/src/modules/aws.rs
new file mode 100644
index 000000000..65ca05142
--- /dev/null
+++ b/src/modules/aws.rs
@@ -0,0 +1,29 @@
+use std::env;
+
+use ansi_term::Color;
+
+use super::{Context, Module};
+
+pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
+ const AWS_CHAR: &str = "☁️ ";
+ const AWS_PREFIX: &str = "on ";
+
+ let aws_profile = env::var("AWS_PROFILE").ok()?;
+ if aws_profile.is_empty() {
+ return None;
+ }
+
+ let mut module = context.new_module("aws");
+
+ let module_style = module
+ .config_value_style("style")
+ .unwrap_or_else(|| Color::Yellow.bold());
+ module.set_style(module_style);
+
+ module.get_prefix().set_value(AWS_PREFIX);
+
+ module.new_segment("symbol", AWS_CHAR);
+ module.new_segment("profile", &aws_profile);
+
+ Some(module)
+}
diff --git a/src/modules/mod.rs b/src/modules/mod.rs
index 405906188..c1618252d 100644
--- a/src/modules/mod.rs
+++ b/src/modules/mod.rs
@@ -1,4 +1,5 @@
// While adding out new module add out module to src/module.rs ALL_MODULES const array also.
+mod aws;
mod character;
mod cmd_duration;
mod directory;
@@ -27,6 +28,7 @@ use crate::module::Module;
pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
match module {
+ "aws" => aws::module(context),
"directory" => directory::module(context),
"character" => character::module(context),
"nodejs" => nodejs::module(context),
diff --git a/src/print.rs b/src/print.rs
index 12e98fb2a..1c7e4670c 100644
--- a/src/print.rs
+++ b/src/print.rs
@@ -15,6 +15,7 @@ const DEFAULT_PROMPT_ORDER: &[&str] = &[
"username",
"hostname",
"directory",
+ "aws",
"git_branch",
"git_state",
"git_status",
diff --git a/tests/testsuite/aws.rs b/tests/testsuite/aws.rs
new file mode 100644
index 000000000..d02bccca2
--- /dev/null
+++ b/tests/testsuite/aws.rs
@@ -0,0 +1,25 @@
+use ansi_term::Color;
+use std::io;
+
+use crate::common;
+
+#[test]
+fn no_profile_set() -> io::Result<()> {
+ let output = common::render_module("aws").env_clear().output()?;
+ let expected = "";
+ let actual = String::from_utf8(output.stdout).unwrap();
+ assert_eq!(expected, actual);
+ Ok(())
+}
+
+#[test]
+fn profile_set() -> io::Result<()> {
+ let output = common::render_module("aws")
+ .env_clear()
+ .env("AWS_PROFILE", "astronauts")
+ .output()?;
+ let expected = format!("on {} ", Color::Yellow.bold().paint("☁️ astronauts"));
+ let actual = String::from_utf8(output.stdout).unwrap();
+ assert_eq!(expected, actual);
+ Ok(())
+}
diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs
index d299ca610..89f573f9c 100644
--- a/tests/testsuite/main.rs
+++ b/tests/testsuite/main.rs
@@ -1,3 +1,4 @@
+mod aws;
mod character;
mod cmd_duration;
mod common;