vai

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: Apache-2.0 Imports: 22 Imported by: 0

README

vai

GitHub Tag GitHub go.mod Go version GitHub License GitHub code size in bytes

A simple task runner. Imagine GitHub actions and Makefile had a baby.

[!CAUTION] This project is still in its early stages. Expect breaking changes.

Installation

go install github.com/Noxsios/vai/cmd/vai@latest

To update to the latest version:

rm $(which vai)
go install github.com/Noxsios/vai/cmd/vai@latest

Example

cat <<EOF > vai.yaml
echo:
  - cmd: echo "\$message"
    with:
      message: \${{ input }}

echo-matrix:
  - cmd: echo "\$message"
    matrix:
      message: ["Hello", "World!"]

remote-echo-short:
  - uses: github.com/Noxsios/vai@main?task=simple#tasks/echo.yaml
    with:
      message: hello from main
EOF
$ vai echo --with message="Hello World!"
echo "$message"

Hello World!

$ vai echo-matrix
echo "$message"

Hello, World!
echo "$message"

General Kenobi!

Learn more w/ vai --help

Schema Validation

Enabling schema validation in VSCode:

    "yaml.schemas": {
        "https://raw.githubusercontent.com/Noxsios/vai/main/vai.schema.json": "vai.yaml",
    },

Per file basis:

# yaml-language-server: $schema=https://raw.githubusercontent.com/Noxsios/vai/main/vai.schema.json

Documentation

Overview

Package vai provides a simple task runner.

Index

Constants

View Source
const (
	// CacheEnvVar is the environment variable for the cache directory.
	CacheEnvVar = "VAI_CACHE"
)
View Source
const DefaultFileName = "vai.yaml"

DefaultFileName is the default file name

View Source
const DefaultTaskName = "default"

DefaultTaskName is the default task name

View Source
const UsesPrefix = "pkg:vai/"

UsesPrefix is the prefix for remote tasks.

Variables

View Source
var ErrHashMismatch = fmt.Errorf("hash mismatch")

ErrHashMismatch is returned when the hash of the stored file does not match the hash in the index.

View Source
var Force = false

Force is a global flag to bypass SHA256 checksum verification for cached remote files.

View Source
var TaskNamePattern = regexp.MustCompile("^[_a-zA-Z][a-zA-Z0-9_-]*$")

TaskNamePattern is a regular expression for valid task names, it is also used for step IDs

Functions

func ConfirmSHAOverwrite

func ConfirmSHAOverwrite() (bool, error)

ConfirmSHAOverwrite asks the user if they want to overwrite the SHA and bypass the check.

func IsHashMismatch

func IsHashMismatch(err error) bool

IsHashMismatch returns true if the error is a hash mismatch error.

func Logger

func Logger() *log.Logger

Logger returns the global logger.

func ParseOutputFile added in v0.1.1

func ParseOutputFile(outFilePath string) (map[string]string, error)

ParseOutputFile parses the output file of a step

func PerformLookups added in v0.1.2

func PerformLookups(outer, local, persisted With, outputs CommandOutputs, mi MatrixInstance) (With, With, error)

PerformLookups does the following:

1. Templating: executes the `input`, `default`, `persist`, and `from` functions against the `outer` and `local` With maps

2. Merging: merges the `persisted` and `local` With maps, with `local` taking precedence

3. MatrixInstance: merges the `mi` MatrixInstance into the result, with `mi` taking precedence

func Run

func Run(wf Workflow, taskName string, outer With) error

Run executes a task in a workflow with the given inputs.

For all steps that have a `uses` step, this function will be called recursively.

func RunUses added in v0.1.4

func RunUses(uses string, with With) error

RunUses runs a task from a remote workflow source.

func SetLogLevel

func SetLogLevel(level log.Level)

SetLogLevel sets the global log level.

func Validate added in v0.1.4

func Validate(wf Workflow) error

Validate validates a workflow

func WorkFlowSchema added in v0.1.4

func WorkFlowSchema() *jsonschema.Schema

WorkFlowSchema returns a JSON schema for a vai workflow

Types

type CacheIndex

type CacheIndex struct {
	Files []struct {
		Name   string `json:"name"`
		Digest string `json:"digest"`
	} `json:"files"`
}

CacheIndex is a list of files and their digests.

func (*CacheIndex) Add

func (c *CacheIndex) Add(key, value string)

Add adds an entry to the index.

If the key already exists in the index and the value is the same, nothing will happen.

If the key already exists in the index and the value is different, the key will be removed and re-added.

If the key does not exist in the index, it will be added.

func (CacheIndex) Find

func (c CacheIndex) Find(key string) (string, bool)

