Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --json flag to generate command #87

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 additions & 11 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ serde_yaml = "0.8"
shell-words = "1.0"
shellexpand = "2.0"
toml = "0.5"
serde_json = "1.0.128"
1 change: 1 addition & 0 deletions nix/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ rustPlatform.buildRustPackage {
homepage = manifest.repository;
license = lib.licenses.mit;
platforms = lib.platforms.all;
mainProgram = "flavours";
};
}
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ pub fn build_cli() -> App<'static> {
.about("Outputs scheme to stdout instead of writing it to a file.")
.long("stdout")
)
.arg(
Arg::new("json")
.about("Outputs scheme in JSON format rather than YAML.")
.long("json")
)
)
.subcommand(
App::new("apply")
Expand Down
19 changes: 15 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ fn main() -> Result<()> {
}?;

let to_stdout = sub_matches.is_present("stdout");
let to_json = sub_matches.is_present("json");

let colors = generate::generate(&image, mode, verbose)?;
let scheme = Scheme {
Expand All @@ -194,7 +195,11 @@ fn main() -> Result<()> {
};

if to_stdout {
print!("{}", serde_yaml::to_string(&scheme)?);
if !to_json {
print!("{}", serde_yaml::to_string(&scheme)?);
} else {
print!("{}", serde_json::to_string(&scheme)?);
}
} else {
let path = flavours_dir
.join("base16")
Expand All @@ -204,9 +209,15 @@ fn main() -> Result<()> {
create_dir_all(&path)
.with_context(|| format!("Couldn't create directory {:?}", &path))?;
}
let file_path = &path.join(format!("{}.yaml", &scheme.slug));
write(file_path, serde_yaml::to_string(&scheme)?)
.with_context(|| format!("Couldn't write scheme file at {:?}", path))?;
if !to_json {
let file_path = &path.join(format!("{}.yaml", &scheme.slug));
write(file_path, serde_yaml::to_string(&scheme)?)
.with_context(|| format!("Couldn't write scheme file at {:?}", path))?;
} else {
let file_path = &path.join(format!("{}.json", &scheme.slug));
write(file_path, serde_json::to_string(&scheme)?)
.with_context(|| format!("Couldn't write scheme file at {:?}", path))?;
}
}
Ok(())
}
Expand Down