Skip to content

Commit 676709d

Browse files
committed
up: Move AppSource into its own module
Signed-off-by: Lann Martin <[email protected]>
1 parent ed4e13f commit 676709d

File tree

2 files changed

+70
-63
lines changed

2 files changed

+70
-63
lines changed

src/commands/up.rs

Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
mod app_source;
2+
13
use std::{
24
collections::HashSet,
35
ffi::OsString,
4-
fmt::{Debug, Display},
6+
fmt::Debug,
57
path::{Path, PathBuf},
68
};
79

@@ -16,6 +18,8 @@ use tempfile::TempDir;
1618

1719
use crate::opts::*;
1820

21+
use self::app_source::AppSource;
22+
1923
const APPLICATION_OPT: &str = "APPLICATION";
2024

2125
/// Start the Fermyon runtime.
@@ -243,8 +247,8 @@ impl UpCommand {
243247
fn resolve_app_source(&self) -> AppSource {
244248
match (&self.app_source, &self.file_source, &self.registry_source) {
245249
(None, None, None) => self.default_manifest_or_none(),
246-
(Some(source), None, None) => Self::infer_source(source),
247-
(None, Some(file), None) => Self::infer_file_source(file.to_owned()),
250+
(Some(source), None, None) => AppSource::infer_source(source),
251+
(None, Some(file), None) => AppSource::infer_file_source(file.to_owned()),
248252
(None, None, Some(reference)) => AppSource::OciRegistry(reference.to_owned()),
249253
_ => AppSource::unresolvable("More than one application source was specified"),
250254
}
@@ -265,24 +269,6 @@ impl UpCommand {
265269
}
266270
}
267271

268-
fn infer_source(source: &str) -> AppSource {
269-
let path = PathBuf::from(source);
270-
if path.exists() {
271-
Self::infer_file_source(path)
272-
} else if spin_oci::is_probably_oci_reference(source) {
273-
AppSource::OciRegistry(source.to_owned())
274-
} else {
275-
AppSource::Unresolvable(format!("File or directory '{source}' not found. If you meant to load from a registry, use the `--from-registry` option."))
276-
}
277-
}
278-
279-
fn infer_file_source(path: impl Into<PathBuf>) -> AppSource {
280-
match spin_common::paths::resolve_manifest_file_path(path.into()) {
281-
Ok(file) => AppSource::File(file),
282-
Err(e) => AppSource::Unresolvable(e.to_string()),
283-
}
284-
}
285-
286272
fn trigger_args_look_file_like(&self) -> bool {
287273
// Heuristic for the user typing `spin up foo` instead of `spin up -f foo` - in the
288274
// first case `foo` gets interpreted as a trigger arg which is probably not what the
@@ -430,50 +416,10 @@ fn trigger_command_from_locked_app(locked_app: &LockedApp) -> Result<Vec<String>
430416
}
431417
}
432418

433-
#[derive(Debug, PartialEq, Eq)]
434-
enum AppSource {
435-
None,
436-
File(PathBuf),
437-
OciRegistry(String),
438-
Unresolvable(String),
439-
}
440-
441-
impl AppSource {
442-
fn unresolvable(message: impl Into<String>) -> Self {
443-
Self::Unresolvable(message.into())
444-
}
445-
446-
fn local_app_dir(&self) -> Option<&Path> {
447-
match self {
448-
Self::File(path) => path.parent().or_else(|| {
449-
tracing::warn!("Error finding local app dir from source {path:?}");
450-
None
451-
}),
452-
_ => None,
453-
}
454-
}
455-
456-
async fn build(&self) -> anyhow::Result<()> {
457-
match self {
458-
Self::File(path) => spin_build::build(path, &[]).await,
459-
_ => Ok(()),
460-
}
461-
}
462-
}
463-
464-
impl Display for AppSource {
465-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
466-
match self {
467-
AppSource::None => write!(f, "<no source>"),
468-
AppSource::File(path) => write!(f, "local app {path:?}"),
469-
AppSource::OciRegistry(reference) => write!(f, "remote app {reference:?}"),
470-
AppSource::Unresolvable(s) => write!(f, "unknown app source: {s:?}"),
471-
}
472-
}
473-
}
474-
475419
#[cfg(test)]
476420
mod test {
421+
use crate::commands::up::app_source::AppSource;
422+
477423
use super::*;
478424

479425
fn repo_path(path: &str) -> String {

src/commands/up/app_source.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use std::path::{Path, PathBuf};
2+
3+
#[derive(Debug, PartialEq, Eq)]
4+
pub enum AppSource {
5+
None,
6+
File(PathBuf),
7+
OciRegistry(String),
8+
Unresolvable(String),
9+
}
10+
11+
impl AppSource {
12+
pub fn infer_source(source: &str) -> Self {
13+
let path = PathBuf::from(source);
14+
if path.exists() {
15+
Self::infer_file_source(path)
16+
} else if spin_oci::is_probably_oci_reference(source) {
17+
Self::OciRegistry(source.to_owned())
18+
} else {
19+
Self::Unresolvable(format!("File or directory '{source}' not found. If you meant to load from a registry, use the `--from-registry` option."))
20+
}
21+
}
22+
23+
pub fn infer_file_source(path: impl Into<PathBuf>) -> Self {
24+
match spin_common::paths::resolve_manifest_file_path(path.into()) {
25+
Ok(file) => Self::File(file),
26+
Err(e) => Self::Unresolvable(e.to_string()),
27+
}
28+
}
29+
30+
pub fn unresolvable(message: impl Into<String>) -> Self {
31+
Self::Unresolvable(message.into())
32+
}
33+
34+
pub fn local_app_dir(&self) -> Option<&Path> {
35+
match self {
36+
Self::File(path) => path.parent().or_else(|| {
37+
tracing::warn!("Error finding local app dir from source {path:?}");
38+
None
39+
}),
40+
_ => None,
41+
}
42+
}
43+
44+
pub async fn build(&self) -> anyhow::Result<()> {
45+
match self {
46+
Self::File(path) => spin_build::build(path, &[]).await,
47+
_ => Ok(()),
48+
}
49+
}
50+
}
51+
52+
impl std::fmt::Display for AppSource {
53+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54+
match self {
55+
Self::None => write!(f, "<no source>"),
56+
Self::File(path) => write!(f, "local app {path:?}"),
57+
Self::OciRegistry(reference) => write!(f, "remote app {reference:?}"),
58+
Self::Unresolvable(s) => write!(f, "unknown app source: {s:?}"),
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)