-
-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathUnixGoCheckTrait.php
More file actions
83 lines (66 loc) · 2.55 KB
/
UnixGoCheckTrait.php
File metadata and controls
83 lines (66 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
declare(strict_types=1);
namespace SPC\builder\traits;
use SPC\doctor\CheckResult;
use SPC\exception\RuntimeException;
use SPC\store\Downloader;
use SPC\store\FileSystem;
trait UnixGoCheckTrait
{
private function checkGoAndXcaddy(): ?CheckResult
{
$paths = explode(PATH_SEPARATOR, getenv('PATH'));
$goroot = getenv('GOROOT') ?: '/usr/local/go';
$goBin = "{$goroot}/bin";
$paths[] = $goBin;
if ($this->findCommand('go', $paths) === null) {
$this->installGo();
}
$gobin = getenv('GOBIN') ?: (getenv('HOME') . '/go/bin');
putenv("GOBIN={$gobin}");
$paths[] = $gobin;
if ($this->findCommand('xcaddy', $paths) === null) {
shell(true)->exec('go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest');
}
return CheckResult::ok();
}
private function installGo(): bool
{
$prefix = '';
if (get_current_user() !== 'root') {
$prefix = 'sudo ';
logger()->warning('Current user is not root, using sudo for running command');
}
$arch = php_uname('m');
$go_arch = match ($arch) {
'x86_64' => 'amd64',
'aarch64' => 'arm64',
default => $arch
};
$os = strtolower(PHP_OS_FAMILY);
$go_version = '1.24.4';
$go_filename = "go{$go_version}.{$os}-{$go_arch}.tar.gz";
$go_url = "https://go.dev/dl/{$go_filename}";
logger()->info("Downloading Go {$go_version} for {$go_arch}");
try {
// Download Go binary
Downloader::downloadFile('go', $go_url, $go_filename);
// Extract the tarball
FileSystem::extractSource('go', SPC_SOURCE_ARCHIVE, DOWNLOAD_PATH . "/{$go_filename}");
// Move to /usr/local/go
logger()->info('Installing Go to /usr/local/go');
shell()->exec("{$prefix}rm -rf /usr/local/go");
shell()->exec("{$prefix}mv " . SOURCE_PATH . '/go /usr/local/');
if (!str_contains(getenv('PATH'), '/usr/local/go/bin')) {
logger()->info('Adding Go to PATH');
shell()->exec("{$prefix}echo 'export PATH=\$PATH:/usr/local/go/bin' >> /etc/profile");
putenv('PATH=' . getenv('PATH') . ':/usr/local/go/bin');
}
logger()->info('Go has been installed successfully');
return true;
} catch (RuntimeException $e) {
logger()->error('Failed to install Go: ' . $e->getMessage());
return false;
}
}
}