Rename Append to Merge, replace strings_Join with slice_Join, and auto-detect non-TTY output

- Rename Append to Merge on Cookies, Headers, and Params types and simplify Parse to use direct append
- Replace strings_Join(sep, ...values) with slice_Join(slice, sep) for consistency with slice-based template functions
- Auto-enable quiet mode when stdout is not a terminal
- Remove ErrCLINoArgs check to allow running sarin without arguments
- Add -it flag to Docker examples in docs
This commit is contained in:
2026-02-15 02:56:32 +04:00
parent a3e20cd3d3
commit d346067e8a
10 changed files with 23 additions and 37 deletions

View File

@@ -824,19 +824,19 @@ quiet: true
**Basic Docker usage:** **Basic Docker usage:**
```sh ```sh
docker run --rm aykhans/sarin -U http://example.com -r 1000 -c 10 docker run -it --rm aykhans/sarin -U http://example.com -r 1000 -c 10
``` ```
**With local config file:** **With local config file:**
```sh ```sh
docker run --rm -v $(pwd)/config.yaml:/config.yaml aykhans/sarin -f /config.yaml docker run -it --rm -v $(pwd)/config.yaml:/config.yaml aykhans/sarin -f /config.yaml
``` ```
**With remote config file:** **With remote config file:**
```sh ```sh
docker run --rm aykhans/sarin -f https://example.com/config.yaml docker run -it --rm aykhans/sarin -f https://example.com/config.yaml
``` ```
**Interactive mode with TTY:** **Interactive mode with TTY:**

View File

@@ -98,14 +98,14 @@ sarin -U http://example.com/users \
| `strings_Truncate(s string, n int)` | Truncate to `n` characters with ellipsis | `{{ strings_Truncate "hello world" 5 }}``hello...` | | `strings_Truncate(s string, n int)` | Truncate to `n` characters with ellipsis | `{{ strings_Truncate "hello world" 5 }}``hello...` |
| `strings_TrimPrefix(s string, prefix string)` | Remove prefix from string | `{{ strings_TrimPrefix "hello" "he" }}``llo` | | `strings_TrimPrefix(s string, prefix string)` | Remove prefix from string | `{{ strings_TrimPrefix "hello" "he" }}``llo` |
| `strings_TrimSuffix(s string, suffix string)` | Remove suffix from string | `{{ strings_TrimSuffix "hello" "lo" }}``hel` | | `strings_TrimSuffix(s string, suffix string)` | Remove suffix from string | `{{ strings_TrimSuffix "hello" "lo" }}``hel` |
| `strings_Join(sep string, values ...string)` | Join strings with separator | `{{ strings_Join "-" "a" "b" "c" }}``a-b-c` |
### Collection Functions ### Collection Functions
| Function | Description | Example | | Function | Description | Example |
| ----------------------------- | --------------------------------------------- | -------------------------------------------- | | ---------------------------------------- | --------------------------------------------- | -------------------------------------------------------- |
| `dict_Str(pairs ...string)` | Create string dictionary from key-value pairs | `{{ dict_Str "key1" "val1" "key2" "val2" }}` | | `dict_Str(pairs ...string)` | Create string dictionary from key-value pairs | `{{ dict_Str "key1" "val1" "key2" "val2" }}` |
| `slice_Str(values ...string)` | Create string slice | `{{ slice_Str "a" "b" "c" }}` | | `slice_Str(values ...string)` | Create string slice | `{{ slice_Str "a" "b" "c" }}` |
| `slice_Join(slice []string, sep string)` | Join string slice with separator | `{{ slice_Join (slice_Str "a" "b" "c") "-" }}``a-b-c` |
| `slice_Int(values ...int)` | Create int slice | `{{ slice_Int 1 2 3 }}` | | `slice_Int(values ...int)` | Create int slice | `{{ slice_Int 1 2 3 }}` |
| `slice_Uint(values ...uint)` | Create uint slice | `{{ slice_Uint 1 2 3 }}` | | `slice_Uint(values ...uint)` | Create uint slice | `{{ slice_Uint 1 2 3 }}` |

