🔧 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.
func GetPlatformName() stringEquivalent to runtime.GOOS, returns values like linux, darwin, windows, freebsd etc. Used for logging, diagnostic reports, or deciding which install branch to take.
fmt.Println(installer.GetPlatformName()) // e.g.: linuxGetArchName
Return current CPU architecture name.
func GetArchName() stringEquivalent 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.
fmt.Println(installer.GetArchName()) // e.g.: arm64CanUseSudo
Check if sudo command is available on the system.
func CanUseSudo() boolImplemented as exec.LookPath("sudo") == nil, i.e., whether sudo is in PATH. Only checks existence, doesn't verify current user has passwordless sudo rights.
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.
func ValidateInstallPath(path string) errorCurrent 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.).
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:
| Function | Purpose |
|---|---|
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
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
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:
GetSystemInforeturned map's platform and arch info collected similarly; for complete "one-click diagnostic" snapshot, preferGetSystemInfo()over calling individual functions on this page. - Cross-platform consistency:
GetPlatformName/GetArchNamebehave consistently on all OSes,CanUseSudoalways returnsfalseon Windows (no sudo on Windows). - Future extension:
ValidateInstallPathis 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.