progrium/go-basher là dự án mã nguồn mở trên GitHub với 390 sao, viết chủ yếu bằng Go. Library for writing hybrid Go and Bash programs
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
A Go library for creating Bash environments, exporting Go functions in them as Bash functions, and running commands in that Bash environment. Combined with a tool like go-bindata, you can write programs that are part written in Go and part written in Bash that can be distributed as standalone binaries.
Using go-basher
Here we have a simple Go program that defines a reverse function, creates a Bash environment sourcing main.bash and then runs main in that environment.
Here is our main.bash file, the actual heart of the program:
main() {
echo "Hello world" | reverse
}
Cancellation with context
Context.RunContext accepts a context.Context and is otherwise identical to Run. Cancelling the context terminates the underlying Bash process via exec.CommandContext, which sends SIGKILL.
ApplicationContext and ApplicationWithPathContext are context-aware variants of the Application* helpers that forward ctx into the Bash invocation.
Using go-basher with go-bindata
You can bundle your Bash scripts into your Go binary using go-bindata. First install go-bindata:
go get github.com/jteeuwen/go-bindata/...
Now put all your Bash scripts in a directory called bash. The above example program would mean you'd have a bash/main.bash file. Run go-bindata on the directory:
go-bindata bash
This will produce a bindata.go file that includes all of your Bash scripts.
bindata.go includes a function called Asset that behaves like ioutil.ReadFile for files in your bindata.go.
Here's how you embed it into the above example program:
copy/paste it's import-statements and functions to your application code
method A: change bash.Source("bash/main.bash", nil) into bash.Source("bash/main.bash, Asset)
method B: replace all code in the main()-function with the Application()-helper function (see below)
Did you already hear that term? Sometimes Bash binary is missing, for example when using alpine linux or busybox. Or sometimes its not the correct version. Like OSX ships with Bash 3.x which misses a lot of usefull features. Or you want to make sure to avoid shellshock attack.
For those reasons static versions of Bash binaries are included for linux and darwin. Statically linked bash binaries are released at: https://github.com/robxu9/bash-static. These are then turned into go code, with go-bindata: bindata_linux.go and bindata_darwin.go.
When you use the basher.Application() function, the built in Bash binary will be extracted into the ~/.basher/ dir.
When you use the basher.ApplicationWithPath() function, you will need to specify a bash path with the same setup guarantees as basher.Application().
When you use the basher.NewContext() function, you have to specify the path to Bash and will have complete freedom to modify the context at will.
Motivation
Go is a great compiled systems language, but it can still be faster to write and glue existing commands together in Bash. However, there are operations you wouldn't want to do in Bash that are straightforward in Go, for example, writing and reading structured data formats. By allowing them to work together, you can use each where they are strongest.
Take a common task like making an HTTP request for JSON data. Parsing JSON is easy in Go, but without depending on a tool like jq it is not even worth trying in Bash. And some formats like YAML don't even have a good jq equivalent. Whereas making an HTTP request in Go in the simplest case is going to be 6+ lines, as opposed to Bash where you can use curl in one line. If we write our JSON parser in Go and fetch the HTTP doc with curl, we can express printing a field from a remote JSON object in one line:
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/progrium/go-basher là nguồn chính thức.
progrium/go-basher có phải mã nguồn mở không?
Có — progrium/go-basher phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/progrium/go-basher.
progrium/go-basher là gì?
progrium/go-basher (progrium/go-basher) là dự án Go trên GitHub. Theo mô tả gốc: Library for writing hybrid Go and Bash programs
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về go-basher?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về go-basher.