2
0
Files
GitCaddy b303a83a77
Some checks failed
CI / build-and-test (push) Has been cancelled
Release / build (amd64, darwin) (push) Has been cancelled
Release / build (amd64, linux) (push) Has been cancelled
Release / build (amd64, windows) (push) Has been cancelled
Release / build (arm64, darwin) (push) Has been cancelled
Release / build (arm64, linux) (push) Has been cancelled
Release / release (push) Has been cancelled
feat(capabilities): add visionOS SDK, PowerShell versions, working directory disk space
- Add visionOS/xrOS SDK detection for Vision Pro development
- Add PowerShell version detection (pwsh and powershell) with actual versions
- Detect disk space on working directory filesystem (not just root)
  - Useful for runners using external/USB drives for builds
- Add watchOS and tvOS suggested labels
- Refactor disk detection to accept path parameter

🤖 Generated with Claude Code (https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-11 20:32:23 +00:00

890 lines
24 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package envcheck
import (
"bufio"
"context"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/docker/docker/client"
)
// DiskInfo holds disk space information
type DiskInfo struct {
Path string `json:"path,omitempty"` // Path being checked (working directory)
Total uint64 `json:"total_bytes"`
Free uint64 `json:"free_bytes"`
Used uint64 `json:"used_bytes"`
UsedPercent float64 `json:"used_percent"`
}
// DistroInfo holds Linux distribution information
type DistroInfo struct {
ID string `json:"id,omitempty"` // e.g., "ubuntu", "debian", "fedora"
VersionID string `json:"version_id,omitempty"` // e.g., "24.04", "12"
PrettyName string `json:"pretty_name,omitempty"` // e.g., "Ubuntu 24.04 LTS"
}
// XcodeInfo holds Xcode and iOS development information
type XcodeInfo struct {
Version string `json:"version,omitempty"`
Build string `json:"build,omitempty"`
SDKs []string `json:"sdks,omitempty"` // e.g., ["iOS 17.0", "macOS 14.0"]
Simulators []string `json:"simulators,omitempty"` // Available iOS simulators
}
// RunnerCapabilities represents the capabilities of a runner for AI consumption
type RunnerCapabilities struct {
OS string `json:"os"`
Arch string `json:"arch"`
Distro *DistroInfo `json:"distro,omitempty"`
Xcode *XcodeInfo `json:"xcode,omitempty"`
Docker bool `json:"docker"`
DockerCompose bool `json:"docker_compose"`
ContainerRuntime string `json:"container_runtime,omitempty"`
Shell []string `json:"shell,omitempty"`
Tools map[string][]string `json:"tools,omitempty"`
BuildTools []string `json:"build_tools,omitempty"` // Available build/installer tools
PackageManagers []string `json:"package_managers,omitempty"`
Features *CapabilityFeatures `json:"features,omitempty"`
Limitations []string `json:"limitations,omitempty"`
Disk *DiskInfo `json:"disk,omitempty"`
Bandwidth *BandwidthInfo `json:"bandwidth,omitempty"`
SuggestedLabels []string `json:"suggested_labels,omitempty"`
}
// CapabilityFeatures represents feature support flags
type CapabilityFeatures struct {
ArtifactsV4 bool `json:"artifacts_v4"`
Cache bool `json:"cache"`
Services bool `json:"services"`
CompositeActions bool `json:"composite_actions"`
}
// DetectCapabilities detects the runner's capabilities
// workingDir is the directory where builds will run (for disk space detection)
func DetectCapabilities(ctx context.Context, dockerHost string, workingDir string) *RunnerCapabilities {
cap := &RunnerCapabilities{
OS: runtime.GOOS,
Arch: runtime.GOARCH,
Tools: make(map[string][]string),
BuildTools: []string{},
PackageManagers: []string{},
Shell: detectShells(),
Features: &CapabilityFeatures{
ArtifactsV4: false, // Gitea doesn't support v4 artifacts
Cache: true,
Services: true,
CompositeActions: true,
},
Limitations: []string{
"actions/upload-artifact@v4 not supported (use v3 or direct API upload)",
"actions/download-artifact@v4 not supported (use v3)",
},
}
// Detect Linux distribution
if runtime.GOOS == "linux" {
cap.Distro = detectLinuxDistro()
}
// Detect macOS Xcode/iOS
if runtime.GOOS == "darwin" {
cap.Xcode = detectXcode(ctx)
}
// Detect Docker
cap.Docker, cap.ContainerRuntime = detectDocker(ctx, dockerHost)
if cap.Docker {
cap.DockerCompose = detectDockerCompose(ctx)
cap.Features.Services = true
}
// Detect common tools
detectTools(ctx, cap)
// Detect build tools
detectBuildTools(ctx, cap)
// Detect package managers
detectPackageManagers(ctx, cap)
// Detect disk space on the working directory's filesystem
cap.Disk = detectDiskSpace(workingDir)
// Generate suggested labels based on detected capabilities
cap.SuggestedLabels = generateSuggestedLabels(cap)
return cap
}
// detectXcode detects Xcode and iOS development capabilities on macOS
func detectXcode(ctx context.Context) *XcodeInfo {
timeoutCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
// Check for xcodebuild
cmd := exec.CommandContext(timeoutCtx, "xcodebuild", "-version")
output, err := cmd.Output()
if err != nil {
return nil
}
xcode := &XcodeInfo{}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.HasPrefix(line, "Xcode ") {
xcode.Version = strings.TrimPrefix(line, "Xcode ")
} else if strings.HasPrefix(line, "Build version ") {
xcode.Build = strings.TrimPrefix(line, "Build version ")
}
}
// Get available SDKs
cmd = exec.CommandContext(timeoutCtx, "xcodebuild", "-showsdks")
output, err = cmd.Output()
if err == nil {
lines = strings.Split(string(output), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
// Look for SDK lines like "-sdk iphoneos17.0" or "iOS 17.0"
if strings.Contains(line, "SDK") || strings.HasPrefix(line, "-sdk") {
continue // Skip header lines
}
if strings.Contains(line, "iOS") || strings.Contains(line, "macOS") ||
strings.Contains(line, "watchOS") || strings.Contains(line, "tvOS") ||
strings.Contains(line, "visionOS") || strings.Contains(line, "xrOS") {
// Extract SDK name
if idx := strings.Index(line, "-sdk"); idx != -1 {
sdkPart := strings.TrimSpace(line[:idx])
if sdkPart != "" {
xcode.SDKs = append(xcode.SDKs, sdkPart)
}
}
}
}
}
// Get available simulators
cmd = exec.CommandContext(timeoutCtx, "xcrun", "simctl", "list", "devices", "available", "-j")
output, err = cmd.Output()
if err == nil {
var simData struct {
Devices map[string][]struct {
Name string `json:"name"`
State string `json:"state"`
} `json:"devices"`
}
if json.Unmarshal(output, &simData) == nil {
seen := make(map[string]bool)
for runtime, devices := range simData.Devices {
if strings.Contains(runtime, "iOS") {
for _, dev := range devices {
key := dev.Name
if !seen[key] {
seen[key] = true
xcode.Simulators = append(xcode.Simulators, dev.Name)
}
}
}
}
}
}
if xcode.Version == "" {
return nil
}
return xcode
}
// detectLinuxDistro reads /etc/os-release to get distribution info
func detectLinuxDistro() *DistroInfo {
file, err := os.Open("/etc/os-release")
if err != nil {
return nil
}
defer file.Close()
distro := &DistroInfo{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "ID=") {
distro.ID = strings.Trim(strings.TrimPrefix(line, "ID="), "\"")
} else if strings.HasPrefix(line, "VERSION_ID=") {
distro.VersionID = strings.Trim(strings.TrimPrefix(line, "VERSION_ID="), "\"")
} else if strings.HasPrefix(line, "PRETTY_NAME=") {
distro.PrettyName = strings.Trim(strings.TrimPrefix(line, "PRETTY_NAME="), "\"")
}
}
if distro.ID == "" {
return nil
}
return distro
}
// generateSuggestedLabels creates industry-standard labels based on capabilities
func generateSuggestedLabels(cap *RunnerCapabilities) []string {
labels := []string{}
seen := make(map[string]bool)
addLabel := func(label string) {
if label != "" && !seen[label] {
seen[label] = true
labels = append(labels, label)
}
}
// OS labels
switch cap.OS {
case "linux":
addLabel("linux")
addLabel("linux-latest")
case "windows":
addLabel("windows")
addLabel("windows-latest")
case "darwin":
addLabel("macos")
addLabel("macos-latest")
}
// Distro labels (Linux only)
if cap.Distro != nil && cap.Distro.ID != "" {
distro := strings.ToLower(cap.Distro.ID)
addLabel(distro)
addLabel(distro + "-latest")
}
// Xcode/iOS labels (macOS only)
if cap.Xcode != nil {
addLabel("xcode")
// Check for SDKs
for _, sdk := range cap.Xcode.SDKs {
sdkLower := strings.ToLower(sdk)
if strings.Contains(sdkLower, "ios") {
addLabel("ios")
}
if strings.Contains(sdkLower, "visionos") || strings.Contains(sdkLower, "xros") {
addLabel("visionos")
}
if strings.Contains(sdkLower, "watchos") {
addLabel("watchos")
}
if strings.Contains(sdkLower, "tvos") {
addLabel("tvos")
}
}
// If simulators available, add simulator label
if len(cap.Xcode.Simulators) > 0 {
addLabel("ios-simulator")
}
}
// Tool-based labels
if _, ok := cap.Tools["dotnet"]; ok {
addLabel("dotnet")
}
if _, ok := cap.Tools["java"]; ok {
addLabel("java")
}
if _, ok := cap.Tools["node"]; ok {
addLabel("node")
}
// Build tool labels
for _, tool := range cap.BuildTools {
switch tool {
case "msbuild":
addLabel("msbuild")
case "visual-studio":
addLabel("vs2022") // or detect actual version
case "inno-setup":
addLabel("inno-setup")
case "nsis":
addLabel("nsis")
}
}
return labels
}
// ToJSON converts capabilities to JSON string for transmission
func (c *RunnerCapabilities) ToJSON() string {
data, err := json.Marshal(c)
if err != nil {
return "{}"
}
return string(data)
}
func detectShells() []string {
shells := []string{}
switch runtime.GOOS {
case "windows":
if _, err := exec.LookPath("pwsh"); err == nil {
shells = append(shells, "pwsh")
}
if _, err := exec.LookPath("powershell"); err == nil {
shells = append(shells, "powershell")
}
shells = append(shells, "cmd")
case "darwin":
if _, err := exec.LookPath("zsh"); err == nil {
shells = append(shells, "zsh")
}
if _, err := exec.LookPath("bash"); err == nil {
shells = append(shells, "bash")
}
shells = append(shells, "sh")
default: // linux and others
if _, err := exec.LookPath("bash"); err == nil {
shells = append(shells, "bash")
}
shells = append(shells, "sh")
}
return shells
}
func detectDocker(ctx context.Context, dockerHost string) (bool, string) {
opts := []client.Opt{client.FromEnv}
if dockerHost != "" {
opts = append(opts, client.WithHost(dockerHost))
}
cli, err := client.NewClientWithOpts(opts...)
if err != nil {
return false, ""
}
defer cli.Close()
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
_, err = cli.Ping(timeoutCtx)
if err != nil {
return false, ""
}
// Check if it's podman or docker
info, err := cli.Info(timeoutCtx)
if err == nil {
if strings.Contains(strings.ToLower(info.Name), "podman") {
return true, "podman"
}
}
return true, "docker"
}
func detectDockerCompose(ctx context.Context) bool {
// Check for docker compose v2 (docker compose)
cmd := exec.CommandContext(ctx, "docker", "compose", "version")
if err := cmd.Run(); err == nil {
return true
}
// Check for docker-compose v1
if _, err := exec.LookPath("docker-compose"); err == nil {
return true
}
return false
}
func detectTools(ctx context.Context, cap *RunnerCapabilities) {
toolDetectors := map[string]func(context.Context) []string{
"node": detectNodeVersions,
"go": detectGoVersions,
"python": detectPythonVersions,
"java": detectJavaVersions,
"dotnet": detectDotnetVersions,
"rust": detectRustVersions,
"ruby": detectRubyVersions,
"php": detectPHPVersions,
"swift": detectSwiftVersions,
"kotlin": detectKotlinVersions,
"flutter": detectFlutterVersions,
"dart": detectDartVersions,
"powershell": detectPowerShellVersions,
}
for tool, detector := range toolDetectors {
if versions := detector(ctx); len(versions) > 0 {
cap.Tools[tool] = versions
}
}
// Detect additional tools that just need presence check
simpleTools := map[string]string{
"git": "git",
"cmake": "cmake",
"make": "make",
"ninja": "ninja",
"gradle": "gradle",
"maven": "mvn",
"npm": "npm",
"yarn": "yarn",
"pnpm": "pnpm",
"cargo": "cargo",
"pip": "pip3",
}
for name, cmd := range simpleTools {
if v := detectSimpleToolVersion(ctx, cmd); v != "" {
cap.Tools[name] = []string{v}
}
}
}
func detectBuildTools(ctx context.Context, cap *RunnerCapabilities) {
switch runtime.GOOS {
case "windows":
detectWindowsBuildTools(ctx, cap)
case "darwin":
detectMacOSBuildTools(ctx, cap)
case "linux":
detectLinuxBuildTools(ctx, cap)
}
}
func detectWindowsBuildTools(ctx context.Context, cap *RunnerCapabilities) {
// Check for Visual Studio via vswhere
vswherePaths := []string{
`C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe`,
`C:\Program Files\Microsoft Visual Studio\Installer\vswhere.exe`,
}
for _, vswhere := range vswherePaths {
if _, err := os.Stat(vswhere); err == nil {
cmd := exec.CommandContext(ctx, vswhere, "-latest", "-property", "displayName")
if output, err := cmd.Output(); err == nil && len(output) > 0 {
cap.BuildTools = append(cap.BuildTools, "visual-studio")
break
}
}
}
// Check for MSBuild
msbuildPaths := []string{
`C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe`,
`C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe`,
`C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe`,
`C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe`,
`C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe`,
`C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe`,
}
for _, msbuild := range msbuildPaths {
if _, err := os.Stat(msbuild); err == nil {
cap.BuildTools = append(cap.BuildTools, "msbuild")
break
}
}
// Check for Inno Setup
innoSetupPaths := []string{
`C:\Program Files (x86)\Inno Setup 6\ISCC.exe`,
`C:\Program Files\Inno Setup 6\ISCC.exe`,
`C:\Program Files (x86)\Inno Setup 5\ISCC.exe`,
`C:\Program Files\Inno Setup 5\ISCC.exe`,
}
for _, iscc := range innoSetupPaths {
if _, err := os.Stat(iscc); err == nil {
cap.BuildTools = append(cap.BuildTools, "inno-setup")
break
}
}
// Also check PATH
if _, err := exec.LookPath("iscc"); err == nil {
if !contains(cap.BuildTools, "inno-setup") {
cap.BuildTools = append(cap.BuildTools, "inno-setup")
}
}
// Check for NSIS
nsisPaths := []string{
`C:\Program Files (x86)\NSIS\makensis.exe`,
`C:\Program Files\NSIS\makensis.exe`,
}
for _, nsis := range nsisPaths {
if _, err := os.Stat(nsis); err == nil {
cap.BuildTools = append(cap.BuildTools, "nsis")
break
}
}
if _, err := exec.LookPath("makensis"); err == nil {
if !contains(cap.BuildTools, "nsis") {
cap.BuildTools = append(cap.BuildTools, "nsis")
}
}
// Check for WiX Toolset
wixPaths := []string{
`C:\Program Files (x86)\WiX Toolset v3.11\bin\candle.exe`,
`C:\Program Files (x86)\WiX Toolset v3.14\bin\candle.exe`,
}
for _, wix := range wixPaths {
if _, err := os.Stat(wix); err == nil {
cap.BuildTools = append(cap.BuildTools, "wix")
break
}
}
// Check for signtool (Windows SDK)
signtoolPaths, _ := filepath.Glob(`C:\Program Files (x86)\Windows Kits\10\bin\*\x64\signtool.exe`)
if len(signtoolPaths) > 0 {
cap.BuildTools = append(cap.BuildTools, "signtool")
}
}
func detectMacOSBuildTools(ctx context.Context, cap *RunnerCapabilities) {
// Check for xcpretty
if _, err := exec.LookPath("xcpretty"); err == nil {
cap.BuildTools = append(cap.BuildTools, "xcpretty")
}
// Check for fastlane
if _, err := exec.LookPath("fastlane"); err == nil {
cap.BuildTools = append(cap.BuildTools, "fastlane")
}
// Check for CocoaPods
if _, err := exec.LookPath("pod"); err == nil {
cap.BuildTools = append(cap.BuildTools, "cocoapods")
}
// Check for Carthage
if _, err := exec.LookPath("carthage"); err == nil {
cap.BuildTools = append(cap.BuildTools, "carthage")
}
// Check for SwiftLint
if _, err := exec.LookPath("swiftlint"); err == nil {
cap.BuildTools = append(cap.BuildTools, "swiftlint")
}
// Check for create-dmg or similar
if _, err := exec.LookPath("create-dmg"); err == nil {
cap.BuildTools = append(cap.BuildTools, "create-dmg")
}
// Check for Packages (packagesbuild)
if _, err := exec.LookPath("packagesbuild"); err == nil {
cap.BuildTools = append(cap.BuildTools, "packages")
}
// Check for pkgbuild (built-in)
if _, err := exec.LookPath("pkgbuild"); err == nil {
cap.BuildTools = append(cap.BuildTools, "pkgbuild")
}
// Check for codesign (built-in)
if _, err := exec.LookPath("codesign"); err == nil {
cap.BuildTools = append(cap.BuildTools, "codesign")
}
// Check for notarytool (built-in with Xcode)
if _, err := exec.LookPath("notarytool"); err == nil {
cap.BuildTools = append(cap.BuildTools, "notarytool")
}
}
func detectLinuxBuildTools(ctx context.Context, cap *RunnerCapabilities) {
// Check for common Linux build tools
tools := []string{
"gcc", "g++", "clang", "clang++",
"autoconf", "automake", "libtool",
"pkg-config", "meson",
"dpkg-deb", "rpmbuild", "fpm",
"appimage-builder", "linuxdeploy",
}
for _, tool := range tools {
if _, err := exec.LookPath(tool); err == nil {
cap.BuildTools = append(cap.BuildTools, tool)
}
}
}
func detectPackageManagers(ctx context.Context, cap *RunnerCapabilities) {
switch runtime.GOOS {
case "windows":
if _, err := exec.LookPath("choco"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "chocolatey")
}
if _, err := exec.LookPath("scoop"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "scoop")
}
if _, err := exec.LookPath("winget"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "winget")
}
case "darwin":
if _, err := exec.LookPath("brew"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "homebrew")
}
if _, err := exec.LookPath("port"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "macports")
}
case "linux":
if _, err := exec.LookPath("apt"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "apt")
}
if _, err := exec.LookPath("yum"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "yum")
}
if _, err := exec.LookPath("dnf"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "dnf")
}
if _, err := exec.LookPath("pacman"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "pacman")
}
if _, err := exec.LookPath("zypper"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "zypper")
}
if _, err := exec.LookPath("apk"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "apk")
}
if _, err := exec.LookPath("snap"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "snap")
}
if _, err := exec.LookPath("flatpak"); err == nil {
cap.PackageManagers = append(cap.PackageManagers, "flatpak")
}
}
}
func detectNodeVersions(ctx context.Context) []string {
return detectToolVersion(ctx, "node", "--version", "v")
}
func detectGoVersions(ctx context.Context) []string {
return detectToolVersion(ctx, "go", "version", "go")
}
func detectPythonVersions(ctx context.Context) []string {
versions := []string{}
// Try python3 first
if v := detectToolVersion(ctx, "python3", "--version", "Python "); len(v) > 0 {
versions = append(versions, v...)
}
// Also try python
if v := detectToolVersion(ctx, "python", "--version", "Python "); len(v) > 0 {
for _, ver := range v {
if !contains(versions, ver) {
versions = append(versions, ver)
}
}
}
return versions
}
func detectJavaVersions(ctx context.Context) []string {
cmd := exec.CommandContext(ctx, "java", "-version")
output, err := cmd.CombinedOutput()
if err != nil {
return nil
}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
if strings.Contains(line, "version") {
start := strings.Index(line, "\"")
end := strings.LastIndex(line, "\"")
if start != -1 && end > start {
version := line[start+1 : end]
parts := strings.Split(version, ".")
if len(parts) > 0 {
if parts[0] == "1" && len(parts) > 1 {
return []string{parts[1]}
}
return []string{parts[0]}
}
}
}
}
return nil
}
func detectDotnetVersions(ctx context.Context) []string {
cmd := exec.CommandContext(ctx, "dotnet", "--list-sdks")
output, err := cmd.Output()
if err != nil {
return nil
}
versions := []string{}
lines := strings.Split(string(output), "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
continue
}
parts := strings.Split(line, " ")
if len(parts) > 0 {
version := parts[0]
major := strings.Split(version, ".")[0]
if !contains(versions, major) {
versions = append(versions, major)
}
}
}
return versions
}
func detectRustVersions(ctx context.Context) []string {
return detectToolVersion(ctx, "rustc", "--version", "rustc ")
}
func detectRubyVersions(ctx context.Context) []string {
return detectToolVersion(ctx, "ruby", "--version", "ruby ")
}
func detectPHPVersions(ctx context.Context) []string {
return detectToolVersion(ctx, "php", "--version", "PHP ")
}
func detectSwiftVersions(ctx context.Context) []string {
return detectToolVersion(ctx, "swift", "--version", "Swift version ")
}
func detectKotlinVersions(ctx context.Context) []string {
return detectToolVersion(ctx, "kotlin", "-version", "Kotlin version ")
}
func detectFlutterVersions(ctx context.Context) []string {
return detectToolVersion(ctx, "flutter", "--version", "Flutter ")
}
func detectDartVersions(ctx context.Context) []string {
return detectToolVersion(ctx, "dart", "--version", "Dart SDK version: ")
}
func detectPowerShellVersions(ctx context.Context) []string {
versions := []string{}
// Check for pwsh (PowerShell Core / PowerShell 7+)
if v := detectPwshVersion(ctx, "pwsh"); v != "" {
versions = append(versions, "pwsh:"+v)
}
// Check for powershell (Windows PowerShell 5.x)
if runtime.GOOS == "windows" {
if v := detectPwshVersion(ctx, "powershell"); v != "" {
versions = append(versions, "powershell:"+v)
}
}
return versions
}
func detectPwshVersion(ctx context.Context, cmd string) string {
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
// Use -Command to get version
var c *exec.Cmd
if cmd == "pwsh" {
c = exec.CommandContext(timeoutCtx, cmd, "-Command", "$PSVersionTable.PSVersion.ToString()")
} else {
c = exec.CommandContext(timeoutCtx, cmd, "-Command", "$PSVersionTable.PSVersion.ToString()")
}
output, err := c.Output()
if err != nil {
return ""
}
version := strings.TrimSpace(string(output))
// Return major.minor
parts := strings.Split(version, ".")
if len(parts) >= 2 {
return parts[0] + "." + parts[1]
}
return version
}
func detectSimpleToolVersion(ctx context.Context, cmd string) string {
if _, err := exec.LookPath(cmd); err != nil {
return ""
}
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
c := exec.CommandContext(timeoutCtx, cmd, "--version")
output, err := c.Output()
if err != nil {
// Try without --version for tools that don't support it
return "installed"
}
line := strings.TrimSpace(strings.Split(string(output), "\n")[0])
// Extract version number if possible
parts := strings.Fields(line)
for _, part := range parts {
// Look for something that looks like a version
if len(part) > 0 && (part[0] >= '0' && part[0] <= '9' || part[0] == 'v') {
return strings.TrimPrefix(part, "v")
}
}
return "installed"
}
func detectToolVersion(ctx context.Context, cmd string, args string, prefix string) []string {
timeoutCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
c := exec.CommandContext(timeoutCtx, cmd, args)
output, err := c.Output()
if err != nil {
return nil
}
line := strings.TrimSpace(string(output))
if prefix != "" {
if idx := strings.Index(line, prefix); idx != -1 {
line = line[idx+len(prefix):]
}
}
parts := strings.Fields(line)
if len(parts) > 0 {
version := parts[0]
version = strings.TrimPrefix(version, "v")
vparts := strings.Split(version, ".")
if len(vparts) >= 2 {
return []string{vparts[0] + "." + vparts[1]}
}
return []string{vparts[0]}
}
return nil
}
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}