-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from golemcloud/vigoo/expose-cli-module
Expose parts of the CLI implementation
- Loading branch information
Showing
5 changed files
with
90 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,64 @@ | ||
use clap::*; | ||
use golem_examples::model::{ | ||
ComponentName, ExampleName, ExampleParameters, GuestLanguage, GuestLanguageTier, PackageName, | ||
}; | ||
use golem_examples::*; | ||
use std::env; | ||
|
||
use crate::model::{ComponentName, ExampleName, GuestLanguage, GuestLanguageTier, PackageName}; | ||
|
||
#[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>, | ||
pub example: Option<ExampleName>, | ||
|
||
/// Language to use for it's default example | ||
#[arg(short, long, alias = "lang", group = "ex")] | ||
language: Option<GuestLanguage>, | ||
pub 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, | ||
} | ||
|
||
pub fn main() { | ||
let command: GolemCommand = GolemCommand::parse(); | ||
match &command.command { | ||
Command::New { | ||
name_or_language, | ||
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 examples = GolemExamples::list_all_examples(); | ||
let example = examples.iter().find(|example| example.name == example_name); | ||
match example { | ||
Some(example) => { | ||
let cwd = env::current_dir().expect("Failed to get current working directory"); | ||
match GolemExamples::instantiate( | ||
example, | ||
ExampleParameters { | ||
component_name: component_name.clone(), | ||
package_name: package_name | ||
.clone() | ||
.unwrap_or(PackageName::from_string("golem:component").unwrap()), | ||
target_path: cwd, | ||
}, | ||
) { | ||
Ok(instructions) => println!("{instructions}"), | ||
Err(err) => eprintln!("Failed to instantiate example: {err}"), | ||
} | ||
} | ||
None => { | ||
eprintln!("Unknown example {example_name}. Use the list-examples command to see the available commands."); | ||
} | ||
} | ||
} | ||
Command::ListExamples { min_tier, language } => { | ||
GolemExamples::list_all_examples() | ||
.iter() | ||
.filter(|example| match language { | ||
Some(language) => example.language == *language, | ||
None => true, | ||
}) | ||
.filter(|example| match min_tier { | ||
Some(min_tier) => example.language.tier() <= *min_tier, | ||
None => true, | ||
}) | ||
.for_each(|example| println!("{:?}", example)); | ||
} | ||
} | ||
pub command: Command, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use clap::Parser; | ||
use golem_examples::cli::*; | ||
use golem_examples::model::*; | ||
use golem_examples::{Examples, GolemExamples}; | ||
use std::env; | ||
|
||
pub fn main() { | ||
let command: GolemCommand = GolemCommand::parse(); | ||
match &command.command { | ||
Command::New { | ||
name_or_language, | ||
component_name, | ||
package_name, | ||
} => { | ||
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 { | ||
Some(example) => { | ||
let cwd = env::current_dir().expect("Failed to get current working directory"); | ||
match GolemExamples::instantiate( | ||
example, | ||
ExampleParameters { | ||
component_name: component_name.clone(), | ||
package_name: package_name | ||
.clone() | ||
.unwrap_or(PackageName::from_string("golem:component").unwrap()), | ||
target_path: cwd, | ||
}, | ||
) { | ||
Ok(instructions) => println!("{instructions}"), | ||
Err(err) => eprintln!("Failed to instantiate example: {err}"), | ||
} | ||
} | ||
None => { | ||
eprintln!("Unknown example {example_name}. Use the list-examples command to see the available commands."); | ||
} | ||
} | ||
} | ||
Command::ListExamples { min_tier, language } => { | ||
GolemExamples::list_all_examples() | ||
.iter() | ||
.filter(|example| match language { | ||
Some(language) => example.language == *language, | ||
None => true, | ||
}) | ||
.filter(|example| match min_tier { | ||
Some(min_tier) => example.language.tier() <= *min_tier, | ||
None => true, | ||
}) | ||
.for_each(|example| println!("{:?}", example)); | ||
} | ||
} | ||
} |