Skip to content

Commit

Permalink
(#19) Add colours to successful outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Jul 24, 2020
1 parent 5020375 commit db8fdf0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ gctx delete my-config
gctx --help
```

## Output

`gctx` auto-detects terminal capabilities by default and supports the [`NO_COLOR`](https://no-color.org/) and
[`CLICOLOR`](https://bixense.com/clicolors/) standards as provided by the [`colored`](https://crates.io/crates/colored)
crate. For example:

```bash
# will have colors if the terminal supports them
gctx list

# force no colors on output
NO_COLOR=1 gctx list
CLICOLOR=0 gctx list

# force colors on output
CLICOLOR_FORCE=1 gctx list
```

## Motivation

I'm often working with multiple GCP projects with a variety of different settings (e.g. default compute zone).However,
Expand Down
1 change: 1 addition & 0 deletions gctx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ categories = ["command-line-utilities", "config"]
[dependencies]
anyhow = "1"
clap = "3.0.0-beta.1"
colored = "2"
gcloud-ctx = { path = "../gcloud-ctx", version = "0.3" }
which = "4"

Expand Down
49 changes: 30 additions & 19 deletions gctx/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{Context, Result};
use colored::*;
use gcloud_ctx::{ConfigurationStore, ConflictAction, PropertiesBuilder};

/// Used to control whether to activate a configuration after creation
Expand All @@ -23,23 +24,25 @@ impl From<bool> for PostCreation {

/// List the available configurations with an indicator of the active one
pub fn list() -> Result<()> {
let store = ConfigurationStore::with_default_location().context("Opening configuration store")?;
let store = ConfigurationStore::with_default_location()?;

for config in store.configurations() {
let prefix = if store.is_active(config) { "* " } else { " " };

println!("{}{}", prefix, config.name());
if store.is_active(config) {
println!("{} {}", "*".blue(), config.name().blue());
} else {
println!(" {}", config.name());
}
}

Ok(())
}

/// Activate the given configuration by name
pub fn activate(name: &str) -> Result<()> {
let mut store = ConfigurationStore::with_default_location().context("Opening configuration store")?;
let mut store = ConfigurationStore::with_default_location()?;
store.activate(name)?;

println!("Successfully activated '{}'", name);
println!("Successfully activated '{}'", name.blue());

Ok(())
}
Expand All @@ -49,11 +52,15 @@ pub fn copy(src_name: &str, dest_name: &str, conflict: ConflictAction, activate:
let mut store = ConfigurationStore::with_default_location()?;
store.copy(src_name, dest_name, conflict)?;

println!("Successfully copied configuration '{}' to '{}'", src_name, dest_name);
println!(
"Successfully copied configuration '{}' to '{}'",
src_name.yellow(),
dest_name.blue()
);

if activate == PostCreation::Activate {
store.activate(dest_name)?;
println!("Configuration '{}' is now active", dest_name);
println!("Configuration '{}' is now active", dest_name.blue());
}

Ok(())
Expand All @@ -69,7 +76,7 @@ pub fn create(
conflict: ConflictAction,
activate: PostCreation,
) -> Result<()> {
let mut store = ConfigurationStore::with_default_location().context("Opening configuration store")?;
let mut store = ConfigurationStore::with_default_location()?;
let mut builder = PropertiesBuilder::default();

builder.project(project).account(account).zone(zone);
Expand All @@ -82,35 +89,35 @@ pub fn create(

store.create(name, &properties, conflict)?;

println!("Successfully created configuration '{}'", name);
println!("Successfully created configuration '{}'", name.blue());

if activate == PostCreation::Activate {
store.activate(name)?;
println!("Configuration '{}' is now active", name);
println!("Configuration '{}' is now active", name.blue());
}

Ok(())
}

/// Show the current activated configuration
pub fn current() -> Result<()> {
let store = ConfigurationStore::with_default_location().context("Opening configuration store")?;
println!("{}", store.active());
let store = ConfigurationStore::with_default_location()?;
println!("{}", store.active().blue());
Ok(())
}

/// Delete a configuration
pub fn delete(name: &str) -> Result<()> {
let mut store = ConfigurationStore::with_default_location().context("Opening configuration store")?;
let mut store = ConfigurationStore::with_default_location()?;
store.delete(name)?;

println!("Successfully deleted configuration '{}'", name);
println!("Successfully deleted configuration '{}'", name.yellow());
Ok(())
}

/// Describe all the properties in the given configuration
pub fn describe(name: Option<&str>) -> Result<()> {
let store = ConfigurationStore::with_default_location().context("Opening configuration store")?;
let store = ConfigurationStore::with_default_location()?;
let name = name.unwrap_or_else(|| store.active());
let properties = store.describe(name)?;

Expand All @@ -123,14 +130,18 @@ pub fn describe(name: Option<&str>) -> Result<()> {

/// Rename a configuration
pub fn rename(old_name: &str, new_name: &str, conflict: ConflictAction) -> Result<()> {
let mut store = ConfigurationStore::with_default_location().context("Opening configuration store")?;
let mut store = ConfigurationStore::with_default_location()?;
store.rename(old_name, new_name, conflict)?;

println!("Successfully renamed configuration '{}' to '{}'", old_name, new_name);
println!(
"Successfully renamed configuration '{}' to '{}'",
old_name.yellow(),
new_name.blue()
);

if let Some(configuration) = store.find_by_name(new_name) {
if store.is_active(configuration) {
println!("Configuration '{}' is now active", new_name);
println!("Configuration '{}' is now active", new_name.blue());
}
}

Expand Down

0 comments on commit db8fdf0

Please sign in to comment.