make config file optional
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
example:
|
||||
go run . example/genql.yaml
|
||||
go generate ./...
|
||||
go run ./example/cmd/example/main.go csilvers
|
||||
|
||||
.PHONY: example
|
||||
|
||||
@@ -43,6 +43,8 @@ func getViewer(ctx context.Context, client *graphql.Client) (*getViewerResponse,
|
||||
graphqlClient := graphql.NewClient("https://example.com/graphql", http.DefaultClient)
|
||||
viewerResp, _ := getViewer(context.Background(), graphqlClient)
|
||||
fmt.Println("you are", *viewerResp.Viewer.MyName)
|
||||
|
||||
//go:generate go run github.com/Khan/genql
|
||||
```
|
||||
|
||||
For a complete working example, see `example/`.
|
||||
@@ -64,6 +66,7 @@ Config options:
|
||||
- file locations (queries, generated, schema (or get via HTTP))
|
||||
- use ctx or not, including complexities of how Khan uses context
|
||||
- HTTP calling convention (is there enough variation to matter?)
|
||||
- proper config/arguments setup (e.g. with [viper](https://github.com/spf13/viper)
|
||||
|
||||
Other:
|
||||
- error-checking/validation/etc. everywhere
|
||||
|
||||
+13
-9
@@ -56,18 +56,22 @@ func (c *Config) ValidateAndFillDefaults() error {
|
||||
}
|
||||
|
||||
func ReadAndValidateConfig(filename string) (*Config, error) {
|
||||
text, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unreadable config file %v: %v", filename, err)
|
||||
}
|
||||
|
||||
var config Config
|
||||
err = yaml.Unmarshal(text, &config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid config file %v: %v", filename, err)
|
||||
if filename == "" {
|
||||
config = *defaultConfig
|
||||
} else {
|
||||
text, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unreadable config file %v: %v", filename, err)
|
||||
}
|
||||
|
||||
err = yaml.Unmarshal(text, &config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid config file %v: %v", filename, err)
|
||||
}
|
||||
}
|
||||
|
||||
err = config.ValidateAndFillDefaults()
|
||||
err := config.ValidateAndFillDefaults()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid config file %v: %v", filename, err)
|
||||
}
|
||||
|
||||
+7
-6
@@ -35,11 +35,12 @@ func Main() {
|
||||
}
|
||||
}()
|
||||
|
||||
if len(os.Args) != 2 {
|
||||
// TODO: omit config to get it from genql.yaml, or to use the defaults.
|
||||
err = fmt.Errorf("usage: %s genql.yaml", os.Args[0])
|
||||
return
|
||||
switch len(os.Args) {
|
||||
case 2:
|
||||
err = readConfigGenerateAndWrite(os.Args[1])
|
||||
case 1:
|
||||
err = readConfigGenerateAndWrite("")
|
||||
default:
|
||||
err = fmt.Errorf("usage: %s [config]", os.Args[0])
|
||||
}
|
||||
|
||||
err = readConfigGenerateAndWrite(os.Args[1])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user