Skip to content

🔧 Utility Functions

Generic helper functions used internally by installer — platform name, arch name, sudo availability, install path validation. Most are publicly exported for callers to do similar diagnostics and validation.

Package path: github.com/scagogogo/composer-skills/pkg/installer

Function List

💻

GetPlatformName

Return current OS name (runtime.GOOS).

🏗️

GetArchName

Return current CPU arch name (runtime.GOARCH).

🔒

CanUseSudo

Check if sudo is available on system.

📁

ValidateInstallPath

Validate install path is non-empty.

GetPlatformName

Return current operating system name.

go
func GetPlatformName() string

Equivalent to runtime.GOOS, returns values like linux, darwin, windows, freebsd etc. Used for logging, diagnostic reports, or deciding which install branch to take.

go
fmt.Println(installer.GetPlatformName()) // e.g.: linux

GetArchName

Return current CPU architecture name.

go
func GetArchName() string

Equivalent to runtime.GOARCH, returns values like amd64, arm64, 386 etc. Current installer package itself doesn't differentiate behavior by arch, this function mainly serves diagnostics and logging.

go
fmt.Println(installer.GetArchName()) // e.g.: arm64

CanUseSudo

Check if sudo command is available on the system.

go
func CanUseSudo() bool

Implemented as exec.LookPath("sudo") == nil, i.e., whether sudo is in PATH. Only checks existence, doesn't verify current user has passwordless sudo rights.

go
if installer.CanUseSudo() {
    cfg.UseSudo = true
}

Working with Config.UseSudo

Very useful when dynamically deciding whether to enable sudo: first CanUseSudo() probe, then set Config.UseSudo accordingly, avoiding hard-enabling sudo in sudo-less containers causing command failure.

ValidateInstallPath

Validate install path is usable.

go
func ValidateInstallPath(path string) error

Current implementation only checks path non-empty (returns install path cannot be empty when empty). This is most basic validation, callers can add stricter checks on top (e.g., writeability, whether already exists etc.).

go
if err := installer.ValidateInstallPath(cfg.InstallPath); err != nil {
    log.Fatal(err)
}

Current Validation is Lenient

ValidateInstallPath doesn't check whether path exists, is writable, or is absolute. For stronger validation, recommend combining with composerutils.CheckWritePermission and composerutils.EnsureDirectoryExists.

Internal Helper Functions (Not Public)

Following functions aren't exported, but understanding them helps reading installer source:

FunctionPurpose
findBinary(name)Find executable in PATH, equivalent to exec.LookPath
findCommand(name, args...)Create exec.Cmd, equivalent to exec.Command
runPkgCommand(useSudo, args...)Run package manager command, prefix sudo when useSudo=true
downloadComposerPhar(url, dest, cfg)Download phar using curl -fsSL -o
createComposerWrapper(phar, installPath, useSudo)Generate composer(sh) or composer.bat wrapper script
writeFile(path, content, useSudo)Write file, use sudo tee when useSudo

Quick Examples

System Environment Diagnostics

go
package main

import (
    "fmt"

    "github.com/scagogogo/composer-skills/pkg/installer"
)

func main() {
    fmt.Println("Platform:", installer.GetPlatformName())
    fmt.Println("Architecture:", installer.GetArchName())
    fmt.Println("sudo available:", installer.CanUseSudo())
}

Dynamically Build Config

go
package main

import (
    "log"

    "github.com/scagogogo/composer-skills/pkg/installer"
)

func main() {
    cfg := installer.DefaultConfig()

    // Path non-empty validation
    if err := installer.ValidateInstallPath(cfg.InstallPath); err != nil {
        log.Fatal(err)
    }

    // Container may not have sudo, decide by availability
    cfg.UseSudo = installer.CanUseSudo()

    _ = installer.NewInstaller(cfg)
}

Advanced

  • Relationship with GetSystemInfo: GetSystemInfo returned map's platform and arch info collected similarly; for complete "one-click diagnostic" snapshot, prefer GetSystemInfo() over calling individual functions on this page.
  • Cross-platform consistency: GetPlatformName/GetArchName behave consistently on all OSes, CanUseSudo always returns false on Windows (no sudo on Windows).
  • Future extension: ValidateInstallPath is placeholder implementation currently, later can be extended to check absolute path, writeability, conflict with existing files etc.; callers shouldn't rely on "only validates non-empty" current behavior, should treat it as "at least validates non-empty" guarantee.

Released under the MIT License