sh

package module
v0.0.0-...-1ecc285 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2023 License: MIT Imports: 12 Imported by: 0

README

sh is a library to let you coding go like a shell script.

Examples

package main

import (
	"os"
	"strings"

	"github.com/jopbrown/sh"
)

func init() {
	// set -e
	sh.SetExitOnError(true)
	// set -x
	sh.SetXtrace(true)
}

func main() {
	// echo "$#"
	sh.Echof("%d", sh.ArgN()).Print()
	// echo "$0"
	sh.Echo(sh.Arg(0)).Print()
	// echo "$1"
	sh.Echo(sh.Arg(1)).Print()

	// for arg in $@; do
	// 	echo $arg
	// done
	sh.FromSlice(sh.Args()).Print()

	// pwd
	sh.Echo(sh.Pwd()).Print()
	// echo "$PATH"
	sh.Echo(os.Getenv("PATH")).Print()

	// rm -rf tmp*
	sh.Rm("tmp*")

	// ls
	sh.Ls().Print()
	// ls *.go
	sh.Ls("*.go").Print()

	// mkdir -p tmp tmp2
	sh.Mkdir("tmp", "tmp2")
	// cp *.go tmp/.
	sh.Cp("*.go", "tmp/.")

	// cp -r tmp tmp3
	sh.Cp("tmp", "tmp3")
	// mv tmp/*.go tmp2/.
	sh.Mv("tmp/*.go", "tmp2/.")

	// pushd tmp2
	sh.Pushd("tmp2")
	// touch 1.txt 2.txt
	sh.Touch("1.txt", "2.txt")

	// if [[ -e 1.txt ]]; then
	//     echo "exist"
	// fi
	if sh.Exists("1.txt") {
		sh.Echo("exist").Print()
	}

	// if [[ -f 1.txt ]]; then
	//     echo "file exist"
	// fi
	if sh.ExistsFile("1.txt") {
		sh.Echo("file exist").Print()
	}

	// if [[ -d 1.txt ]]; then
	//     echo "dir exist"
	// fi
	if sh.ExistsDir("1.txt") {
		sh.Echo("dir exist").Print()
	}

	sh.Ls("*.txt").Print()
	// ls *.txt
	sh.Rm("*.txt", "*.go")
	// rm *.txt *.go

	sh.Popd()
	// popd

	// pushd tmp3
	sh.Pushd("tmp3")

	// grep 'func' main.go
	sh.Grep("func", "main.go").Print()
	// cat *.go | grep 'func'
	sh.Cat("*.go").Grep("func").Print()

	// sed -e 's/func/xxxx/g' main.go > main2.txt
	sh.Sed(func(s string) string {
		return strings.ReplaceAll(s, "func", "xxxx")
	}, "main.go").PrintToFile("main2.txt", false)

	// cat main.go | sed -e 's/func/ssss/g' >> main2.txt
	sh.Cat("main.go").Sed(func(s string) string {
		return strings.ReplaceAll(s, "func", "ssss")
	}).PrintToFile("main2.txt", true)

	// tar zcvf xxx.tar.gz *
	sh.Exec("tar", "zcvf", "xxx.tar.gz", "*").Print()
	if sh.Err() != nil {
		sh.Exit(-1)
	}

	// echo "xxxxx" | base64
	sh.Echo("xxxxx").Exec("base64").Print()

	// popd
	sh.Popd()
}

sgo

sgo is a interpreter to run go without go installed.

Embed sh and subset of stdlib.

Install

go install github.com/jopbrown/sh/cmd/sgo

Usage

# sgo [-vendor <VENDOR_DIR>] <ENTRY_GO_FILE> [args...]
sgo main.go arg1 arg2

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Arg

func Arg(n int) string

func ArgN

func ArgN() int

func Args

func Args() []string

func Cd

func Cd(dir string)

func CheckErr

func CheckErr(err error) bool

func Chmod

func Chmod(perm int, fileList ...string)

func Cp

func Cp(args ...string)

func Err

func Err() error

func Exists

func Exists(name string) bool

func ExistsDir

func ExistsDir(name string) bool

func ExistsFile

func ExistsFile(name string) bool

func Exit

func Exit(code int)

func ExitIfErr

func ExitIfErr()

func Mkdir

func Mkdir(args ...string)

func Mv

func Mv(args ...string)

func Popd

func Popd()

func Pushd

func Pushd(dir string)

func Pwd

func Pwd() string

func Rm

func Rm(args ...string)

func SetExitOnError

func SetExitOnError(enable bool) bool

func SetXtrace

func SetXtrace(enable bool) bool

func Touch

func Touch(args ...string)

Types

type MakeAuto

type MakeAuto struct {
	Target            string
	FirstPrerequisite string
	Prerequisites     []string
}

type MakeFile

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

func NewMakeFile

func NewMakeFile() *MakeFile

func (*MakeFile) Make

func (mk *MakeFile) Make(targets ...string)

func (*MakeFile) QueryTask

func (mk *MakeFile) QueryTask(target string) *MakeTask

func (*MakeFile) Task

func (mk *MakeFile) Task(target string) *MakeTask

func (*MakeFile) Tasks

func (mk *MakeFile) Tasks(targets ...string) MakeTasks

type MakeFunc

type MakeFunc func(auto *MakeAuto) bool

type MakeTask

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

func (*MakeTask) Command

func (task *MakeTask) Command(fn MakeFunc) *MakeTask

func (*MakeTask) Depend

func (task *MakeTask) Depend(prerequisites ...string) *MakeTask

func (*MakeTask) Phony

func (task *MakeTask) Phony() *MakeTask

func (*MakeTask) Target

func (task *MakeTask) Target() string

type MakeTasks

type MakeTasks []*MakeTask

func (MakeTasks) ForEach

func (tasks MakeTasks) ForEach(fn func(task *MakeTask))

type Stream

type Stream chan string

func Cat

func Cat(args ...string) Stream

func Echo

func Echo(msg string) Stream

func Echof

func Echof(format string, v ...interface{}) Stream

func Exec

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

func Exec2

func Exec2(name string, args ...string) Stream

func From

func From(ss ...string) Stream

func FromFields

func FromFields(fields string) Stream

func FromReader

func FromReader(r io.Reader) Stream

func FromSlice

func FromSlice(ss []string) Stream

func Grep

func Grep(pattern string, fileList ...string) Stream

func GrepV

func GrepV(pattern string, fileList ...string) Stream

func Ls

func Ls(args ...string) Stream

func Sed

func Sed(fn func(string) string, fileList ...string) Stream

func (Stream) Exec

func (sin Stream) Exec(name string, args ...string) Stream

func (Stream) Exec2

func (sin Stream) Exec2(name string, args ...string) Stream

func (Stream) Grep

func (sin Stream) Grep(pattern string) Stream

func (Stream) GrepV

func (sin Stream) GrepV(pattern string) Stream

func (Stream) Print

func (s Stream) Print()

func (Stream) PrintTo

func (s Stream) PrintTo(w io.Writer)

func (Stream) PrintToDevNull

func (s Stream) PrintToDevNull()

func (Stream) PrintToErr

func (s Stream) PrintToErr()

func (Stream) PrintToFile

func (s Stream) PrintToFile(fname string, append bool)

func (Stream) Reader

func (s Stream) Reader() io.Reader

func (Stream) Sed

func (sin Stream) Sed(fn func(string) string) Stream

func (Stream) Slice

func (s Stream) Slice() []string

func (Stream) String

func (s Stream) String() string

Directories

Path Synopsis
cmd
sgo
samples

Jump to

Keyboard shortcuts

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