system

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: MIT Imports: 14 Imported by: 0

README

go-system

Go library providing OS-level utilities: environment/config loading, shell command execution, file checksums, and debugger detection.

Installation

go get github.com/eslider/go-system

Features

  • Load .env files and YAML configs with environment-based sections
  • Execute shell commands with captured stdout/stderr and exit codes
  • SHA-256 file checksums for multipart uploads
  • Parse file paths into components (name, basename, extension, directory)
  • Detect if running under the Delve debugger

Quick Start

Load Environment Variables
system.LoadEnvs("/path/to/config")
dbURL := os.Getenv("DATABASE_URL")
Read YAML Config
type AppConfig struct {
    Port     int    `mapstructure:"port"`
    Database string `mapstructure:"database"`
}

var cfg AppConfig
system.ReadConfig("config.yml", "production", &cfg)
Execute Shell Commands
result, err := system.Exec("git", "status", "--short")
if err != nil {
    log.Fatal(err)
}
fmt.Println(result.StdOut)
File Path Info
info := system.GetFileInfo("/data/reports/q4-2025.csv")
// info.Name     = "q4-2025.csv"
// info.BaseName = "q4-2025"
// info.Ext      = ".csv"
// info.Dir      = "/data/reports/"

API

Function Description
LoadEnvs(path) Load .env and .env.default files
ReadConfig(path, env, ptr) Read YAML config for environment
Exec(args...) Run shell command, capture output
CheckSum(file) SHA-256 hex digest of multipart file
GetFileInfo(path) Parse file path components
IsFileExists(path) Check if file exists (not directory)
IsLaunchedByDebugger() Detect Delve debugger

License

MIT

Documentation

Overview

Package system provides OS-level utilities: environment/config loading, shell command execution, file checksums, and debugger detection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckSum

func CheckSum(file multipart.File) (string, error)

CheckSum calculates the SHA-256 hex digest of a multipart file, then resets the file pointer to the beginning.

func IsFileExists

func IsFileExists(f string) bool

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

func IsLaunchedByDebugger

func IsLaunchedByDebugger() bool

IsLaunchedByDebugger checks if the current process was launched by the Delve debugger. Requires gops (https://github.com/google/gops) to be in PATH.

func LoadEnvs

func LoadEnvs(path string) error

LoadEnvs loads environment variables from .env and .env.default files in the given directory.

func ReadConfig

func ReadConfig(path string, env string, pointer interface{}) error

ReadConfig reads a YAML configuration file, selects the given environment section, and decodes it into the provided pointer using mapstructure.

Types

type FileInfo

type FileInfo struct {
	Name     string // Full file name with extension
	BaseName string // File name without extension
	Dir      string // Directory path
	Ext      string // File extension including dot
}

FileInfo holds parsed components of a file path.

func GetFileInfo

func GetFileInfo(path string) FileInfo

GetFileInfo splits a file path into its components.

type ShellCommandResult

type ShellCommandResult struct {
	StdOut   string
	StdErr   string
	ExitCode int
	Args     []string
}

ShellCommandResult holds the output and exit code of a shell command.

func Exec

func Exec(args ...string) (*ShellCommandResult, error)

Exec runs a shell command and returns the captured stdout, stderr, and exit code.

func (ShellCommandResult) GetError

func (r ShellCommandResult) GetError() error

GetError returns stderr content as an error.

func (ShellCommandResult) HasError

func (r ShellCommandResult) HasError() bool

HasError returns true if stderr contains output.

Jump to

Keyboard shortcuts

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