Explorar o código

cli: Add `idl convert` command (#3009)

acheron hai 1 ano
pai
achega
b7511837b6
Modificáronse 3 ficheiros con 21 adicións e 1 borrados
  1. 1 0
      CHANGELOG.md
  2. 1 1
      cli/Cargo.toml
  3. 19 0
      cli/src/lib.rs

+ 1 - 0
CHANGELOG.md

@@ -23,6 +23,7 @@ The minor version will be incremented upon a breaking change and the patch versi
 - cli: Add additional solana arguments to the `upgrade` command ([#2998](https://github.com/coral-xyz/anchor/pull/2998)).
 - spl: Export `spl-associated-token-account` crate ([#2999](https://github.com/coral-xyz/anchor/pull/2999)).
 - lang: Support legacy IDLs with `declare_program!` ([#2997](https://github.com/coral-xyz/anchor/pull/2997)).
+- cli: Add `idl convert` command ([#3009](https://github.com/coral-xyz/anchor/pull/3009)).
 
 ### Fixes
 

+ 1 - 1
cli/Cargo.toml

@@ -16,7 +16,7 @@ dev = []
 
 [dependencies]
 anchor-client = { path = "../client", version = "0.30.0" }
-anchor-lang-idl = { path = "../idl", features = ["build"], version = "0.1.0" }
+anchor-lang-idl = { path = "../idl", version = "0.1.0", features = ["build", "convert"] }
 anchor-lang = { path = "../lang", version = "0.30.0" }
 anyhow = "1.0.32"
 base64 = "0.21"

+ 19 - 0
cli/src/lib.rs

@@ -488,6 +488,14 @@ pub enum IdlCommand {
         #[clap(short, long)]
         out: Option<String>,
     },
+    /// Convert legacy IDLs (pre Anchor 0.30) to the new IDL spec
+    Convert {
+        /// Path to the IDL file
+        path: String,
+        /// Output file for the idl (stdout if not specified)
+        #[clap(short, long)]
+        out: Option<String>,
+    },
 }
 
 #[derive(Debug, Parser)]
@@ -2200,6 +2208,7 @@ fn idl(cfg_override: &ConfigOverride, subcmd: IdlCommand) -> Result<()> {
             skip_lint,
         } => idl_build(cfg_override, program_name, out, out_ts, no_docs, skip_lint),
         IdlCommand::Fetch { address, out } => idl_fetch(cfg_override, address, out),
+        IdlCommand::Convert { path, out } => idl_convert(path, out),
     }
 }
 
@@ -2714,6 +2723,16 @@ fn idl_fetch(cfg_override: &ConfigOverride, address: Pubkey, out: Option<String>
     write_idl(&idl, out)
 }
 
+fn idl_convert(path: String, out: Option<String>) -> Result<()> {
+    let idl = fs::read(path)?;
+    let idl = Idl::from_slice_with_conversion(&idl)?;
+    let out = match out {
+        None => OutFile::Stdout,
+        Some(out) => OutFile::File(PathBuf::from(out)),
+    };
+    write_idl(&idl, out)
+}
+
 fn write_idl(idl: &Idl, out: OutFile) -> Result<()> {
     let idl_json = serde_json::to_string_pretty(idl)?;
     match out {