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

cli: Add ability to force init and new commands via cli #2698

Merged
merged 16 commits into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- syn: Add missing `new_from_array` method to `Hash` ([#2682](https://github.com/coral-xyz/anchor/pull/2682)).
- cli: Switch to Cargo feature resolver(`resolver = "2"`) ([#2676](https://github.com/coral-xyz/anchor/pull/2676)).
- cli: Fix using user specific path for `provider.wallet` in `Anchor.toml` ([#2696](https://github.com/coral-xyz/anchor/pull/2696)).
- cli: Allow force `init` and `new` ([#2698](https://github.com/coral-xyz/anchor/pull/2698)).

### Breaking

Expand Down
20 changes: 17 additions & 3 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ pub enum Command {
/// Rust program template to use
#[clap(value_enum, short, long, default_value = "single")]
template: ProgramTemplate,
/// Initialize even if there are files
#[clap(long, action)]
force: bool,
},
/// Builds the workspace.
#[clap(name = "build", alias = "b")]
Expand Down Expand Up @@ -225,6 +228,9 @@ pub enum Command {
/// Rust program template to use
#[clap(value_enum, short, long, default_value = "single")]
template: ProgramTemplate,
/// Create new program even if there is already one
#[clap(long, action)]
force: bool,
},
/// Commands for interacting with interface definitions.
Idl {
Expand Down Expand Up @@ -592,6 +598,7 @@ fn process_command(opts: Opts) -> Result<()> {
no_git,
jest,
template,
force,
} => init(
&opts.cfg_override,
name,
Expand All @@ -600,12 +607,14 @@ fn process_command(opts: Opts) -> Result<()> {
no_git,
jest,
template,
force,
),
Command::New {
solidity,
name,
template,
} => new(&opts.cfg_override, solidity, name, template),
force,
} => new(&opts.cfg_override, solidity, name, template, force),
Command::Build {
idl,
idl_ts,
Expand Down Expand Up @@ -753,8 +762,9 @@ fn init(
no_git: bool,
jest: bool,
template: ProgramTemplate,
force: bool,
) -> Result<()> {
if Config::discover(cfg_override)?.is_some() {
if !force && Config::discover(cfg_override)?.is_some() {
return Err(anyhow!("Workspace already initialized"));
}

Expand Down Expand Up @@ -927,6 +937,7 @@ fn new(
solidity: bool,
name: String,
template: ProgramTemplate,
force: bool,
) -> Result<()> {
with_workspace(cfg_override, |cfg| {
match cfg.path().parent() {
Expand All @@ -938,7 +949,7 @@ fn new(

let cluster = cfg.provider.cluster.clone();
let programs = cfg.programs.entry(cluster).or_default();
if programs.contains_key(&name) {
if !force && programs.contains_key(&name) {
acheroncrypto marked this conversation as resolved.
Show resolved Hide resolved
return Err(anyhow!("Program already exists"));
}

Expand Down Expand Up @@ -4416,6 +4427,7 @@ mod tests {
false,
false,
ProgramTemplate::default(),
false,
)
.unwrap();
}
Expand All @@ -4434,6 +4446,7 @@ mod tests {
false,
false,
ProgramTemplate::default(),
false,
)
.unwrap();
}
Expand All @@ -4452,6 +4465,7 @@ mod tests {
false,
false,
ProgramTemplate::default(),
false,
)
.unwrap();
}
Expand Down