linep

package module
v0.6.2 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2025 License: MIT Imports: 21 Imported by: 0

README

linep

❯ linep
linep -- process lines by one liner

Usage:
linep TEMPLATE MAP [FLAGS]
linep TEMPLATE INIT MAP [FLAGS]
linep TEMPLATE INIT MAP REDUCE [FLAGS]

TEMPLATE: go, py, python, pipenv, rs, rust, nil, null, empty

Requirements of templates:
go: go
python, py: python
pipenv: pipenv, pyenv
rust, rs: cargo

Examples:
> seq 3 | linep go 'fmt.Println(x+"0")' -q
10
20
30

> seq 10 | linep rust 'let n:i32=x.parse().unwrap();if n%2==0{println!("{}",n)}' -q
2
4
6
8
10

> seq 4 | linep pipenv 'acc=[]' 'acc.append(int(x));print(math.prod(acc))' 'print(sum(acc))' --import 'math' -q
1
2
6
24
10

# without pipenv
> seq 3 | linep pipenv 'print(x+"0")' --exec 'python @MAIN' --init 'sleep 0'
10
20
30
# is almost equivalent to
> seq 3 | linep py 'print(x+"0")'

# indent MAP (python)
> linep py 'r={}' 'x=x.split(".")[-1]
if x in r:
  r[x]+=1
else:
  r[x]=1' 'for k, v in r.items():
  print(f"{k}\t{v}")' --dry
import sys
import signal
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
r={}
try:
  for x in sys.stdin:
    x = x.rstrip()
    x=x.split(".")[-1]
    if x in r:
      r[x]+=1
    else:
      r[x]=1
except BrokenPipeError:
  pass
for k, v in r.items():
  print(f"{k}\t{v}")

Templates:
TEMPLATE argument can be a template filename.
empty (nil, null) template is for overriding.
A template file format is:

# template name, required.
name: sample
# generated script name, required.
main: main.go
# aliases of name.
# also used by TEMPLATE argument.
alias:
  - smpl
# template of script body (main.go, main.py, ...).
# executed by https://pkg.go.dev/text/template with https://masterminds.github.io/sprig/
# available fields:
#   Init   : INIT argument (string)
#   Map    : MAP argument (string)
#   Reduce : REDUCE argument (string)
#   Import : --import argument (slice of string)
script: |
  ...
# init script command.
# initialize a directory of generated script like 'go mod init'.
# macros are replaced with a reference of an environment variable.
# available macros:
#   @MAIN     : main of this template
#   @WORK_DIR : --workDir argument
#   @EXEC_PWD : current directory of linep execution
#   @SRC_DIR  : directory of the generated script
init: |
  ...
# execute script command.
# execute generated script like 'go run @MAIN'.
# macros are available.
exec: |
  ...

# show template
> linep go --displayTemplate

Environment variables:
You can use the flag name with the hyphen removed and converted to uppercase as an environment variable.
If both the corresponding flag and the environment variable are specified at the same time, the flag takes precedence.

Flags:
      --debug             enable debug logs
      --displayTemplate   do not run; display template
      --dry               do not run; display generated script
      --exec string       override exec script
  -i, --import string     additional libraries; separated by '|'
      --init string       override init script
      --keep              keep generated script directory
      --main string       override main script name
  -q, --quiet             quiet stderr logs
      --script string     override script
      --sh string         execute shell command; separated by ';' (default "sh")
  -w, --workDir string    working directory; default: $HOME/.linep

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidTemplate = errors.New("InvalidTemplate")
)

Functions

func MkdirTemp added in v0.4.0

func MkdirTemp(dir, pattern string) (string, error)

func SetupLogger added in v0.4.0

func SetupLogger(debug, quiet bool)

func Stderr added in v0.4.0

func Stderr(quiet bool) io.Writer

func WithErr added in v0.4.0

func WithErr(err error) any

Types

type Config

