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