View File

@@ -71,7 +71,6 @@ func (arg *stringSliceArg) Set(value string) error {
// Parse parses command-line arguments into a Config object. // Parse parses command-line arguments into a Config object.
// It can return the following errors: // It can return the following errors:
// - types.ErrCLINoArgs
// - types.CLIUnexpectedArgsError // - types.CLIUnexpectedArgsError
// - types.FieldParseErrors // - types.FieldParseErrors
func (parser ConfigCLIParser) Parse() (*Config, error) { func (parser ConfigCLIParser) Parse() (*Config, error) {
@@ -178,12 +177,6 @@ func (parser ConfigCLIParser) Parse() (*Config, error) {
panic(err) panic(err)
} }
// Check if no flags were set and no non-flag arguments were provided.
// This covers cases where `sarin` is run without any meaningful arguments.
if flagSet.NFlag() == 0 && len(flagSet.Args()) == 0 {
return nil, types.ErrCLINoArgs
}
// Check for any unexpected non-flag arguments remaining after parsing. // Check for any unexpected non-flag arguments remaining after parsing.
if args := flagSet.Args(); len(args) > 0 { if args := flagSet.Args(); len(args) > 0 {
return nil, types.NewCLIUnexpectedArgsError(args) return nil, types.NewCLIUnexpectedArgsError(args)

View File

@@ -536,12 +536,6 @@ func ReadAllConfigs() *Config {
cliParser := NewConfigCLIParser(os.Args) cliParser := NewConfigCLIParser(os.Args)
cliConf, err := cliParser.Parse() cliConf, err := cliParser.Parse()
_ = utilsErr.MustHandle(err, _ = utilsErr.MustHandle(err,
utilsErr.OnSentinel(types.ErrCLINoArgs, func(err error) error {
cliParser.PrintHelp()
fmt.Fprintln(os.Stderr, StyleYellow.Render("\nNo arguments provided."))
os.Exit(1)
return nil
}),
utilsErr.OnType(func(err types.CLIUnexpectedArgsError) error { utilsErr.OnType(func(err types.CLIUnexpectedArgsError) error {
cliParser.PrintHelp() cliParser.PrintHelp()
fmt.Fprintln(os.Stderr, fmt.Fprintln(os.Stderr,

View File

@@ -3,6 +3,7 @@ package sarin
import ( import (
"context" "context"
"net/url" "net/url"
"os"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@@ -13,6 +14,7 @@ import (
"github.com/charmbracelet/bubbles/spinner" "github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/x/term"
"github.com/valyala/fasthttp" "github.com/valyala/fasthttp"
"go.aykhans.me/sarin/internal/script" "go.aykhans.me/sarin/internal/script"
"go.aykhans.me/sarin/internal/types" "go.aykhans.me/sarin/internal/types"
@@ -155,6 +157,10 @@ func (q sarin) Start(ctx context.Context) {
var messageChannel chan runtimeMessage var messageChannel chan runtimeMessage
var sendMessage messageSender var sendMessage messageSender
if !q.quiet && !term.IsTerminal(os.Stdout.Fd()) {
q.quiet = true
}
if q.quiet { if q.quiet {
sendMessage = func(level runtimeMessageLevel, text string) {} sendMessage = func(level runtimeMessageLevel, text string) {}
} else { } else {

View File

@@ -62,10 +62,6 @@ func NewDefaultTemplateFuncMap(randSource rand.Source, fileCache *FileCache) tem
}, },
"strings_TrimPrefix": strings.TrimPrefix, "strings_TrimPrefix": strings.TrimPrefix,
"strings_TrimSuffix": strings.TrimSuffix, "strings_TrimSuffix": strings.TrimSuffix,
"strings_Join": func(sep string, values ...string) string {
return strings.Join(values, sep)
},
// Dict // Dict
"dict_Str": func(values ...string) map[string]string { "dict_Str": func(values ...string) map[string]string {
dict := make(map[string]string) dict := make(map[string]string)
@@ -83,6 +79,7 @@ func NewDefaultTemplateFuncMap(randSource rand.Source, fileCache *FileCache) tem
"slice_Str": func(values ...string) []string { return values }, "slice_Str": func(values ...string) []string { return values },
"slice_Int": func(values ...int) []int { return values }, "slice_Int": func(values ...int) []int { return values },
"slice_Uint": func(values ...uint) []uint { return values }, "slice_Uint": func(values ...uint) []uint { return values },
"slice_Join": strings.Join,
// File // File
// file_Base64 reads a file (local or remote URL) and returns its Base64 encoded content. // file_Base64 reads a file (local or remote URL) and returns its Base64 encoded content.

View File

@@ -15,7 +15,7 @@ func (cookies Cookies) GetValue(key string) *[]string {
return nil return nil
} }
func (cookies *Cookies) Append(cookie ...Cookie) { func (cookies *Cookies) Merge(cookie ...Cookie) {
for _, c := range cookie { for _, c := range cookie {
if item := cookies.GetValue(c.Key); item != nil { if item := cookies.GetValue(c.Key); item != nil {
*item = append(*item, c.Value...) *item = append(*item, c.Value...)
@@ -27,7 +27,7 @@ func (cookies *Cookies) Append(cookie ...Cookie) {
func (cookies *Cookies) Parse(rawValues ...string) { func (cookies *Cookies) Parse(rawValues ...string) {
for _, rawValue := range rawValues { for _, rawValue := range rawValues {
cookies.Append(*ParseCookie(rawValue)) *cookies = append(*cookies, *ParseCookie(rawValue))
} }
} }

View File

@@ -250,10 +250,6 @@ func (e TemplateRenderError) Unwrap() error {
// ======================================== CLI ======================================== // ======================================== CLI ========================================
var (
ErrCLINoArgs = errors.New("CLI expects arguments but received none")
)
type CLIUnexpectedArgsError struct { type CLIUnexpectedArgsError struct {
Args []string Args []string
} }

View File

@@ -24,7 +24,7 @@ func (headers Headers) GetValue(key string) *[]string {
return nil return nil
} }
func (headers *Headers) Append(header ...Header) { func (headers *Headers) Merge(header ...Header) {
for _, h := range header { for _, h := range header {
if item := headers.GetValue(h.Key); item != nil { if item := headers.GetValue(h.Key); item != nil {
*item = append(*item, h.Value...) *item = append(*item, h.Value...)
@@ -36,7 +36,7 @@ func (headers *Headers) Append(header ...Header) {
func (headers *Headers) Parse(rawValues ...string) { func (headers *Headers) Parse(rawValues ...string) {
for _, rawValue := range rawValues { for _, rawValue := range rawValues {
headers.Append(*ParseHeader(rawValue)) *headers = append(*headers, *ParseHeader(rawValue))
} }
} }

View File

@@ -15,7 +15,7 @@ func (params Params) GetValue(key string) *[]string {
return nil return nil
} }
func (params *Params) Append(param ...Param) { func (params *Params) Merge(param ...Param) {
for _, p := range param { for _, p := range param {
if item := params.GetValue(p.Key); item != nil { if item := params.GetValue(p.Key); item != nil {
*item = append(*item, p.Value...) *item = append(*item, p.Value...)
@@ -27,7 +27,7 @@ func (params *Params) Append(param ...Param) {
func (params *Params) Parse(rawValues ...string) { func (params *Params) Parse(rawValues ...string) {
for _, rawValue := range rawValues { for _, rawValue := range rawValues {
params.Append(*ParseParam(rawValue)) *params = append(*params, *ParseParam(rawValue))
} }
} }