MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / run_with_verifier

Method run_with_verifier

atomic-cli/src/commands/workspace/set.rs:101–177  ·  view source on GitHub ↗

Inner runner that takes the slug-verification step as a parameter. Splitting this out lets tests inject a deterministic verifier (succeed / fail) without making a real network call, so we can assert the load-bearing invariant: **when verification fails, the config file is not modified**.

(&self, verify: F)

Source from the content-addressed store, hash-verified

99 /// assert the load-bearing invariant: **when verification fails, the
100 /// config file is not modified**.
101 fn run_with_verifier<F>(&self, verify: F) -> CliResult<()>
102 where
103 F: FnOnce(&str, &str) -> CliResult<()>,
104 {
105 if self.slug.is_empty() {
106 return Err(CliError::InvalidArgument {
107 message: "Workspace slug cannot be empty.".to_string(),
108 });
109 }
110
111 let mut config = GlobalConfig::load().map_err(|e| {
112 CliError::Internal(anyhow::anyhow!("Failed to load global config: {e}"))
113 })?;
114
115 // Determine the target org. Explicit --org wins; otherwise fall
116 // back to the configured default. We don't go through `resolve_org`
117 // here because we want a distinct error message tied to *this*
118 // command's flow.
119 let target_org = match self.org.as_deref() {
120 Some(o) if !o.is_empty() => o.to_string(),
121 Some(_) => {
122 return Err(CliError::InvalidArgument {
123 message: "Organization slug cannot be empty.".to_string(),
124 });
125 }
126 None => config
127 .server
128 .default_org
129 .clone()
130 .ok_or_else(|| CliError::InvalidArgument {
131 message: "No default org set. Use --org or first run: \
132 atomic org set <slug>"
133 .to_string(),
134 })?,
135 };
136
137 // Validate against the server first so a bad slug fails fast
138 // rather than producing confusing 404s on subsequent commands.
139 // The `?` here is load-bearing: if it returns Err, the mutation
140 // and save below never execute.
141 if !self.no_verify {
142 verify(&target_org, &self.slug)?;
143 }
144
145 let previous = config
146 .server
147 .default_workspaces
148 .insert(target_org.clone(), self.slug.clone());
149
150 config.save().map_err(|e| {
151 CliError::Internal(anyhow::anyhow!("Failed to save global config: {e}"))
152 })?;
153
154 print_success(&format!(
155 "Default workspace for '{}' set to: {}",
156 target_org, self.slug
157 ));
158

Calls 7

print_successFunction · 0.85
print_hintFunction · 0.85
as_refMethod · 0.80
is_emptyMethod · 0.45
cloneMethod · 0.45
insertMethod · 0.45
saveMethod · 0.45

Tested by

no test coverage detected