Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aybabtme/embed
Rewrites string or []byte variables with the content of files.
https://github.com/aybabtme/embed
Last synced: about 1 month ago
JSON representation
Rewrites string or []byte variables with the content of files.
- Host: GitHub
- URL: https://github.com/aybabtme/embed
- Owner: aybabtme
- License: mit
- Created: 2014-09-18T02:23:52.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-11-29T01:20:45.000Z (almost 8 years ago)
- Last Synced: 2024-06-19T11:37:35.763Z (5 months ago)
- Language: Go
- Homepage:
- Size: 62.5 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# embed
Hacked together prototype to rewrite `string` or `[]byte` variables and
constants in a package, using the content of a file.Meant to be used with `go generate`, comming in go 1.4.
## Usage
```bash
$ cd testdata/
$ embed file -var createDbSQL --source create_query.sql
```or
```go
package testdata//go:generate embed file -var createDbSQL --source create_query.sql
var createDbSQL string
```Flags:
* `-dir`: look for Go files in `testdata`, defaults to current directory
* `-var`: set the variable `createDbSQL`
* `--source` or `stdin`: source of content for `createDbSQL`
* `--keep`: creates a new file instead of setting the variable directly in the file## Installation
### linux
```bash
wget -qO- https://github.com/aybabtme/embed/releases/download/0.1/embed_linux.tar.gz | tar xvz
```### darwin
```bash
wget -qO- https://github.com/aybabtme/embed/releases/download/0.1/embed_darwin.tar.gz | tar xvz
```## Example
### With `go generate`
If you have a Go file:
```go
package testdata//go:generate embed file -var createDbSQL --source create_query.sql
var createDbSQL string
```And invoke:
```
$ cd testdata/
$ go generate
embed: in file "example.go"; value of "createDbSQL" set
```Now the file contains:
```go
package testdata//go:generate embed file -var createDbSQL --source create_query.sql
var createDbSQL = "CREATE DATABASE IF NOT EXISTS hello_world;\n"
```### Manually
With the following files:
* `testdata/create_query.sql`
```SQL
CREATE DATABASE IF NOT EXISTS hello_world;
```* `testdata/example.go`
```go
package testdataimport (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)var createDbSQL string
func CreateDB(dsn string) error {
db, err := sql.Open("mysql", dsn)
if err != nil {
return err
}
_, err := db.Exec(createDbSQL)
return err
}```
Running this command:
```bash
$ embed file --keep -dir testdata -var createDbSQL < testdata/create_query.sql
```Gives you:
* `testdata/generated_example.go`
```go
package testdataimport (
"database/sql"
_ "github.com/go-sql-driver/mysql"
)var createDbSQL = "CREATE DATABASE IF NOT EXISTS hello_world;\n"
func CreateDB(dsn string) error {
db, err := sql.Open("mysql", dsn)
if err != nil {
return err
}
_, err := db.Exec(createDbSQL)
return err
}
```