utils

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Copyright © 2021 Antoine Martin <antoine@openance.com>

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Package utils provides utility functions and interfaces for filesystem operations. This file defines a FileSystem interface that abstracts filesystem operations, allowing for both real filesystem access and testing with in-memory filesystems.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecuteIfExist added in v0.1.8

func ExecuteIfExist(file string, fn func() error) error

ExecuteIfExist executes the function fn if the file exists.

func ExecuteIfNotExist

func ExecuteIfNotExist(file string, fn func() error) error

ExecuteIfNotExist executes the function fn if the file doesn't exist.

func ExecuteOnExistence added in v0.1.8

func ExecuteOnExistence(file string, existence bool, fn func() error) error

ExecuteOnExistence executes the function fn if the file existence is the one given by the parameter.

func GetOutboundIP

func GetOutboundIP() (net.IP, error)

GetOutboundIP returns the preferred outbound ip of this machine.

func IsOnWSL added in v0.2.0

func IsOnWSL() bool

func MoveFileIfExists

func MoveFileIfExists(src, dst string) error

MoveFileIfExists moves the file src to the destination dst if it exists.

Types

type CommandExecutor added in v0.2.0

type CommandExecutor struct{}

func (*CommandExecutor) Pipe added in v0.2.0

func (c *CommandExecutor) Pipe(
	stdin io.Reader,
	combined bool,
	cmd string,
	arguments ...string,
) ([]byte, error)

func (*CommandExecutor) Run added in v0.2.0

func (c *CommandExecutor) Run(combined bool, cmd string, arguments ...string) ([]byte, error)

type Executor added in v0.2.0

type Executor interface {
	Run(combined bool, cmd string, arguments ...string) ([]byte, error)
	Pipe(stdin io.Reader, combined bool, cmd string, arguments ...string) ([]byte, error)
}

The Executor interface provides a way to run commands and pipe input to them.

var Exec Executor = &CommandExecutor{}

type ExecutorFunction added in v0.2.0

type ExecutorFunction = func(cmd string, arguments ...string) ([]byte, error)

ExecutorFunction is a function that executes a command and returns its input.

type FileSystem added in v0.6.1

type FileSystem interface {
	// ReadFile reads the file at the given path and returns its contents.
	// It returns an error if the file does not exist or cannot be read.
	ReadFile(path string) ([]byte, error)

	// WriteFile writes data to the file at the given path with the specified permissions.
	// It creates the file if it does not exist, or truncates it if it does.
	WriteFile(path string, data []byte, perm os.FileMode) error

	// Stat returns the FileInfo for the file at the given path.
	// It returns an error if the file does not exist or cannot be accessed.
	Stat(path string) (os.FileInfo, error)

	// Create creates or truncates the file at the given path.
	// It returns the opened file or an error if the operation fails.
	Create(path string) (afero.File, error)

	// Open opens the file at the given path for reading.
	// It returns an error if the file does not exist or cannot be opened.
	Open(path string) (afero.File, error)

	// OpenFile opens the file at the given path with the specified flags and permissions.
	// It returns the opened file or an error if the operation fails.
	OpenFile(path string, flag int, perm os.FileMode) (afero.File, error)

	// Remove deletes the file at the given path.
	// It returns an error if the file does not exist or cannot be deleted.
	Remove(path string) error

	// RemoveAll deletes the file or directory at the given path and all its contents.
	// It returns an error if the operation fails.
	RemoveAll(path string) error

	// MkdirAll creates a directory along with any necessary parent directories.
	// It does nothing if the directory already exists.
	MkdirAll(path string, perm os.FileMode) error

	// Symlink creates a symbolic link at newName pointing to oldName.
	// It returns an error if the operation fails or is not supported by the filesystem.
	Symlink(oldName, newName string) error

	// ReadDir reads the directory at the given path and returns a list of file entries.
	// It returns an error if the directory does not exist or cannot be read.
	ReadDir(dirname string) ([]os.FileInfo, error)

	// Exists checks if the file or directory at the given path exists.
	// It returns true if the path exists, false otherwise. An error is returned if the check fails.
	Exists(path string) (bool, error)

	// Pipe creates a script.Pipe for the file at the given path.
	// It returns the pipe for further processing or an error if the file cannot be opened.
	Pipe(path string) *script.Pipe

	// WritePipe writes the contents of the given script.Pipe to the file at the specified path.
	// It returns the number of bytes written and an error if the operation fails.
	WritePipe(path string, pipe *script.Pipe, flag int, perm os.FileMode) (int64, error)

	// DirExists checks if the directory at the given path exists.
	DirExists(path string) (bool, error)

	// MoveFile moves a file from oldPath to newPath.
	Rename(oldPath, newPath string) error
}

FileSystem defines an interface for filesystem operations. It abstracts the underlying filesystem implementation, allowing for easy testing with mock or in-memory filesystems while maintaining compatibility with real filesystem operations.

var FS FileSystem = &aferoFS{fs: afero.NewOsFs()}

FS is the global FileSystem instance used throughout the application. It defaults to a real filesystem (NewOsFs) but can be swapped for testing purposes.

func NewBasePathFS added in v0.6.1

func NewBasePathFS(baseFS afero.Fs, basePath string) FileSystem

NewBasePathFS creates a FileSystem that is rooted at the given basePath. All file operations will be relative to this base path.

func NewMemMapFS added in v0.6.1

func NewMemMapFS() FileSystem

For tests.

Jump to

Keyboard shortcuts

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