system

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 5, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package system provides internal system-level utilities for macgo.

Index

Constants

View Source
const (
	// Core configuration
	EnvAppName    = "MACGO_APP_NAME"
	EnvBundleID   = "MACGO_BUNDLE_ID"
	EnvDebug      = "MACGO_DEBUG"
	EnvKeepBundle = "MACGO_KEEP_BUNDLE"
	EnvVersion    = "MACGO_VERSION"

	// Code signing
	EnvCodeSignIdentity = "MACGO_CODE_SIGN_IDENTITY"
	EnvAutoSign         = "MACGO_AUTO_SIGN"
	EnvAdHocSign        = "MACGO_AD_HOC_SIGN"

	// Launch behavior
	EnvNoRelaunch           = "MACGO_NO_RELAUNCH"
	EnvForceLaunchServices  = "MACGO_FORCE_LAUNCH_SERVICES"
	EnvForceDirectExecution = "MACGO_FORCE_DIRECT"

	// Permission flags
	EnvCamera     = "MACGO_CAMERA"
	EnvMicrophone = "MACGO_MICROPHONE"
	EnvLocation   = "MACGO_LOCATION"
	EnvFiles      = "MACGO_FILES"
	EnvNetwork    = "MACGO_NETWORK"
	EnvSandbox    = "MACGO_SANDBOX"

	// TCC and permissions
	EnvResetPermissions = "MACGO_RESET_PERMISSIONS"

	// Testing and development
	EnvTestIntegration = "MACGO_TEST_INTEGRATION"
)

Environment variable constants for macgo

Variables

This section is empty.

Functions

func AllMacgoEnvVars

func AllMacgoEnvVars() []string

AllMacgoEnvVars returns a list of all known macgo environment variables.

func CalculateFileSHA256

func CalculateFileSHA256(filePath string) (string, error)

CalculateFileSHA256 calculates the SHA256 hash of a file. Returns the hexadecimal string representation of the hash.

func CleanAppName

func CleanAppName(name string) string

CleanAppName removes problematic characters from app names. It replaces filesystem-problematic characters with hyphens and removes non-printable ASCII characters.

func ClearAllMacgoEnv

func ClearAllMacgoEnv()

ClearAllMacgoEnv removes all macgo environment variables. Useful for testing to ensure clean state.

func CompareFileSHA256

func CompareFileSHA256(filePath, expectedHash string) (bool, error)

CompareFileSHA256 compares the SHA256 hash of a file with an expected hash. Returns true if they match, false otherwise.

func CopyFile

func CopyFile(src, dst string) error

CopyFile copies a file from src to dst. It handles error cases gracefully and preserves file permissions.

func DirExists

func DirExists(path string) bool

DirExists checks if a directory exists.

func EnsureDir

func EnsureDir(path string, perm os.FileMode) error

EnsureDir creates a directory and all parent directories if they don't exist.

func ExtractAppNameFromPath

func ExtractAppNameFromPath(execPath string) string

ExtractAppNameFromPath extracts a reasonable app name from an executable path.

func FileExists

func FileExists(path string) bool

FileExists checks if a file exists and is not a directory.

func GetBool

func GetBool(key string) bool

GetBool returns the boolean value of an environment variable. Returns true if the variable is set to "1", "true", "yes", or "on" (case-insensitive). Returns false otherwise.

func GetBundleContentsPath

func GetBundleContentsPath(bundlePath string) string

GetBundleContentsPath constructs the path to the Contents directory in an app bundle.

func GetBundleEntitlementsPath

func GetBundleEntitlementsPath(bundlePath string) string

GetBundleEntitlementsPath constructs the path to the entitlements.plist in an app bundle.

func GetBundleExecutablePath

func GetBundleExecutablePath(bundlePath, execName string) string

GetBundleExecutablePath constructs the path to the executable inside an app bundle.

func GetBundleID

func GetBundleID(bundlePath string) string

GetBundleID extracts the bundle identifier from an app bundle's Info.plist

func GetBundleInfoPlistPath

func GetBundleInfoPlistPath(bundlePath string) string

GetBundleInfoPlistPath constructs the path to the Info.plist in an app bundle.

func GetEnabledPermissions

func GetEnabledPermissions() []string

GetEnabledPermissions returns a slice of permission names that are enabled via environment variables.

func GetInt

func GetInt(key string, defaultValue int) int

GetInt returns the integer value of an environment variable. Returns the defaultValue if the variable is not set, empty, or cannot be parsed.

func GetLaunchMode

func GetLaunchMode() string

GetLaunchMode determines the launch mode based on environment variables. Returns "launch_services", "direct", or "auto" (default).

func GetMacgoEnvSnapshot

