diff --git a/src/tye/Program.InitCommand.cs b/src/tye/Program.InitCommand.cs index c3aeb444..b425dbed 100644 --- a/src/tye/Program.InitCommand.cs +++ b/src/tye/Program.InitCommand.cs @@ -18,17 +18,20 @@ namespace Tye CommonArguments.Path_Optional, }; - command.Handler = CommandHandler.Create((console, path) => + command.AddOption(new Option("--force") + { + Description = "Overrides the tye.yaml file if already present for project.", + Required = false + }); + + command.Handler = CommandHandler.Create((console, path, force) => { var output = new OutputContext(console, Verbosity.Info); output.WriteBanner(); - - if (path is FileInfo && - path.Exists && - (string.Equals(".yml", path.Extension, StringComparison.OrdinalIgnoreCase) || - string.Equals(".yaml", path.Extension, StringComparison.OrdinalIgnoreCase))) + if (path is FileInfo && path.Exists && !force) { - throw new CommandException($"File '{path.FullName}' already exists."); + ThrowIfTyeFilePresent(path, "tye.yml"); + ThrowIfTyeFilePresent(path, "tye.yaml"); } var template = @" @@ -105,5 +108,14 @@ services: return command; } + + private static void ThrowIfTyeFilePresent(FileInfo? path, string yml) + { + var tyeYaml = Path.Combine(path!.DirectoryName, yml); + if (File.Exists(tyeYaml)) + { + throw new CommandException($"File '{tyeYaml}' already exists. Use --force to override the {yml} file if desired."); + } + } } }