ensure

package module
v0.0.0-...-7229ba2 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2020 License: BSD-2-Clause Imports: 2 Imported by: 0

README

ensure

Ensure is a small library to help ensure state on a UNIX system. It is currently pretty basic and doesn't have much to offer apart from a file/directory and exec helper.

But with these small pieces more should easily be possible and go offers more than enough to implement other functions on top of the ensurer interface and the runner to ensure the state.

how to use

To use this library use one of the provided resources or implement one yourself with the Ensurer interface.

Either call Ensure on the resource yourself or let one of the runners do it for all your resources.

package main

import (
  "io/ioutil"
  "os"
  "path"

  "git.zero-knowledge.org/gibheer/ensure"
  "git.zero-knowledge.org/gibheer/ensure/file"
)

func main() {
  tmpDir, _ := ioutil.TempDir("", "")
  defer os.RemoveAll(tmpDir)
  f := &file.F{
    Path: path.Join(tmpDir, "test")
  }
  if !f.Is() {
    if err := f.Ensure(); err != nil {
      fmt.Fatalf("could not ensure file: %s\n", err)
    }
  }

  // Use a runner to ensure multiple resources blocked by a check.
  // In case of the runner, ensure will also run Is() before making changes.
  // Better read the documentation, not all resources do that or can do that.
  r := &ensure.Runner{
    States: []ensure.Ensurer{f},
    Is: f.Is,
  }
  if err := r.Ensure(); err != nil {
    fmt.Fatalf("could not ensure directory: %s", err)
  }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckEnsurer

type CheckEnsurer interface {
	Is() bool
	Ensure() error
}

CheckEnsurer is an etension of ensurer, which also provides an Is function to check the state of the resource.

type Enforced

type Enforced func() bool

Enforced checks if state is already enforced in the environment. When it is, it must return true.

type Ensurer

type Ensurer interface {
	// Ensure will modify the environment so that it represents the wanted
	// state.
	Ensure() error
}

State represents a wanted state that should be pushed to the environment.

func WithCheck

func WithCheck(en CheckEnsurer) Ensurer

WithCheck creates an Ensurer instance that runs is before Ensure.

type Runner

type Runner struct {
	// Is is checked before enforcing all states.
	// The states will not be enforced when the function is returning true.
	Is Enforced

	// Parallel defines if the states are ensured in parallel. There is no
	// dependency management at work. The requirements must already be met
	// beforehand.
	// When parallel is set to true the processing will not be halted on the
	// first error. Instead all errors will be collected and returned.
	Parallel bool

	// States is the list of states to ensure when Ensure() is called.
	States []Ensurer

	// When parallel mode is used, the number of threads to spawn can be set.
	// When not set, every state will spawn a thread.
	Workers int
}

Runner takes a list of ensurers and enforces their state. The runner is not transactional.

func (*Runner) Ensure

func (r *Runner) Ensure() error

Ensure will call Ensure() on every state. When Parallel is true, all states will be ensured in parallel. The number of threads that will be spawned can be controlled by the Workers attribute. In case of an error the processing will be aborted. When Parallel is true all errors are collected and returned as one and the processing will not be halted.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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