type Config struct {
	Dry             bool     `json:"dry" yaml:"dry" name:"dry" usage:"do not run; display generated script"`
	Debug           bool     `json:"debug" yaml:"debug" name:"debug" usage:"enable debug logs"`
	Quiet           bool     `json:"quiet" yaml:"quiet" name:"quiet" short:"q" usage:"quiet stderr logs"`
	Keep            bool     `json:"keep" yaml:"keep" name:"keep" usage:"keep generated script directory"`
	WorkDir         string   `json:"workDir" yaml:"workDir" name:"workDir" short:"w" usage:"working directory; default: $HOME/.linep"`
	Shell           []string `json:"sh" yaml:"sh" name:"sh" default:"sh" usage:"execute shell command; separated by ';'"`
	TemplateName    string   `json:"name" yaml:"name"`
	TemplateScript  string   `json:"tscript" yaml:"tscript" name:"script" usage:"override script"`
	TemplateInit    string   `json:"tinit" yaml:"tinit" name:"init" usage:"override init script"`
	TemplateExec    string   `json:"texec" yaml:"texec" name:"exec" usage:"override exec script"`
	TemplateMain    string   `json:"tmain" yaml:"tmain" name:"main" usage:"override main script name"`
	Init            string   `json:"init" yaml:"init"`
	Map             string   `json:"map" yaml:"map"`
	Reduce          string   `json:"reduce" yaml:"reduce"`
	Import          []string `json:"import" yaml:"import" name:"import" short:"i" usage:"additional libraries; separated by '|'"`
	PWD             string   `json:"pwd" yaml:"pwd"`
	DisplayTemplate bool     `json:"displayTemplate" yaml:"displayTemplate" name:"displayTemplate" usage:"do not run; display template"`
}

func NewConfig

func NewConfig(fs *pflag.FlagSet) (*Config, error)

func (Config) Executor added in v0.4.0

func (c Config) Executor(stdin io.Reader, stdout io.Writer) (*Executor, error)

func (*Config) Initialize added in v0.5.3

func (c *Config) Initialize() error

func (Config) Merger added in v0.4.0

func (c Config) Merger() *structconfig.Merger[Config]

func (Config) SciprtArgs added in v0.4.0

func (c Config) SciprtArgs() *ScriptArgs

func (Config) SetupLogger

func (c Config) SetupLogger()

func (Config) StructConfig added in v0.4.0

func (c Config) StructConfig() *structconfig.StructConfig[Config]

func (Config) Template

func (c Config) Template() (*Template, error)

type Executor added in v0.4.0

type Executor struct {
	Shell           []string
	Template        *Template
	Args            *ScriptArgs
	ExecPWD         string
	WorkDir         string
	KeepScript      bool
	Dry             bool
	DisplayTemplate bool

	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
	// contains filtered or unexported fields
}

func (Executor) Close added in v0.4.0

func (e Executor) Close() error

func (*Executor) Execute added in v0.4.0

func (e *Executor) Execute(ctx context.Context) error

type ScriptArgs added in v0.4.0

type ScriptArgs struct {
	Init   string   `json:"init" yaml:"init"`
	Map    string   `json:"map" yaml:"map"`
	Reduce string   `json:"reduce" yaml:"reduce"`
	Import []string `json:"import" yaml:"import"`
}

type Template added in v0.4.0

type Template struct {
	Name   string   `json:"name" yaml:"name"`
	Alias  []string `json:"alias" yaml:"alias"`
	Script string   `json:"script" yaml:"script"`
	Init   string   `json:"init" yaml:"init"`
	Exec   string   `json:"exec" yaml:"exec"`
	Main   string   `json:"main" yaml:"main"`
}

func (Template) Execute added in v0.4.0

func (t Template) Execute(w io.Writer, args *ScriptArgs) error

func (*Template) Override added in v0.4.0

func (t *Template) Override(
	script, init, exec, main string,
)

func (Template) Validate added in v0.4.0

func (t Template) Validate() error

Directories

Path Synopsis
cmd
linep command

Jump to

Keyboard shortcuts

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