helper

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 1, 2021 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package helper provides helpers for modules, in order to simplify their development

Index

Constants

This section is empty.

Variables

View Source
var Dryrun bool

Dryrun allows running instructions without system modification

View Source
var SudoPassword string

SudoPassword must contain the sudo password, in order to user ExecSudo. This password may be set with the sudo-password instruction.

Functions

func DownloadJSON

func DownloadJSON(url string, destination interface{}) error

DownloadJSON downloads the given URL and decodes it to the given destination.

The URL is downloaded even in dry-run mode.

func Exec

func Exec(env []string, cmd string, args ...string) ([]byte, error)

Exec executes a command as the current user, returning its stdin and stdout outputs combined.

If command fails, its combined output is in the error message.

func ExecSudo

func ExecSudo(env []string, args ...string) ([]byte, error)

ExecSudo executes a command as root by using sudo, returning its stdin and stdout outputs combined.

If command fails, its combined output is in the error message.

func HTTPGet

func HTTPGet(url string) ([]byte, error)

HTTPGet downloads the given URL and returns it as a []byte.

The URL is downloaded even in dry-run mode.

func HTTPPost

func HTTPPost(url string, payload []byte) ([]byte, error)

HTTPPost uses method POST on the given URL and returns the result as a []byte.

The URL is downloaded even in dry-run mode.

func MapStringString

func MapStringString(input interface{}) (map[string]string, error)

MapStringString transforms an interface to a map from strings to strings

func Path

func Path(str string) string

Path returns the absolute path for the given file path. Relative paths are prefixed with the home directory. if the argument is empty, it returns the home directory path.

func SliceString

func SliceString(input interface{}) ([]string, error)

SliceString transforms a slice of interfaces to a slice of strings

func Store

func Store(key, value string)

Store adds a value to the store. Ignored if key is empty.

func String

func String(v interface{}) (str string, err error)

String returns a string from an interface, replacing occurrences of "<xxx>" with the value of xxx in the store.

Types

type ResultStatus

type ResultStatus int

ResultStatus is returned by helper to inform the module of the result status

const (
	// ResultError means there is an error
	ResultError ResultStatus = iota
	// ResultSuccess means the requested action is successful
	ResultSuccess
	// ResultDryrun means the action is not done because of dry-run mode
	ResultDryrun
	// ResultAlready means the action was already done
	ResultAlready
)

func Directory

func Directory(path string, root bool) (ResultStatus, error)

Directory creates a new directory, and its parents if needed. Directory owner is root if the root boolean is true.

If the directory already exists, it returns ResultAlready.

In dry-run mode, ResultDryrun is returned.

func DownloadFile

func DownloadFile(url, path string, out output.Output) (ResultStatus, error)

DownloadFile downloads the given URL to the given path, writing the percentage to the given output if provided.

If in dry-run mode, it returns ResultDryrun right away, without downloading.

func ExtractTarxz

func ExtractTarxz(tarxzfilepath, dest string) (ResultStatus, error)

ExtractTarxz extracts a tar.xz archive.

In dry-run mode, only return ResultDryrun.

func ExtractZip

func ExtractZip(zipfilepath, dest string, out output.Output) (ResultStatus, error)

ExtractZip unzips the given file into the given destination.

In dry-run mode, only return ResultDryrun.

If out is provided, a "X on Y" loader is provided to the user.

func File

func File(path string, content []byte, permission os.FileMode, root bool) (ResultStatus, error)

File writes the provided content to the provided path, with the given permission. The file owner is root if the root boolean is true.

If the file already exists and already has the same content, ResultAlready is returned.

In dry-run mode, comparison is done (thus, ResultAlready may be returned) but ResultDryrun is returned if file need to be created or modified.

func GitClone

func GitClone(repo, dest string, depth int) (ResultStatus, error)

GitClone clones a Git repository to the provided destination.

depth is the number of commits to fetch for clone (if 0, only fetch HEAD).

If the provided destination is already this repository, return ResultAlready

In dry-run mode, do not clone, return ResultDryrun

func GitPull

func GitPull(path string) (ResultStatus, error)

GitPull pulls the latest HEAD of the given Git repository from origin.

If the provided destination is already at the latest HEAD from origin, return ResultAlready.

In dry-run mode, do not pull, return ResultDryrun.

func Remove

func Remove(path string) (ResultStatus, error)

Remove removes a file or empty directory from the system.

If the path is a non-empty directory, it returns an error.

If the path does not exist, it returns ResultAlready.

In dry-run mode, file or directory is not removed and ResultDryrun is returned.

func Symlink(path, target string) (status ResultStatus, err error)

Symlink creates a symbolic link.

If the symlink already exists and points to the specified target, return ResultAlready.

If in dry-run mode, do not create nor change the symlink.

Jump to

Keyboard shortcuts

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