io

package
v5.12.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package io provides utilities for input and output operations.

This package contains utilities for reading from and writing to files, along with various I/O helper functions for file operations, including configuration validation utilities.

Key functionality:

  • File reading: ReadFileSafe, FindFile
  • File writing: TryWriteFile
  • Path operations: ExpandHomePath
  • String utilities: TrimNonEmpty

Subpackages:

  • config-manager: Configuration loading and management
  • generator: Template and configuration generation
  • marshaller: Serialization and deserialization
  • scaffolder: Project scaffolding and file generation
  • validator: Configuration validation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPathOutsideBase is returned when a file path is outside the specified base directory.
	ErrPathOutsideBase = errors.New("invalid path: file is outside base directory")

	// ErrEmptyOutputPath is returned when the output path is empty.
	ErrEmptyOutputPath = errors.New("output path cannot be empty")

	// ErrBasePath is returned when the base path is empty.
	ErrBasePath = errors.New("base path cannot be empty")
)

File operation errors.

Functions

func ExpandHomePath

func ExpandHomePath(path string) (string, error)

ExpandHomePath expands a path beginning with ~/ to the user's home directory and converts relative paths to absolute paths.

Parameters:

  • path: The path to expand (e.g., "~/config.yaml", "./config.yaml", or "/absolute/path")

Returns:

  • string: The expanded and absolute path
  • error: Error if unable to get current user information or convert to absolute path

func FindFile

func FindFile(filePath string) (string, error)

FindFile resolves a file path with directory traversal. For absolute paths, returns the path as-is. For relative paths, traverses up from the current directory to find the file.

Parameters:

  • filePath: The file path to resolve

Returns:

  • string: The resolved absolute path if found, or the original path if not found
  • error: Error if unable to get current directory

func ReadFileSafe

func ReadFileSafe(basePath, filePath string) ([]byte, error)

ReadFileSafe reads the file at filePath only if it is located within basePath. It resolves absolute paths and rejects reads where the resolved path is outside basePath (prevents path traversal and accidental file inclusion).

Parameters:

  • basePath: The base directory that filePath must be within
  • filePath: The file path to read (must be within basePath)

Returns:

  • []byte: The file contents
  • error: ErrPathOutsideBase if path is outside base, or read error

func TrimNonEmpty

func TrimNonEmpty(s string) (string, bool)

TrimNonEmpty returns the trimmed string and whether it's non-empty. This consolidates the common pattern of trimming and checking for emptiness.

Parameters:

  • s: The string to trim and check

Returns:

  • string: The trimmed string
  • bool: True if the trimmed string is non-empty, false otherwise

func TryWriteFile

func TryWriteFile(content string, output string, force bool) (string, error)

TryWriteFile writes content to a file path, handling force/overwrite logic. It validates that the output path doesn't contain path traversal attempts.

Parameters:

  • content: The content to write to the file
  • output: The output file path
  • force: If true, overwrites existing files; if false, skips existing files

Returns:

  • string: The content that was written (for chaining)
  • error: ErrEmptyOutputPath if output is empty, or write error

Caller responsibilities:

  • Ensure the output path is within intended bounds
  • Handle the returned content appropriately

Types

This section is empty.

Directories

Path Synopsis
Package configmanager provides centralized configuration management for KSail.
Package configmanager provides centralized configuration management for KSail.
helpers
Package helpers provides common functionality for config managers to eliminate duplication.
Package helpers provides common functionality for config managers to eliminate duplication.
k3d
Package k3d provides configuration management for K3d clusters.
Package k3d provides configuration management for K3d clusters.
kind
Package kind provides configuration management for Kind clusters.
Package kind provides configuration management for Kind clusters.
ksail
Package configmanager provides configuration management for KSail v1alpha1.Cluster configurations.
Package configmanager provides configuration management for KSail v1alpha1.Cluster configurations.
talos
Package talos provides configuration management for Talos cluster patches.
Package talos provides configuration management for Talos cluster patches.
Package detector provides utilities for detecting existing Kubernetes resources in the source directory, such as GitOps Custom Resources managed by KSail.
Package detector provides utilities for detecting existing Kubernetes resources in the source directory, such as GitOps Custom Resources managed by KSail.
Package generator provides an interface for generating files from code.
Package generator provides an interface for generating files from code.
argocd
Package argocd provides generators for ArgoCD GitOps resources.
Package argocd provides generators for ArgoCD GitOps resources.
flux
Package flux provides generators for Flux GitOps resources.
Package flux provides generators for Flux GitOps resources.
k3d
Package k3dgenerator provides utilities for generating K3d cluster configurations.
Package k3dgenerator provides utilities for generating K3d cluster configurations.
kind
Package kindgenerator provides utilities for generating Kind cluster configurations.
Package kindgenerator provides utilities for generating Kind cluster configurations.
kustomization
Package kustomizationgenerator provides utilities for generating kustomization.yaml files.
Package kustomizationgenerator provides utilities for generating kustomization.yaml files.
talos
Package talosgenerator provides a generator for Talos project scaffolding.
Package talosgenerator provides a generator for Talos project scaffolding.
yaml
Package yamlgenerator provides YAML generation functionality for arbitrary models.
Package yamlgenerator provides YAML generation functionality for arbitrary models.
Package marshaller provides functionality for marshaling and unmarshaling resources.
Package marshaller provides functionality for marshaling and unmarshaling resources.
yaml
Package yamlmarshaller provides functionality for marshaling and unmarshaling YAML documents.
Package yamlmarshaller provides functionality for marshaling and unmarshaling YAML documents.
Package scaffolder provides utilities for scaffolding KSail project files and configuration.
Package scaffolder provides utilities for scaffolding KSail project files and configuration.
Package validator provides interfaces for configuration file validation.
Package validator provides interfaces for configuration file validation.
k3d
Package k3d provides K3d configuration validation functionality.
Package k3d provides K3d configuration validation functionality.
kind
Package kind provides Kind configuration validation functionality.
Package kind provides Kind configuration validation functionality.
ksail
Package ksail provides validation for KSail cluster configurations.
Package ksail provides validation for KSail cluster configurations.
metadata
Package metadata provides shared metadata validation utilities used across multiple validators.
Package metadata provides shared metadata validation utilities used across multiple validators.

Jump to

Keyboard shortcuts

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