Files
genqlient/generate/main.go
T
Ben Kraft d41cf636af Clean up godoc (#82)
## Summary:
Before open-sourcing, we want to make sure that (a) GoDoc looks
reasonable, and (b) everything in the API is something we want to commit
to.  In this commit, I do some miscellaneous cleanup on both fronts;
this does involve a few breaking changes to the programmatic API (better
now than once it has users).  In future commits, I'll likely move the
documentation for `genqlient.yaml` and `@genqlient` to clearer places,
and make `GenqlientDirective` private, such that GoDoc is really only
for programmatic users.

Fixes #25.

Issue: https://github.com/Khan/genqlient/issues/25

## Test plan:
make check


Author: benjaminjkraft

Reviewers: dnerdy, benjaminjkraft, jvoll, aberkan, MiguelCastillo, mahtabsabet

Required Reviewers: 

Approved By: dnerdy, jvoll

Checks:  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint,  Test (1.17),  Test (1.16),  Test (1.15),  Test (1.14),  Lint

Pull Request URL: https://github.com/Khan/genqlient/pull/82
2021-09-10 15:54:34 -07:00

76 lines
1.8 KiB
Go

// Package generate provides programmatic access to genqlient's functionality,
// and documentation of its configuration options. For general usage
// documentation, see github.com/Khan/genqlient.
package generate
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/alexflint/go-arg"
)
func readConfigGenerateAndWrite(configFilename string) error {
config, err := ReadAndValidateConfig(configFilename)
if err != nil {
return err
}
generated, err := Generate(config)
if err != nil {
return err
}
for filename, content := range generated {
err = os.MkdirAll(filepath.Dir(filename), 0o755)
if err != nil {
return errorf(nil,
"could not create parent directory for generated file %v: %v",
filename, err)
}
err = ioutil.WriteFile(filename, content, 0o644)
if err != nil {
return errorf(nil, "could not write generated file %v: %v",
filename, err)
}
}
return nil
}
type cliArgs struct {
ConfigFilename string `arg:"positional" placeholder:"CONFIG" default:"genqlient.yaml" help:"path to genqlient configuration (default genqlient.yaml)"`
Init bool `arg:"--init" help:"write out and use a default config file"`
}
func (cliArgs) Description() string {
return strings.TrimSpace(`
Generates GraphQL client code for a given schema and queries.
See https://github.com/Khan/genqlient for full documentation.
`)
}
// Main is the command-line entrypoint to genqlient; it's equivalent to calling
// `go run github.com/Khan/genqlient`. For lower-level control over
// genqlient's operation, see Generate.
func Main() {
exitIfError := func(err error) {
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
var args cliArgs
arg.MustParse(&args)
if args.Init {
err := initConfig(args.ConfigFilename)
exitIfError(err)
}
err := readConfigGenerateAndWrite(args.ConfigFilename)
exitIfError(err)
}