Skip to content

Commit 67e18c6

Browse files
committed
Improve command execution
1 parent 47b6965 commit 67e18c6

File tree

10 files changed

+164
-167
lines changed

10 files changed

+164
-167
lines changed

flake.lock

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cli.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ pub struct Opts {
121121

122122
impl Opts {
123123
pub async fn run(&self) -> Result<()> {
124-
let workspace = Workspace {
125-
path: self.workspace_path.clone(),
126-
};
124+
let workspace = Workspace::load(&self.workspace_path)?;
127125
println!("Using workspace at {}", workspace.path.display());
128126
match self.command.clone() {
129127
SubCommand::Init => {

src/config.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ impl<T> std::ops::DerefMut for WithPath<T> {
4444
}
4545
}
4646

47-
#[derive(Debug, Default, Serialize, Deserialize)]
47+
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
4848
pub struct Config {
4949
pub rpc_endpoints: RPC,
50+
pub upgrade_authority_keypair: Option<String>,
5051
}
5152

5253
#[derive(Clone, Debug, Serialize, Deserialize)]
@@ -104,13 +105,15 @@ impl Config {
104105
}
105106

106107
#[derive(Debug, Serialize, Deserialize)]
107-
struct _Config {
108+
struct RawConfig {
108109
rpc_endpoints: Option<RPC>,
110+
upgrade_authority_keypair: Option<String>,
109111
}
110112

111113
impl ToString for Config {
112114
fn to_string(&self) -> String {
113-
let cfg = _Config {
115+
let cfg = RawConfig {
116+
upgrade_authority_keypair: self.upgrade_authority_keypair.clone(),
114117
rpc_endpoints: Some(RPC {
115118
..self.rpc_endpoints.clone()
116119
}),
@@ -124,10 +127,11 @@ impl FromStr for Config {
124127
type Err = Error;
125128

126129
fn from_str(s: &str) -> Result<Self, Self::Err> {
127-
let cfg: _Config = toml::from_str(s)
130+
let cfg: RawConfig = toml::from_str(s)
128131
.map_err(|e| anyhow::format_err!("Unable to deserialize config: {}", e.to_string()))?;
129132
Ok(Config {
130133
rpc_endpoints: cfg.rpc_endpoints.unwrap_or_default(),
134+
upgrade_authority_keypair: cfg.upgrade_authority_keypair,
131135
})
132136
}
133137
}

src/solana_cmd.rs

Lines changed: 96 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Calls to the `solana` CLI.
2+
23
use anchor_client::Cluster;
34
use anyhow::Result;
45
use solana_sdk::pubkey::Pubkey;
@@ -7,105 +8,106 @@ use std::{
78
process::{Command, Output},
89
};
910

10-
use crate::{
11-
utils::{exec_command, get_cluster_url},
12-
workspace::Workspace,
13-
};
11+
use crate::{utils::exec_command, workspace::Workspace};
1412

15-
/// Sets the buffer authority of a buffer.
16-
pub fn set_buffer_authority(
17-
workspace: &Workspace,
18-
cluster: &Cluster,
19-
buffer_key: &Pubkey,
20-
authority: &str,
21-
) -> Result<Output> {
22-
workspace.exec_deployer_command(cluster, |cmd: &mut Command| {
23-
cmd.arg("program")
24-
.arg("set-buffer-authority")
25-
.arg(buffer_key.to_string())
26-
.arg("--new-buffer-authority")
27-
.arg(authority);
28-
Ok(())
29-
})
30-
}
13+
impl Workspace {
14+
/// Sets the buffer authority of a buffer.
15+
pub fn set_buffer_authority(
16+
&self,
17+
cluster: &Cluster,
18+
buffer_key: &Pubkey,
19+
authority: &str,
20+
) -> Result<Output> {
21+
self.exec_deployer_command(cluster, |cmd: &mut Command| {
22+
cmd.arg("program")
23+
.arg("set-buffer-authority")
24+
.arg(buffer_key.to_string())
25+
.arg("--new-buffer-authority")
26+
.arg(authority);
27+
Ok(())
28+
})
29+
}
3130

32-
/// Sets the upgrade authority of a program.
33-
pub fn set_upgrade_authority(
34-
cluster: &Cluster,
35-
program_id: &Pubkey,
36-
current_authority: &Path,
37-
new_authority: &str,
38-
) -> Result<Output> {
39-
exec_command(
40-
std::process::Command::new("solana")
41-
.arg("--url")
42-
.arg(get_cluster_url(cluster)?)
43-
.arg("--keypair")
44-
.arg(current_authority)
45-
.arg("program")
46-
.arg("set-upgrade-authority")
47-
.arg(program_id.to_string())
48-
.arg("--new-upgrade-authority")
49-
.arg(new_authority),
50-
)
51-
}
31+
/// Sets the upgrade authority of a program.
32+
pub fn set_upgrade_authority(
33+
&self,
34+
cluster: &Cluster,
35+
program_id: &Pubkey,
36+
current_authority: &Path,
37+
new_authority: &str,
38+
) -> Result<Output> {
39+
exec_command(
40+
std::process::Command::new("solana")
41+
.arg("--url")
42+
.arg(self.get_cluster_url(cluster)?)
43+
.arg("--keypair")
44+
.arg(current_authority)
45+
.arg("program")
46+
.arg("set-upgrade-authority")
47+
.arg(program_id.to_string())
48+
.arg("--new-upgrade-authority")
49+
.arg(new_authority),
50+
)
51+
}
5252

53-
/// Writes a program buffer.
54-
pub fn write_buffer(
55-
workspace: &Workspace,
56-
cluster: &Cluster,
57-
program_file: &Path,
58-
buffer_kp_file: &Path,
59-
) -> Result<Output> {
60-
workspace.exec_deployer_command(cluster, |cmd| {
61-
cmd.arg("program")
62-
.arg("write-buffer")
63-
.arg(program_file)
64-
.arg("--buffer")
65-
.arg(buffer_kp_file);
66-
Ok(())
67-
})
68-
}
53+
/// Writes a program buffer.
54+
pub fn write_buffer(
55+
&self,
56+
cluster: &Cluster,
57+
program_file: &Path,
58+
buffer_kp_file: &Path,
59+
) -> Result<Output> {
60+
self.exec_deployer_command(cluster, |cmd| {
61+
cmd.arg("program")
62+
.arg("write-buffer")
63+
.arg(program_file)
64+
.arg("--buffer")
65+
.arg(buffer_kp_file);
66+
Ok(())
67+
})
68+
}
6969

70-
/// Deploys a program.
71-
pub fn deploy(
72-
workspace: &Workspace,
73-
cluster: &Cluster,
74-
program_file: &Path,
75-
program_kp_path: &Path,
76-
) -> Result<Output> {
77-
workspace.exec_deployer_command(cluster, |cmd| {
78-
cmd.arg("program")
79-
.arg("deploy")
80-
.arg("--program-id")
81-
.arg(program_kp_path)
82-
.arg(program_file);
83-
Ok(())
84-
})
85-
}
70+
/// Deploys a program.
71+
pub fn deploy(
72+
&self,
73+
cluster: &Cluster,
74+
program_file: &Path,
75+
program_kp_path: &Path,
76+
) -> Result<Output> {
77+
self.exec_deployer_command(cluster, |cmd| {
78+
cmd.arg("program")
79+
.arg("deploy")
80+
.arg("--program-id")
81+
.arg(program_kp_path)
82+
.arg(program_file);
83+
Ok(())
84+
})
85+
}
8686

87-
/// Upgrades a program.
88-
pub fn upgrade(
89-
cluster: &Cluster,
90-
upgrade_authority_kp: &str,
91-
buffer_key: &Pubkey,
92-
program_id: &str,
93-
) -> Result<Output> {
94-
exec_command(
95-
std::process::Command::new("solana")
96-
.arg("--url")
97-
.arg(get_cluster_url(cluster)?)
98-
.arg("--keypair")
99-
.arg(upgrade_authority_kp)
100-
.arg("program")
101-
.arg("deploy")
102-
.arg("--buffer")
103-
.arg(buffer_key.to_string())
104-
.arg("--program-id")
105-
.arg(program_id)
106-
.arg("--upgrade-authority")
107-
.arg(upgrade_authority_kp),
108-
)
87+
/// Upgrades a program.
88+
pub fn upgrade(
89+
&self,
90+
cluster: &Cluster,
91+
upgrade_authority_kp: &str,
92+
buffer_key: &Pubkey,
93+
program_id: &str,
94+
) -> Result<Output> {
95+
exec_command(
96+
std::process::Command::new("solana")
97+
.arg("--url")
98+
.arg(self.get_cluster_url(cluster)?)
99+
.arg("--keypair")
100+
.arg(upgrade_authority_kp)
101+
.arg("program")
102+
.arg("deploy")
103+
.arg("--buffer")
104+
.arg(buffer_key.to_string())
105+
.arg("--program-id")
106+
.arg(program_id)
107+
.arg("--upgrade-authority")
108+
.arg(upgrade_authority_kp),
109+
)
110+
}
109111
}
110112

111113
pub fn new_solana_cmd() -> Command {

src/subcommands/deploy.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use std::path::Path;
99
use std::str::FromStr;
1010
use tempfile::NamedTempFile;
1111

12-
use crate::solana_cmd;
1312
use crate::utils::sha256_digest;
1413
use crate::{location::fetch_program_file, workspace::Workspace};
1514

@@ -48,8 +47,8 @@ pub async fn process(
4847
println!("Size (bytes): {}", program_file_size.to_string().green());
4948
println!("SHA256: {}", program_file_digest.green());
5049

51-
solana_cmd::deploy(workspace, &cluster, program_file.path(), program_kp_path)?;
52-
solana_cmd::set_upgrade_authority(
50+
workspace.deploy(&cluster, program_file.path(), program_kp_path)?;
51+
workspace.set_upgrade_authority(
5352
&cluster,
5453
&program_kp.pubkey(),
5554
&deployer_kp_path,

src/subcommands/init.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::fs::File;
66
use std::io::Write;
77
use std::{fs, path::Path};
88

9-
use crate::utils::{exec_command, gen_keypair_file, get_cluster_url};
9+
use crate::utils::{exec_command, gen_keypair_file};
1010
use crate::{config::Config, workspace::Workspace};
1111

1212
pub fn process(workspace: &Workspace) -> Result<()> {
@@ -41,14 +41,16 @@ pub fn process(workspace: &Workspace) -> Result<()> {
4141
println!("{}: {}", cluster, key);
4242
}
4343

44+
let workspace = workspace.reload()?;
45+
4446
for (cluster, _key) in result.iter() {
4547
if cluster.clone() != Cluster::Mainnet {
4648
let path_string = format!(".goki/deployers/{}.json", cluster);
4749
let keypair_path = Path::new(path_string.as_str());
4850
exec_command(
4951
std::process::Command::new("solana")
5052
.arg("--url")
51-
.arg(get_cluster_url(cluster)?)
53+
.arg(workspace.get_cluster_url(cluster)?)
5254
.arg("--keypair")
5355
.arg(keypair_path)
5456
.arg("airdrop")

0 commit comments

Comments
 (0)