MihaMarkic / Cake.Docker

Cake AddIn that extends Cake with Docker
MIT License
47 stars 36 forks source link

Consider using Docker.DotNet instead of docker cli #117

Open paulomorgado opened 2 weeks ago

paulomorgado commented 2 weeks ago

Using Docker.DotNet.

Maybe this could be a different addin to not break anyone and allow incremental implementation of aliases.

I can contribute.

MihaMarkic commented 2 weeks ago

Does it offer full capability compared to cli commands?

paulomorgado commented 2 weeks ago

It should, but, just in case, we could DockerApi for the alias and keep both.

MihaMarkic commented 2 weeks ago

OK, will take a look, it might take some time. Will let you know.

paulomorgado commented 2 weeks ago

The one I have and am using is for docker image load.

But, in the usage I have, I already have the image loaded into a Stream. That could be the STDIN.

So we need to have two options for the image:

Maybe we could have two overloads:

public static void DockerApiImageLoad(this ICakeContext context, FilePath image, DockerApiImageLoadSettings settings);
public static void DockerApiImageLoad(this ICakeContext context, Stream image, DockerApiImageLoadSettings settings);

/// <summary>
/// Settings for docker image load [OPTIONS].
/// Load an image from a tar archive file or stream
/// </summary>
public class DockerApiImageLoadSettings
{
    /// <summary>
    /// Gets or sets whether to suppress or not the load output.
    /// </summary>
        /// <value><see langword="true" /> to suppress load output; <see langword="false" /> to not suppress load output. Default is <see langword="false" />.
        /// <remarks>Same as command-line <c>--quiet</c>,<c>-q</c> option.</remarks>
    public bool Quiet { get; set; }
}

The invocation of the client would be something like:

await dockerClient.Images.LoadImageAsync(
    new ImageLoadParameters { Quiet = settings.Quiet },
    imageStream, // the provided stream or the stream from the provided file path.
    new Progress(context))
    .ConfigureAwait(ConfigureAwaitOptions.None);

private sealed class Progress(BuildContext context) : IProgress<JSONMessage>
{
    public void Report(JSONMessage value)
    {
        if (!string.IsNullOrEmpty(value.Stream))
        {
            context.Log.Information($"Stream: {value.Stream}");
        }

        if (!string.IsNullOrEmpty(value.Status))
        {
            context.Log.Information($"Status: {value.Status}");
        }

        if (value.Error is { } error)
        {
            context.Log.Error($"Error {error.Code}: {error.Message}");
            throw new Exception(error.Message);
        }

        if (!string.IsNullOrEmpty(value.ProgressMessage))
        {
            context.Log.Information($"Progress: {value.ProgressMessage}");
        }
    }
}

How's that for a start?

MihaMarkic commented 2 weeks ago

TBH I'm mulling an idea that cake addin is not even required with this library. One could reference Docker.DotNet directly and use it as it deems appropriate. Even more so because it's stateful. But let's take it slowly.