adachsoft / code-operations-go
Go project validation, testing, and execution for the AdachSoft code operations API.
Requires
- php: ^8.3
- adachsoft/code-operations-contracts: ^0.1 || ^0.2
- adachsoft/command-executor-lib: ^2.4
Requires (Dev)
- adachsoft/php-code-style: ^0.8.0
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
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.modfile. - Validates packages with
go buildandgo 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
GOTOOLCHAINenvironment variable used to select downloaded toolchains. - A Go project containing a
go.modfile.
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
RuntimeVoversion is matched against discovered toolchains. Exact versions and component prefixes such as1.27are supported; the highest available prefix match is selected. - If an explicit full version (
X.Y.Z) is not installed, the resolver selects it withGOTOOLCHAIN=goX.Y.Z, allowing Go to download the toolchain. An unavailable partial version raisesRuntimeNotAvailableException. - Without an explicit version, the
toolchain goX.Y.Zdirective ingo.modtakes precedence over thego 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.Zso Go can download it. An unavailable partial requirement raisesRuntimeNotAvailableException. - 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.