yevna

package module
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 16 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run added in v0.1.2

func Run(ctx context.Context, handlers ...Handler) error
Example
package main

import (
	"context"
	"os"

	"github.com/tlipoca9/yevna"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	err := yevna.Run(
		ctx,
		yevna.Exec("echo", "Hello, World!"),
		yevna.Tee(os.Stdout),
	)
	if err != nil {
		panic(err)
	}
}
Output:

Hello, World!

func SetDefault added in v0.1.1

func SetDefault(c *Context)

Types

type Context added in v0.1.0

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

func Default added in v0.1.1

func Default() *Context

func New added in v0.1.2

func New() *Context

func (*Context) Context added in v0.1.2

func (c *Context) Context() context.Context

func (*Context) Next added in v0.1.2

func (c *Context) Next(in any) (any, error)

func (*Context) Run added in v0.1.2

func (c *Context) Run(ctx context.Context, handlers ...Handler) error

func (*Context) Silent added in v0.1.2

func (c *Context) Silent(s ...bool) bool

func (*Context) Tracer added in v0.1.2

func (c *Context) Tracer(t ...tracer.Tracer) tracer.Tracer

func (*Context) Use added in v0.1.3

func (c *Context) Use(handles ...Handler) *Context

func (*Context) Workdir added in v0.1.2

func (c *Context) Workdir(wd ...string) string

type Handler added in v0.1.2

type Handler interface {
	Handle(c *Context, in any) (any, error)
}

Handler is an interface that defines a handler.

func AppendFile added in v0.1.3

func AppendFile(path ...string) Handler

AppendFile returns a Handler that appends to a file. If the file does not exist, it will be created. If the file exists, it will be appended. If you want to truncate the file, use WriteFile instead. It sends input to next handler.

func Cat added in v0.1.3

func Cat(path string) Handler

Cat returns a Handler that reads a file. It drops the input and sends file content (io.Reader) to next handler.

func Cd added in v0.1.2

func Cd(path string) Handler

Cd returns a Handler that changes the working directory. It uses IfExists to check if the path exists. It sends input to next handler.

func Echo added in v0.1.2

func Echo(s string) Handler

Echo returns a Handler that echoes the string. If you want to read from a file, use Cat instead. If you want to read from a reader, use WithReader instead. It drops the input and sends string(converted to io.Reader) to next handler.

func ErrorHandler added in v0.1.3

func ErrorHandler() Handler

ErrorHandler returns a Handler that handles error. It prints the error using fmt.Printf("%+v\n", err).

func Exec added in v0.1.2

func Exec(name string, args ...string) Handler

Exec returns a Handler that executes a command. It uses exec.CommandContext to execute the command.

  • stdin is set to the input.
  • stdout is sent to next handler.
  • stderr is sent to os.Stderr if silent is false.

It starts the command and waits after the next handler is called.

func Execf added in v0.1.2

func Execf(format string, a ...any) Handler

Execf returns a Handler that executes a command. It is a shortcut for Execs(fmt.Sprintf(format, a...)).

func Execs added in v0.1.2

func Execs(cmd string) Handler

Execs returns a Handler that executes a command. It uses shell.Fields to parse the command. It is a shortcut for Exec(shell.Fields(cmd)).

func Gjson added in v0.1.9

func Gjson(path string) Handler

Gjson returns a Handler that extracts the value using the path.

func IfExists added in v0.1.3

func IfExists(path ...string) Handler

IfExists returns a Handler that checks if the file exists. If the file does not exist, it returns an error. If the file exists, it sends input to next handler.

func OpenFile added in v0.1.10

func OpenFile(path string) Handler

OpenFile returns a Handler that opens a file. It sends input to next handler.

func Recover added in v0.1.3

func Recover() Handler

Recover returns a Handler that recovers from panic. It wraps the panic error using errors.Wrap.

func Sed added in v0.1.3

func Sed(cb func(i int, line string) string) Handler

Sed returns a Handler that applies the callback function to each line.

func Silent added in v0.1.2

func Silent(s bool) Handler

Silent returns a Handler that sets the silent flag. If silent is true, Exec will not print stderr. If you want to disable tracing, use Tracer(tracer.Discard) instead. It sends input to next handler.

func Tee added in v0.1.2

func Tee(w ...io.Writer) Handler

Tee returns a Handler that writes to multiple writers. It uses io.Copy to copy the input to the writers and sends input to next handler.

func Tracer added in v0.1.2

func Tracer(t tracer.Tracer) Handler

Tracer returns a Handler that sets the tracer. If you want to disable tracing, use Tracer(tracer.Discard). It sends input to next handler.

func Unmarshal added in v0.1.2

func Unmarshal(p parser.Parser, v any) Handler

Unmarshal returns a Handler that unmarshal the input. It uses the parser.Parser to unmarshal the input to v. It sends v to next handler.

func WithReader added in v0.1.2

func WithReader(r io.Reader) Handler

WithReader returns a Handler that sets the reader. If you want to read from a file, use Cat instead. If you want to read from a string, use Echo instead. It drops the input and sends io.Reader to next handler.

func WriteFile added in v0.1.3

func WriteFile(path ...string) Handler

WriteFile returns a Handler that writes to a file. If the file does not exist, it will be created. If the file exists, it will be truncated. If you want to append to a file, use AppendFile instead. It sends input to next handler.

type HandlerFunc added in v0.1.2

type HandlerFunc func(c *Context, in any) (any, error)

HandlerFunc defines a function type that implements Handler.

func (HandlerFunc) Handle added in v0.1.2

func (f HandlerFunc) Handle(c *Context, in any) (any, error)

Handle implements Handler.

type HandlersChain added in v0.1.2

type HandlersChain []Handler

HandlersChain defines a Handler slice.

func (HandlersChain) Copy added in v0.1.3

func (c HandlersChain) Copy() HandlersChain

Copy returns a copy of the HandlersChain.

type HttpHandler added in v0.1.8

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

func HTTP added in v0.1.8

func HTTP() *HttpHandler

func (*HttpHandler) Handle added in v0.1.8

func (h *HttpHandler) Handle(c *Context, in any) (any, error)

func (*HttpHandler) MakeRequest added in v0.1.8

func (h *HttpHandler) MakeRequest(fn func(c *req.Client, in any) *req.Request) *HttpHandler

func (*HttpHandler) WithClient added in v0.1.8

func (h *HttpHandler) WithClient(client *req.Client) *HttpHandler

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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