yevna

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: MIT Imports: 16 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultHTTPClient = &HTTPClient{client: http.DefaultClient}

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) 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 HTTPClient added in v0.2.1

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

func (*HTTPClient) Do added in v0.2.1

func (h *HTTPClient) Do(fn func(c *Context, in any) (*http.Request, error)) Handler

func (*HTTPClient) SetClient added in v0.2.1

func (h *HTTPClient) SetClient(client *http.Client) *HTTPClient

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 Chdir added in v0.2.1

func Chdir(path string) Handler

Chdir returns a Handler that changes the working directory. It uses IfExists to check if the path exists. It sends input 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 ForEachLine added in v0.2.1

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

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

func Gjson added in v0.1.9

func Gjson(path string) Handler

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

func HTTP added in v0.1.8

func HTTP(fn func(c *Context, in any) (*http.Request, error)) Handler

func Input added in v0.2.1

func Input(a any) Handler

Input returns a Handler that sets the input. It sends the 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 Output added in v0.2.1

func Output[T any](a *T) Handler

Output returns a Handler that assigns the previous handler's output to a. If successful, it sends original input to the 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 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. It sends original 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 ToStr added in v0.2.1

func ToStr() Handler

ToStr returns a Handler that converts input to string. It tries to convert input to string using the following rules:

  • string: returns the string.
  • []byte: returns the string.
  • []rune: returns the string.
  • io.Reader: reads the reader and returns the string.
  • any other type: uses fmt.Sprint to convert to string.

func Unmarshal added in v0.1.2

func Unmarshal[T any](p parser.Parser, v *T) 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 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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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