Find returns the digest of a file by name and a boolean indicating if the file was found.

func (*CacheIndex) Remove

func (c *CacheIndex) Remove(key string)

Remove removes an entry from the index.

type CommandOutputs added in v0.1.1

type CommandOutputs map[string]map[string]string

CommandOutputs is a map of step IDs to their outputs.

It is currently NOT goroutine safe.

type Matrix

type Matrix map[string][]any

Matrix is a map[]{string|int|bool}

Type safety cannot currently be enforced at compile time, and is instead enforced at runtime using JSON schema validation

example (YAML):

matrix:
  os: [linux, darwin]
  arch: [amd64, arm64]

type MatrixInstance

type MatrixInstance map[string]any

MatrixInstance is a map[string]{string|int|bool}

Type safety cannot currently be enforced at compile time, and is instead enforced at runtime using JSON schema validation

example:

mi := MatrixInstance{
  "os": "linux",
  "latest": true,
}

type Operation

type Operation int

Operation is an enum for the type of operation a step is performing

const (
	// OperationRun is a step that runs a command
	OperationRun Operation = iota
	// OperationUses is a step that calls another task
	OperationUses
)

type Step added in v0.1.1

type Step struct {
	// CMD is the command to run
	CMD string `json:"cmd,omitempty"`
	// Uses is a reference to a remote task
	Uses string `json:"uses,omitempty"`
	// With is a map of additional parameters for the step/task call
	With `json:"with,omitempty"`
	// Matrix is a matrix of parameters to run the step/task with
	Matrix `json:"matrix,omitempty"`
	// ID is a unique identifier for the step
	ID string `json:"id,omitempty"`
	// Description is a description of the step
	Description string `json:"description,omitempty"`
}

Step is a single step in a task

While a step can have both `cmd` and `uses` fields, only one of them can be set at a time.

This is enforced by JSON schema validation.

TODO: - add `if` and `continue-on-error` fields? - add `timeout` field?

func (Step) JSONSchemaExtend added in v0.1.4

func (Step) JSONSchemaExtend(schema *jsonschema.Schema)

JSONSchemaExtend extends the JSON schema for a step

func (Step) Operation added in v0.1.1

func (s Step) Operation() Operation

Operation returns the type of operation the step is performing

func (Step) Run added in v0.1.1

func (s Step) Run(with With, outputFilePath string) error

Run executes the CMD field of a step

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is a cache for storing and retrieving remote workflows.

func DefaultStore

func DefaultStore() (*Store, error)

DefaultStore creates a new store in the default location:

$VAI_CACHE || $HOME/.vai/cache

func NewStore

func NewStore(path string) (*Store, error)

NewStore creates a new store at the given path.

func (*Store) Delete

func (s *Store) Delete(key string) error

Delete a workflow from the store.

func (*Store) Exists

func (s *Store) Exists(key string, r io.Reader) (bool, error)

Exists checks if a workflow exists in the store.

func (*Store) Fetch

func (s *Store) Fetch(key string) (Workflow, error)

Fetch retrieves a workflow from the store by key (SHA256).

func (*Store) Store

func (s *Store) Store(key string, r io.Reader) error

Store a workflow in the store.

type Task

type Task []Step

Task is a list of steps

type With

type With map[string]WithEntry

With is a map of string keys and WithEntry values used to pass parameters to called tasks and within steps

Each key will be mapped to an equivalent environment variable when the command is run. eg. `with: {foo: bar}` will be passed as `foo=bar` to the command.

type WithEntry added in v0.1.1

type WithEntry any

WithEntry is a single entry in a With map

type Workflow

type Workflow map[string]Task

Workflow is a map of tasks, where the key is the task name

This is the main structure that represents `vai.yaml` and other vai workflow files

func FetchIntoStore added in v0.1.3

func FetchIntoStore(pURL packageurl.PackageURL, store *Store) (Workflow, error)

FetchIntoStore fetches and stores a remote workflow into a given store.

func Read added in v0.1.4

func Read(filename string) (Workflow, error)

Read reads a workflow from a file

func ReadAndValidate added in v0.1.4

func ReadAndValidate(filename string) (Workflow, error)

ReadAndValidate reads and validates a workflow

func (Workflow) Find

func (wf Workflow) Find(call string) (Task, error)

Find returns a task by name

If the task is not found, an error is returned

Directories

Path Synopsis
cmd
Package cmd provides the root command for the vai CLI.
Package cmd provides the root command for the vai CLI.
vai
Package main is the entry point for the application
Package main is the entry point for the application
Package main provides the entry point for the application.
Package main provides the entry point for the application.

Jump to

Keyboard shortcuts

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