Skip to content

Commit 4153d74

Browse files
committed
Better behaviour if user forgets -f flag
Signed-off-by: itowlson <ivan.towlson@fermyon.com>
1 parent 0e34fa7 commit 4153d74

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

src/commands/up.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -166,24 +166,37 @@ impl UpCommand {
166166
&self.bindle_source,
167167
&self.registry_source,
168168
) {
169-
(None, None, None, None) => Self::default_manifest_or_none(),
169+
(None, None, None, None) => self.default_manifest_or_none(),
170170
(Some(source), None, None, None) => Self::infer_source(source),
171171
(None, Some(file), None, None) => Self::infer_file_source(file.to_owned()),
172172
(None, None, Some(id), None) => AppSource::Bindle(id.to_owned()),
173173
(None, None, None, Some(reference)) => AppSource::OciRegistry(reference.to_owned()),
174-
(_, _, _, _) => AppSource::unresolvable("More than one application was specified"),
174+
_ => AppSource::unresolvable("More than one application was specified"),
175175
}
176176
}
177177

178-
fn default_manifest_or_none() -> AppSource {
178+
fn default_manifest_or_none(&self) -> AppSource {
179179
let default_manifest = PathBuf::from(DEFAULT_MANIFEST_FILE);
180180
if default_manifest.exists() {
181181
AppSource::File(default_manifest)
182+
} else if self.trigger_args_look_file_like() {
183+
let msg = format!(
184+
"Default file 'spin.toml' found. Did you mean `spin up -f {}`?`",
185+
self.trigger_args[0].to_string_lossy()
186+
);
187+
AppSource::Unresolvable(msg)
182188
} else {
183189
AppSource::None
184190
}
185191
}
186192

193+
fn trigger_args_look_file_like(&self) -> bool {
194+
// Heuristic for the user typing `spin up foo` instead of `spin up -f foo` - in the
195+
// first case `foo` gets interpreted as a trigger arg which is probably not what the
196+
// user intended.
197+
!self.trigger_args.is_empty() && !self.trigger_args[0].to_string_lossy().starts_with('-')
198+
}
199+
187200
fn infer_file_source(path: PathBuf) -> AppSource {
188201
if path.is_file() {
189202
AppSource::File(path)
@@ -361,6 +374,7 @@ impl UpCommand {
361374
&self,
362375
mut locked_app: LockedApp,
363376
working_dir: PathBuf,
377+
needs_registry_flag: bool,
364378
) -> Result<(Vec<String>, TriggerExecOpts)> {
365379
// Apply --env to component environments
366380
if !self.env.is_empty() {
@@ -375,11 +389,10 @@ impl UpCommand {
375389
let exec_opts = if self.help {
376390
TriggerExecOpts::NoApp
377391
} else {
378-
let from_registry = self.registry_source.is_some();
379392
TriggerExecOpts::Remote {
380393
locked_url,
381394
working_dir,
382-
from_registry,
395+
from_registry: needs_registry_flag,
383396
}
384397
};
385398

@@ -440,7 +453,7 @@ impl UpCommand {
440453
.context("cannot get path to spin.lock")?;
441454

442455
let locked_app: LockedApp = serde_json::from_slice(&tokio::fs::read(&app_path).await?)?;
443-
self.prepare_locked_app(locked_app, working_dir).await
456+
self.prepare_locked_app(locked_app, working_dir, true).await
444457
}
445458

446459
async fn prepare_app_from_bindle(
@@ -457,7 +470,8 @@ impl UpCommand {
457470
};
458471

459472
let locked_app = spin_trigger::locked::build_locked_app(app, &working_dir)?;
460-
self.prepare_locked_app(locked_app, working_dir).await
473+
self.prepare_locked_app(locked_app, working_dir, false)
474+
.await
461475
}
462476
}
463477

@@ -579,19 +593,6 @@ impl AppSource {
579593
}
580594
}
581595

582-
#[derive(Clone, Debug, clap::ValueEnum)]
583-
enum AppSourceType {
584-
Auto,
585-
File,
586-
Oci,
587-
}
588-
589-
impl Default for AppSourceType {
590-
fn default() -> Self {
591-
Self::Auto
592-
}
593-
}
594-
595596
#[cfg(test)]
596597
mod test {
597598
use super::*;

0 commit comments

Comments
 (0)