Skip to content

Commit

Permalink
Fix pr #123: Fix issue #121: Allow resetting base tag
Browse files Browse the repository at this point in the history
  • Loading branch information
openhands-agent committed Dec 16, 2024
1 parent d964a2c commit 85e551b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
76 changes: 67 additions & 9 deletions src/Commands/Reset/ResetCliCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,49 @@ internal class ResetCliCommand : AsyncCommand<ResetSettings>
private readonly ICreateContainerCommand _createContainerCommand;
private readonly IRunContainerCommand _runContainerCommand;
private readonly IContainerNamePrompt _containerNamePrompt;
private readonly IImageIdentifierPrompt _imageIdentifierPrompt;
private readonly IGetImageQuery _getImageQuery;
private readonly ICreateImageCommand _createImageCommand;
private readonly ListCliCommand _listCliCommand;

public ResetCliCommand(IGetRunningContainersQuery getRunningContainersQuery,
IStopAndRemoveContainerCommand stopAndRemoveContainerCommand, ICreateContainerCommand createContainerCommand,
IRunContainerCommand runContainerCommand, IContainerNamePrompt containerNamePrompt,
public ResetCliCommand(
IGetRunningContainersQuery getRunningContainersQuery,
IStopAndRemoveContainerCommand stopAndRemoveContainerCommand,
ICreateContainerCommand createContainerCommand,
IRunContainerCommand runContainerCommand,
IContainerNamePrompt containerNamePrompt,
IImageIdentifierPrompt imageIdentifierPrompt,
IGetImageQuery getImageQuery,
ICreateImageCommand createImageCommand,
ListCliCommand listCliCommand)
{
_getRunningContainersQuery = getRunningContainersQuery;
_stopAndRemoveContainerCommand = stopAndRemoveContainerCommand;
_createContainerCommand = createContainerCommand;
_runContainerCommand = runContainerCommand;
_containerNamePrompt = containerNamePrompt;
_imageIdentifierPrompt = imageIdentifierPrompt;
_getImageQuery = getImageQuery;
_createImageCommand = createImageCommand;
_listCliCommand = listCliCommand;
}

public override async Task<int> ExecuteAsync(CommandContext context, ResetSettings settings)
{
var container = await GetContainerAsync(settings);
if (container == null)
if (settings.IsImage)
{
throw new InvalidOperationException("No running container found");
await ResetImageBaseTagAsync(settings);
}
else
{
var container = await GetContainerAsync(settings);
if (container == null)
{
throw new InvalidOperationException("No running container found");
}

await ResetContainerAsync(container);
await ResetContainerAsync(container);
}

await _listCliCommand.ExecuteAsync();

Expand All @@ -53,7 +72,6 @@ public override async Task<int> ExecuteAsync(CommandContext context, ResetSettin
return containers.SingleOrDefault(c => c.ContainerName == identifier);
}


private async Task ResetContainerAsync(Container container)
{
await Spinner.StartAsync(
Expand All @@ -65,4 +83,44 @@ await Spinner.StartAsync(
await _runContainerCommand.ExecuteAsync(container);
});
}
}

private async Task ResetImageBaseTagAsync(ResetSettings settings)
{
var image = await GetImageAsync(settings);
if (image == null)
{
throw new InvalidOperationException("Image not found");
}

var tag = settings.Tag ?? image.Tag;
if (tag == null)
{
throw new InvalidOperationException("No tag specified and image has no tag");
}

await Spinner.StartAsync(
$"Resetting base tag for image '{image.Name}' to '{tag}'",
async _ =>
{
// Create a new image with the same content but new base tag
var labels = new Dictionary<string, string>
{
{ Constants.BaseTag, tag }
};

await _createImageCommand.ExecuteAsync(image.Name, tag);
});
}

private async Task<Image?> GetImageAsync(IImageIdentifierSettings settings)
{
if (settings.ImageIdentifier != null)
{
return await _getImageQuery.QueryAsync(settings.ImageIdentifier);
}

var images = await _getImageQuery.QueryAllAsync().ToListAsync();
var identifier = _imageIdentifierPrompt.GetIdentifierOfImageFromUser(images, "reset base tag");
return await _getImageQuery.QueryAsync(identifier);
}
}
14 changes: 11 additions & 3 deletions src/Commands/Reset/ResetSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

namespace port.Commands.Reset;

internal class ResetSettings : CommandSettings, IContainerIdentifierSettings
internal class ResetSettings : CommandSettings, IContainerIdentifierSettings, IImageIdentifierSettings

Check failure on line 5 in src/Commands/Reset/ResetSettings.cs

View workflow job for this annotation

GitHub Actions / build

'ResetSettings' does not implement interface member 'IImageIdentifierSettings.ImageIdentifier.set'

Check failure on line 5 in src/Commands/Reset/ResetSettings.cs

View workflow job for this annotation

GitHub Actions / build

'ResetSettings' does not implement interface member 'IImageIdentifierSettings.ImageIdentifier.set'
{
[CommandArgument(0, "[ContainerIdentifier]")]
[CommandArgument(0, "[Identifier]")]
public string? ContainerIdentifier { get; set; }
}

[CommandOption("-i|--image")]
public bool IsImage { get; set; }

[CommandOption("-t|--tag")]
public string? Tag { get; set; }

string? IImageIdentifierSettings.ImageIdentifier => ContainerIdentifier;

Check failure on line 16 in src/Commands/Reset/ResetSettings.cs

View workflow job for this annotation

GitHub Actions / build

Explicit interface implementation 'ResetSettings.IImageIdentifierSettings.ImageIdentifier' is missing accessor 'IImageIdentifierSettings.ImageIdentifier.set'

Check failure on line 16 in src/Commands/Reset/ResetSettings.cs

View workflow job for this annotation

GitHub Actions / build

Explicit interface implementation 'ResetSettings.IImageIdentifierSettings.ImageIdentifier' is missing accessor 'IImageIdentifierSettings.ImageIdentifier.set'
}

0 comments on commit 85e551b

Please sign in to comment.