Dist-Zilla-Plugin-Docker-API

 view release on metacpan or  search on metacpan

lib/Dist/Zilla/Plugin/Docker/API/Client.pm  view on Meta::CPAN

            if defined $ENV{XDG_RUNTIME_DIR} && length $ENV{XDG_RUNTIME_DIR};

        # 4. $XDG_CONFIG_HOME/containers/auth.json, falling back to ~/.config.
        my $config_home = (defined $ENV{XDG_CONFIG_HOME} && length $ENV{XDG_CONFIG_HOME})
            ? $ENV{XDG_CONFIG_HOME}
            : Path::Tiny::path($ENV{HOME} // '', '.config');
        push @candidates, Path::Tiny::path($config_home, 'containers', 'auth.json');

        # 5. ~/.docker/config.json (classic docker location).
        push @candidates, Path::Tiny::path($ENV{HOME} // '', '.docker', 'config.json');

        return \@candidates;
    },
);

# Parsed auth files, in candidate order. Missing, unreadable or unparseable
# files are skipped (a parse error warns, as before, then falls through), so
# the list holds only the configs that actually yielded JSON.
has _auth_configs => (
    is      => 'ro',
    lazy    => 1,
    builder => sub {
        my ($self) = @_;
        my @configs;
        for my $path (@{ $self->auth_file_candidates }) {
            next unless $path;
            my $file = Path::Tiny::path($path);
            next unless -r "$file";
            my $data = eval { decode_json($file->slurp_utf8) };
            if ($@ || !$data) {
                $self->logger->("Warning: cannot parse $file: $@") if $@;
                next;
            }
            push @configs, $data;
        }
        return \@configs;
    },
);

# Cheapest round trip that proves an engine is actually listening on the
# socket. Used by the plugin's before_build precheck, so a missing daemon
# is reported before Dist::Zilla does any work rather than after.
sub engine_info {
    my ($self) = @_;

    my $version = eval { $self->docker->system->version };
    croak $@ if $@;
    croak 'the engine returned no version information'
        unless ref $version eq 'HASH';

    my ($engine) = grep { ($_->{Name} // '') =~ /engine/i }
        @{ $version->{Components} // [] };

    return {
        version     => $version->{Version},
        api_version => $version->{ApiVersion},
        engine      => $engine ? $engine->{Name} : undef,
    };
}

# The engine's tag endpoint takes the repository and the tag as two separate
# parameters (POST /images/{name}/tag?repo=&tag=), so a full image reference
# has to be taken apart before it goes over the wire. Passing the whole thing
# as `repo` leaves `tag` empty and the engine fills in its own default:
# podman then rejects `example/app:1.0:latest` with a 500 and every version
# tag is quietly lost. Splitting happens here rather than in API::Docker,
# which mirrors the endpoint and should keep doing so.
sub _split_image_ref {
    my ( $self, $ref ) = @_;

    my $colon = rindex $ref, ':';
    return ( $ref, undef ) if $colon < 0;
    # A colon before the last slash belongs to a registry host:port, not to a
    # tag: registry.example.com:5000/team/app carries no tag at all.
    return ( $ref, undef ) if rindex($ref, '/') > $colon;

    return ( substr($ref, 0, $colon), substr($ref, $colon + 1) );
}

sub build_image {
    my ($self, %arg) = @_;

    my $context = $arg{context_tar};
    my $dockerfile = $arg{dockerfile} // 'Dockerfile';
    my @tags = @{ $arg{tags} // [] };
    my %labels = %{ $arg{labels} // {} };
    my %buildargs = %{ $arg{buildargs} // {} };
    my $pull = $arg{pull} // 0;
    my $nocache = $arg{nocache} // 0;
    my $rm = $arg{rm} // 1;
    my $forcerm = $arg{forcerm} // 1;
    my $target = $arg{target};
    my $network_mode = $arg{network_mode};
    my $platform = $arg{platform};
    my $verbose = $arg{verbose} // 0;

    my $docker = $self->docker;

    my %build_opts = (
        dockerfile => $dockerfile,
        t => @tags ? $tags[0] : undef,
        pull => $pull ? 1 : 0,
        nocache => $nocache ? 1 : 0,
        rm => $rm ? 1 : 0,
        forcerm => $forcerm ? 1 : 0,
    );

    $build_opts{labels} = \%labels if %labels;
    $build_opts{buildargs} = \%buildargs if %buildargs;
    $build_opts{target} = $target if defined $target && length $target;
    $build_opts{networkmode} = $network_mode if defined $network_mode && length $network_mode;
    $build_opts{platform} = $platform if defined $platform && length $platform;

    my $image_id;
    my @processed_tags;

    # Concise mode (default): only forward Dockerfile step headers — both the
    # legacy builder ("Step N/M : ...") and BuildKit ("#N [N/M] ..."). Skip the
    # noisy intermediate container chatter. Verbose mode forwards every line.
    # The line buffer is used in both modes so the Dist::Zilla logger (which
    # appends its own newline) doesn't get a stream chunk with a trailing \n.
    my $line_buf = '';
    my $progress_cb = sub {
        my ($event) = @_;
        if ($event->{errorDetail}) {
            $self->logger_fatal->("Docker build error: " . $event->{errorDetail}{message});
        }



( run in 0.830 second using v1.01-cache-2.11-cpan-eb134014566 )