Embed data files in the binary (#180)

Now that we're on Go 1.16+ we can do this easily! The main advantage is
it means users can build a genqlient binary and use that portably (or we
could distribute one, or whatever). Plus the code is marginally simpler;
the `embed` API is really quite nice.

Fixes #9.

Test plan:
```
make check
go build .
rm -rf generate               # pretend we have no checkout
./genqlient ./internal/integration/genqlient.yaml
./genqlient --init            # fails after generating a default config
```
This commit is contained in:
Ben Kraft
2022-03-22 12:00:56 -07:00
committed by GitHub
parent 13094c3e58
commit 36e86cf97f
4 changed files with 14 additions and 29 deletions
+5 -16
View File
@@ -1,8 +1,8 @@
package generate
import (
_ "embed"
"go/token"
"io"
"io/ioutil"
"os"
"path/filepath"
@@ -123,22 +123,11 @@ func ReadAndValidateConfigFromDefaultLocations() (*Config, error) {
return ReadAndValidateConfig(cfgFile)
}
//go:embed default_genqlient.yaml
var defaultConfig []byte
func initConfig(filename string) error {
// TODO(benkraft): Embed this config file into the binary, see
// https://github.com/Khan/genqlient/issues/9.
r, err := os.Open(filepath.Join(thisDir, "default_genqlient.yaml"))
if err != nil {
return err
}
w, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
if err != nil {
return errorf(nil, "unable to write default genqlient.yaml: %v", err)
}
_, err = io.Copy(w, r)
if err != nil {
return errorf(nil, "unable to write default genqlient.yaml: %v", err)
}
return nil
return os.WriteFile(filename, defaultConfig, 0o644)
}
// findCfg searches for the config file in this directory and all parents up the tree