Skip to content

Commit

Permalink
fix: #1229
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed Mar 1, 2024
1 parent 4b3fac8 commit e239c38
Showing 1 changed file with 30 additions and 47 deletions.
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

0 comments on commit e239c38

Please sign in to comment.