Skip to content

Commit a069ab0

Browse files
infer,translate: implement file destinations
1 parent f1776bf commit a069ab0

File tree

3 files changed

+50
-10
lines changed

3 files changed

+50
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Common uses cases:
2222
GraphQL schemas, protobuf schemas, YAML, TOML, and XML:
2323

2424
```
25-
$ curl https://example.com/some_endpoint | schema infer --graphql
25+
$ curl http://piedpiper.tld/some_endpoint | schema infer --graphql
2626
type People {
2727
age: Float!
2828
name: String!
@@ -36,7 +36,7 @@ type Object {
3636
* Omit `--graphql` to get [JSON Schema](https://json-schema.org):
3737

3838
```
39-
$ curl https://example.com/some_endpoint | schema infer
39+
$ curl http://piedpiper.tld/some_endpoint | schema infer
4040
{
4141
"title": "",
4242
"type": "object",

infer/infer.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,45 @@
11
package infer
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
7+
"io/ioutil"
68
"os"
79

810
"github.com/Confbase/schema/example"
911
"github.com/Confbase/schema/jsonsch"
1012
"github.com/Confbase/schema/util"
1113
)
1214

13-
func InferEntry(cfg Config, args []string) {
14-
if len(args) == 0 {
15+
func InferEntry(cfg Config, targets []string) {
16+
if len(targets) == 0 {
1517
if err := Infer(os.Stdin, os.Stdout, cfg); err != nil {
1618
fmt.Fprintf(os.Stderr, "error: %v\n", err)
1719
os.Exit(1)
1820
}
19-
} else {
20-
fmt.Fprintf(os.Stderr, "error: not implemented yet\n")
21+
return
22+
}
23+
24+
buf, err := ioutil.ReadAll(os.Stdin)
25+
if err != nil {
26+
fmt.Fprintf(os.Stderr, "error: failed to read from stdin\n%v", err)
2127
os.Exit(1)
2228
}
29+
30+
for _, t := range targets {
31+
f, err := os.OpenFile(t, os.O_RDWR|os.O_CREATE, 0666)
32+
if err != nil {
33+
fmt.Fprintf(os.Stderr, "error: failed to open '%v'\n%v\n", t, err)
34+
os.Exit(1)
35+
}
36+
defer f.Close()
37+
38+
if err := Infer(bytes.NewReader(buf), f, cfg); err != nil {
39+
fmt.Fprintf(os.Stderr, "error: %v\n", err)
40+
os.Exit(1)
41+
}
42+
}
2343
}
2444

2545
func Infer(r io.Reader, w io.Writer, cfg Config) error {

translate/translate.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,43 @@
11
package translate
22

33
import (
4+
"bytes"
45
"fmt"
56
"io"
7+
"io/ioutil"
68
"os"
79

810
"github.com/Confbase/schema/util"
911
)
1012

11-
func TranslateEntry(cfg Config, args []string) {
12-
if len(args) == 0 {
13+
func TranslateEntry(cfg Config, targets []string) {
14+
if len(targets) == 0 {
1315
if err := Translate(os.Stdin, os.Stdout, cfg); err != nil {
1416
fmt.Fprintf(os.Stderr, "error: %v\n", err)
1517
os.Exit(1)
1618
}
17-
} else {
18-
fmt.Fprintf(os.Stderr, "error: not implemented yet\n")
19+
return
20+
}
21+
22+
buf, err := ioutil.ReadAll(os.Stdin)
23+
if err != nil {
24+
fmt.Fprintf(os.Stderr, "error: failed to read from stdin\n%v", err)
1925
os.Exit(1)
2026
}
27+
28+
for _, t := range targets {
29+
f, err := os.OpenFile(t, os.O_RDWR|os.O_CREATE, 0666)
30+
if err != nil {
31+
fmt.Fprintf(os.Stderr, "error: failed to open '%v'\n%v\n", t, err)
32+
os.Exit(1)
33+
}
34+
defer f.Close()
35+
36+
if err := Translate(bytes.NewReader(buf), f, cfg); err != nil {
37+
fmt.Fprintf(os.Stderr, "error: %v\n", err)
38+
os.Exit(1)
39+
}
40+
}
2141
}
2242

2343
func Translate(r io.Reader, w io.Writer, cfg Config) error {

0 commit comments

Comments
 (0)