Skip to content

Commit c45d255

Browse files
committed
Default restore to global install, add local flag
1 parent 8d78087 commit c45d255

File tree

5 files changed

+292
-92
lines changed

5 files changed

+292
-92
lines changed

src/dnvm/CommandLineArguments.cs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,17 @@ public sealed partial record PruneCommand : DnvmSubCommand
174174
}
175175

176176
[Command("restore", Summary = "Restore the SDK listed in the global.json file.",
177-
Description = "Downloads the SDK in the global.json in or above the current directory to the .dotnet folder in the same directory.")]
177+
Description = "Downloads the SDK in the global.json in or above the current directory.")]
178178
public sealed partial record RestoreCommand : DnvmSubCommand
179179
{
180+
[CommandOption("-l|--local", Description = "Install the sdk into the .dotnet folder in the same directory as global.json.")]
181+
public bool? Local { get; init; } = null;
182+
183+
[CommandOption("-f|--force", Description = "Force install the SDK, even if already installed.")]
184+
public bool? Force { get; init; } = null;
185+
186+
[CommandOption("-v|--verbose", Description = "Print extra debugging info to the console.")]
187+
public bool? Verbose { get; init; } = null;
180188
}
181189

182190
/// <summary>
@@ -310,7 +318,12 @@ public sealed record PruneArguments : CommandArguments
310318
public bool DryRun { get; init; } = false;
311319
}
312320

313-
public sealed record RestoreArguments : CommandArguments;
321+
public sealed record RestoreArguments : CommandArguments
322+
{
323+
public bool Local { get; init; } = false;
324+
public bool Force { get; init; } = false;
325+
public bool Verbose { get; init; } = false;
326+
}
314327
}
315328

316329
public sealed record class CommandLineArguments(CommandArguments? Command)
@@ -434,7 +447,12 @@ public static CommandLineArguments ParseRaw(IAnsiConsole console, string[] args)
434447
}
435448
case DnvmSubCommand.RestoreCommand r:
436449
{
437-
return new CommandLineArguments(new CommandArguments.RestoreArguments());
450+
return new CommandLineArguments(new CommandArguments.RestoreArguments
451+
{
452+
Local = r.Local ?? false,
453+
Force = r.Force ?? false,
454+
Verbose = r.Verbose ?? false,
455+
});
438456
}
439457
case null:
440458
{

src/dnvm/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ internal static async Task<int> Dnvm(DnvmEnv env, Logger logger, CommandLineArgu
6767
CommandArguments.UntrackArguments o => await UntrackCommand.Run(env, logger, o),
6868
CommandArguments.UninstallArguments o => await UninstallCommand.Run(env, logger, o),
6969
CommandArguments.PruneArguments args => await PruneCommand.Run(env, logger, args),
70-
CommandArguments.RestoreArguments => await RestoreCommand.Run(env, logger) switch {
70+
CommandArguments.RestoreArguments o => await RestoreCommand.Run(env, logger, o) switch {
7171
Result<SemVersion, RestoreCommand.Error>.Ok => 0,
7272
Result<SemVersion, RestoreCommand.Error>.Err x => (int)x.Value,
7373
},

src/dnvm/RestoreCommand.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public enum Error
128128
CantFindRequestedVersion = 6,
129129
}
130130

131-
public static async Task<Result<SemVersion, Error>> Run(DnvmEnv env, Logger logger)
131+
public static async Task<Result<SemVersion, Error>> Run(DnvmEnv env, Logger logger, CommandArguments.RestoreArguments options)
132132
{
133133
UPath? globalJsonPathOpt = null;
134134
UPath cwd = env.Cwd;
@@ -228,10 +228,21 @@ public static async Task<Result<SemVersion, Error>> Run(DnvmEnv env, Logger logg
228228

229229
var downloadUrl = component.Files.Single(f => f.Rid == Utilities.CurrentRID.ToString() && f.Url.EndsWith(Utilities.ZipSuffix)).Url;
230230

231-
var error = await InstallCommand.InstallSdkToDir(env.HttpClient, downloadUrl, env.CwdFs, installDir, env.TempFs, logger);
232-
if (error is not null)
231+
if (options.Local)
233232
{
234-
return Error.IoError;
233+
var error = await InstallCommand.InstallSdkToDir(env.HttpClient, downloadUrl, env.CwdFs, installDir, env.TempFs, logger);
234+
if (error is not null)
235+
{
236+
return Error.IoError;
237+
}
238+
}
239+
else
240+
{
241+
var error = await InstallCommand.Run(env, logger, new CommandArguments.InstallArguments { SdkVersion = component.Version, Force = options.Force, Verbose = options.Verbose });
242+
if (error != InstallCommand.Result.Success)
243+
{
244+
return Error.IoError;
245+
}
235246
}
236247

237248
return component.Version;

test/UnitTests/CommandLineTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,15 @@ public void RestoreHelp(string param)
447447
console,
448448
[ "restore", param ]).Command);
449449
Assert.Equal("""
450-
usage: dnvm restore [-h | --help]
450+
usage: dnvm restore [-l | --local] [-f | --force] [-v | --verbose] [-h | --help]
451451
452-
Downloads the SDK in the global.json in or above the current directory to the
453-
.dotnet folder in the same directory.
452+
Downloads the SDK in the global.json in or above the current directory.
454453
455454
Options:
455+
-l, --local Install the sdk into the .dotnet folder in the same directory
456+
as global.json.
457+
-f, --force Force install the SDK, even if already installed.
458+
-v, --verbose Print extra debugging info to the console.
456459
-h, --help Show help information.
457460
458461

0 commit comments

Comments
 (0)