Documentation
¶
Index ¶
- Variables
- func Run(ctx context.Context, handlers ...Handler) error
- func SetDefault(c *Context)
- type Context
- type HTTPClient
- type Handler
- func AppendFile(path ...string) Handler
- func Chdir(path string) Handler
- func ErrorHandler() Handler
- func Exec(name string, args ...string) Handler
- func Execf(format string, a ...any) Handler
- func Execs(cmd string) Handler
- func ForEachLine(cb func(i int, line string) string) Handler
- func Gjson(path string) Handler
- func HTTP(fn func(c *Context, in any) (*http.Request, error)) Handler
- func Input(a any) Handler
- func OpenFile(path string) Handler
- func Output[T any](a *T) Handler
- func Recover() Handler
- func Silent(s bool) Handler
- func Tee(w ...io.Writer) Handler
- func ToStr() Handler
- func Unmarshal[T any](p parser.Parser, v *T) Handler
- func WriteFile(path ...string) Handler
- type HandlerFunc
- type HandlersChain
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultHTTPClient = &HTTPClient{client: http.DefaultClient}
Functions ¶
func Run ¶ added in v0.1.2
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
}
type HTTPClient ¶ added in v0.2.1
type HTTPClient struct {
// contains filtered or unexported fields
}
func (*HTTPClient) SetClient ¶ added in v0.2.1
func (h *HTTPClient) SetClient(client *http.Client) *HTTPClient
type Handler ¶ added in v0.1.2
Handler is an interface that defines a handler.
func AppendFile ¶ added in v0.1.3
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
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
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
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
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
ForEachLine returns a Handler that applies the callback function to each line.
func Input ¶ added in v0.2.1
Input returns a Handler that sets the input. It sends the input to next handler.
func OpenFile ¶ added in v0.1.10
OpenFile returns a Handler that opens a file. It sends input to next handler.
func Output ¶ added in v0.2.1
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
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
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
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.
type HandlerFunc ¶ added in v0.1.2
HandlerFunc defines a function type that 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.