func GetMacgoEnvSnapshot() map[string]string

GetMacgoEnvSnapshot returns a snapshot of all current macgo environment variables.

func GetPermissionFlags

func GetPermissionFlags() map[string]bool

GetPermissionFlags returns a map of permission names to their enabled status.

func GetString

func GetString(key, defaultValue string) string

GetString returns the string value of an environment variable. Returns the defaultValue if the variable is not set or empty.

func GetStringSlice

func GetStringSlice(key string) []string

GetStringSlice returns a slice of strings from an environment variable. The value should be comma-separated. Empty values are filtered out.

func HasAnyPermissionFlags

func HasAnyPermissionFlags() bool

HasAnyPermissionFlags returns true if any permission environment variables are set.

func InferBundleID

func InferBundleID(appName string) string

InferBundleID creates a reasonable bundle ID from the Go module path and app name. It uses the module path from build info to create meaningful, unique bundle IDs that reflect the actual Go module and program name.

func IsAppBundle

func IsAppBundle(path string) bool

IsAppBundle checks if the given path appears to be an app bundle.

func IsDebugEnabled

func IsDebugEnabled() bool

IsDebugEnabled checks if debug mode is enabled via environment variable.

func IsInAppBundle

func IsInAppBundle() bool

IsInAppBundle checks if we're already running inside an app bundle. This is determined by checking if the executable path contains ".app/Contents/MacOS/".

func IsRelaunchDisabled

func IsRelaunchDisabled() bool

IsRelaunchDisabled checks if app bundle relaunch is disabled.

func IsResetPermissionsEnabled

func IsResetPermissionsEnabled() bool

IsResetPermissionsEnabled checks if TCC permission reset is enabled.

func LimitAppNameLength

func LimitAppNameLength(name string, maxLength int) string

LimitAppNameLength truncates an app name to a reasonable length, reserving space for the .app extension.

func RestoreEnv

func RestoreEnv(saved map[string]string)

RestoreEnv restores environment variables from a saved state. Use with SaveEnv for temporary environment modifications.

func SafeWriteFile

func SafeWriteFile(filename string, data []byte, perm os.FileMode) error

SafeWriteFile writes data to a file safely by writing to a temporary file first, then moving it to the final location. This prevents partial writes in case of errors.

func SaveEnv

func SaveEnv(keys []string) map[string]string

SaveEnv saves current environment variable values for later restoration. Returns a map that can be passed to RestoreEnv.

func SetBool

func SetBool(key string, value bool) error

SetBool sets an environment variable to a boolean value. Sets to "1" for true, "0" for false.

func ValidateAppName

func ValidateAppName(name string) error

ValidateAppName checks if an app name is reasonable for macOS.

func ValidateBundleID

func ValidateBundleID(bundleID string) error

ValidateBundleID checks if a bundle ID follows Apple's naming conventions.

Types

type MacOSVersion

type MacOSVersion struct {
	Major int
	Minor int
	Patch int
	Raw   string
}

MacOSVersion represents a parsed macOS version

func GetMacOSVersion

func GetMacOSVersion() (MacOSVersion, error)

GetMacOSVersion retrieves the current macOS version

func MustGetMacOSVersion

func MustGetMacOSVersion() MacOSVersion

MustGetMacOSVersion retrieves the macOS version and panics on error This is useful for initialization code where the version must be available

func ParseMacOSVersion

func ParseMacOSVersion(version string) (MacOSVersion, error)

ParseMacOSVersion parses a version string like "14.2.1" or "15.0"

func (MacOSVersion) IsAtLeast

func (v MacOSVersion) IsAtLeast(major, minor, patch int) bool

IsAtLeast checks if this version is at least the specified version

func (MacOSVersion) IsSequoiaOrLater

func (v MacOSVersion) IsSequoiaOrLater() bool

IsSequoiaOrLater returns true if running macOS 15 (Sequoia) or later

func (MacOSVersion) IsSonomaOrLater

func (v MacOSVersion) IsSonomaOrLater() bool

IsSonomaOrLater returns true if running macOS 14 (Sonoma) or later

func (MacOSVersion) IsVenturaOrLater

func (v MacOSVersion) IsVenturaOrLater() bool

IsVenturaOrLater returns true if running macOS 13 (Ventura) or later

func (MacOSVersion) ReleaseName

func (v MacOSVersion) ReleaseName() string

ReleaseName returns the marketing name for the macOS version

func (MacOSVersion) String

func (v MacOSVersion) String() string

String returns the version as a string

func (MacOSVersion) UseSystemSettings

func (v MacOSVersion) UseSystemSettings() bool

UseSystemSettings returns true if this version uses "System Settings" instead of "System Preferences" (Ventura and later)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL