Skip to content

Commit

Permalink
Expose parts of the CLI implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vigoo committed Aug 9, 2024
1 parent 0f9719e commit 9c4b18b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,61 @@ use std::env;

#[derive(Args, Debug)]
#[group(required = true, multiple = false)]
struct NameOrLanguage {
pub struct NameOrLanguage {
/// Name of the example to use
#[arg(short, long, group = "ex")]
example: Option<ExampleName>,

/// Language to use for it's default example
#[arg(short, long, alias = "lang", group = "ex")]
language: Option<GuestLanguage>,
}

impl NameOrLanguage {
/// Gets the selected example's name
pub fn example_name(&self) -> ExampleName {
self.example
.clone()
.unwrap_or(ExampleName::from_string(format!(
"{}-default",
self.language.clone().unwrap_or(GuestLanguage::Rust).id()
)))
}
}

#[derive(Subcommand, Debug)]
#[command()]
enum Command {
pub enum Command {
/// Create a new Golem component from built-in examples
#[command()]
New {
#[command(flatten)]
name_or_language: NameOrLanguage,

/// The package name of the generated component (in namespace:name format)
#[arg(short, long)]
package_name: Option<PackageName>,

/// The new component's name
component_name: ComponentName,
},

/// Lists the built-in examples available for creating new components
#[command()]
ListExamples {
/// The minimum language tier to include in the list
#[arg(short, long)]
min_tier: Option<GuestLanguageTier>,

/// Filter examples by a given guest language
#[arg(short, long, alias = "lang")]
language: Option<GuestLanguage>,
},
}

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None, rename_all = "kebab-case")]
struct GolemCommand {
pub struct GolemCommand {
#[command(subcommand)]
command: Command,
}
Expand All @@ -53,18 +74,7 @@ pub fn main() {
component_name,
package_name,
} => {
let example_name =
name_or_language
.example
.clone()
.unwrap_or(ExampleName::from_string(format!(
"{}-default",
name_or_language
.language
.clone()
.unwrap_or(GuestLanguage::Rust)
.id()
)));
let example_name = name_or_language.example_name();
let examples = GolemExamples::list_all_examples();
let example = examples.iter().find(|example| example.name == example_name);
match example {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::io::Write;
use std::path::{Path, PathBuf};
use std::{fs, io};

pub mod cli;
pub mod model;

pub trait Examples {
Expand Down

0 comments on commit 9c4b18b

Please sign in to comment.