Search by

adachsoft / code-operations-go

Arkadiusz Adach

Go project validation, testing, and execution for the AdachSoft code operations API.

Package info

gitlab.com/a.adach/code-operations-go

Issues

pkg:composer/adachsoft/code-operations-go

Statistics

Installs: 11

Dependents: 0

Suggesters: 0

Stars: 0

v0.1.1 2026-12-01 21:59 UTC

This package is auto-updated.

Last update: 2026-12-01 20:59:35 UTC


README

A PHP 8.3+ library that validates, tests, and runs Go projects through the shared adachsoft/code-operations-contracts API. Commands are executed through adachsoft/command-executor-lib.

Features

  • Detects Go projects by their go.mod file.
  • Validates packages with go build and go vet, and runs Go tests and targets.
  • Resolves local and downloaded Go toolchains against explicit or project-declared version requirements.
  • Returns structured operation results with diagnostics, output, exit status, duration, and runtime details.

Requirements

  • PHP 8.3 or newer.
  • Go 1.21 or newer. Go 1.21 introduced the GOTOOLCHAIN environment variable used to select downloaded toolchains.
  • A Go project containing a go.mod file.

Installation

Install the library and its runtime dependencies with Composer:

composer require adachsoft/code-operations-go

Usage

<?php

declare(strict_types=1);

use AdachSoft\CodeOperationsContracts\ExecutionContextDto;
use AdachSoft\CodeOperationsContracts\RuntimeVo;
use AdachSoft\CodeOperationsGo\GoCodeOperations;
use AdachSoft\CodeOperationsGo\GoRuntimeResolver;
use AdachSoft\CommandExecutorLib\SimpleCommandRunner;

$runner = new SimpleCommandRunner();
$operations = new GoCodeOperations(
    new GoRuntimeResolver('/home/u/.local/go/bin/go', $runner),
    $runner,
);

$context = new ExecutionContextDto(
    projectPath: '/workspace/devlink-agent',
    runtime: new RuntimeVo('go', null),
    timeoutSeconds: 120,
);

$validation = $operations->validate($context);
$tests = $operations->test($context, 'TestAgent');

if (!$validation->success || !$tests->success) {
    foreach ([$validation, $tests] as $result) {
        foreach ($result->diagnostics as $diagnostic) {
            printf("%s: %s\n", $diagnostic->file ?? 'project', $diagnostic->message);
        }
    }
}

validate() runs go build ./... followed by go vet ./.... A non-empty ExecutionContextDto::paths limits both commands to the corresponding package trees. test() runs go test ./...; its optional filter is passed as Go's -run expression. run() invokes go run <target> <arguments...>.

Runtime selection

  • An explicit RuntimeVo version is matched against discovered toolchains. Exact versions and component prefixes such as 1.27 are supported; the highest available prefix match is selected.
  • If an explicit full version (X.Y.Z) is not installed, the resolver selects it with GOTOOLCHAIN=goX.Y.Z, allowing Go to download the toolchain. An unavailable partial version raises RuntimeNotAvailableException.
  • Without an explicit version, the toolchain goX.Y.Z directive in go.mod takes precedence over the go X.Y[.Z] directive. The declared version is treated as the project requirement; the newest available runtime that satisfies it is selected.
  • If no available runtime satisfies a full project requirement, the resolver selects GOTOOLCHAIN=goX.Y.Z so Go can download it. An unavailable partial requirement raises RuntimeNotAvailableException.
  • If the project declares no runtime requirement, the local Go toolchain is used with GOTOOLCHAIN=local.

The inherited process environment is combined with the selected runtime environment and ExecutionContextDto::environment; context values override runtime defaults. Results contain combined process output, diagnostics, exit status, and measured duration in milliseconds.