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

fix: #1229 #1232

Merged
merged 1 commit into from
Mar 1, 2024
Merged
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
77 changes: 30 additions & 47 deletions cmd/soroban-cli/src/commands/contract/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ use clap::{
};
use gix::{clone, create, open, progress, remote};
use rust_embed::RustEmbed;
use serde::Deserialize;
use serde_json::{from_str, json, Error as JsonError, Value as JsonValue};
use toml_edit::{Document, Formatted, InlineTable, Item, TomlError, Value as TomlValue};
use ureq::{get, Error as UreqError};

const SOROBAN_EXAMPLES_URL: &str = "https://github.com/stellar/soroban-examples.git";
const GITHUB_URL: &str = "https://github.com";
const GITHUB_API_URL: &str =
"https://api.github.com/repos/stellar/soroban-examples/git/trees/main?recursive=1";

#[derive(Clone, Debug, ValueEnum, PartialEq)]
pub enum FrontendTemplate {
Expand All @@ -48,14 +45,36 @@ pub struct Cmd {
}

fn possible_example_values() -> ValueParser {
// If fetching the example contracts from the soroban-examples repo succeeds, return a parser with the example contracts.
if let Ok(examples) = get_valid_examples() {
let parser = PossibleValuesParser::new(examples.iter().map(PossibleValue::new));
return parser.into();
}

// If fetching with example contracts fails, return a string parser that will allow for any value. It will be ignored in `init`.
ValueParser::string()
let parser = PossibleValuesParser::new(
[
"account",
"alloc",
"atomic_multiswap",
"atomic_swap",
"auth",
"cross_contract",
"custom_types",
"deep_contract_auth",
"deployer",
"errors",
"eth_abi",
"events",
"fuzzing",
"increment",
"liquidity_pool",
"logging",
"mint-lock",
"simple_account",
"single_offer",
"timelock",
"token",
"upgradeable_contract",
"workspace",
]
.iter()
.map(PossibleValue::new),
);
parser.into()
}

fn with_example_help() -> String {
Expand All @@ -66,42 +85,6 @@ fn with_example_help() -> String {
}
}

#[derive(Deserialize, Debug)]
struct RepoPath {
path: String,
#[serde(rename = "type")]
type_field: String,
}

#[derive(Deserialize, Debug)]
struct ReqBody {
tree: Vec<RepoPath>,
}

fn get_valid_examples() -> Result<Vec<String>, Error> {
let body: ReqBody = get(GITHUB_API_URL)
.call()
.map_err(|e| {
tracing::warn!("Error fetching example contracts from soroban-examples repo");
Box::new(e)
})?
.into_json()?;
let mut valid_examples = Vec::new();
for item in body.tree {
if item.type_field == "blob"
|| item.path.starts_with('.')
|| item.path.contains('/')
|| item.path == "hello_world"
{
continue;
}

valid_examples.push(item.path);
}

Ok(valid_examples)
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("Io error: {0}")]
Expand Down
Loading