Skip to content

Commit 5ae1f6e

Browse files
committed
Move parsers/formatters back into parse/format and move main into root
1 parent 1c3648c commit 5ae1f6e

File tree

7 files changed

+93
-73
lines changed

7 files changed

+93
-73
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
Please is a utility for making and receiving web requests and parsing and reformatting the common data formats that are sent over them.
22

3+
## Installing
4+
5+
`brew install stilvoid/tools/please`
6+
7+
_or_
8+
9+
Download a binary from the [releases](https://github.com/stilvoid/please/releases) page.
10+
11+
_or_
12+
13+
Run `go install github.com/stilvoid/please@latest`
14+
315
## Usage
416

517
```

cmd/identify/identify.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"os"
66

77
"github.com/spf13/cobra"
8-
"github.com/stilvoid/please"
98
"github.com/stilvoid/please/internal"
9+
"github.com/stilvoid/please/parse"
1010
)
1111

1212
var Cmd = &cobra.Command{
@@ -45,7 +45,7 @@ var order = []string{
4545
// If the data format could not be identified, an error will be returned
4646
func Identify(input []byte) (string, any, error) {
4747
for _, name := range order {
48-
output, err := please.Parse(name, input)
48+
output, err := parse.Parse(name, input)
4949
if err != nil {
5050
continue
5151
}

cmd/parse/parse.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77

88
"github.com/jmespath/go-jmespath"
99
"github.com/spf13/cobra"
10-
"github.com/stilvoid/please"
1110
"github.com/stilvoid/please/cmd/identify"
11+
"github.com/stilvoid/please/format"
1212
"github.com/stilvoid/please/internal"
13+
"github.com/stilvoid/please/parse"
1314
)
1415

1516
var inFormat string
@@ -23,12 +24,12 @@ func init() {
2324

2425
formats := strings.Builder{}
2526
formats.WriteString("Input formats:\n")
26-
for _, name := range please.Parsers {
27+
for _, name := range parse.Parsers {
2728
formats.WriteString(fmt.Sprintf(" %s\n", name))
2829
}
2930
formats.WriteString("\n")
3031
formats.WriteString("Output formats:\n")
31-
for _, name := range please.Formatters {
32+
for _, name := range format.Formatters {
3233
formats.WriteString(fmt.Sprintf(" %s\n", name))
3334
}
3435

@@ -58,7 +59,7 @@ var Cmd = &cobra.Command{
5859
inFormat, parsed, err = identify.Identify(input)
5960
} else {
6061
// Try parsing
61-
parsed, err = please.Parse(inFormat, input)
62+
parsed, err = parse.Parse(inFormat, input)
6263
}
6364

6465
cobra.CheckErr(err)
@@ -74,7 +75,7 @@ var Cmd = &cobra.Command{
7475
}
7576

7677
// ...and format back out :)
77-
output, err := please.Format(outFormat, parsed)
78+
output, err := format.Format(outFormat, parsed)
7879
cobra.CheckErr(err)
7980

8081
fmt.Println(output)

format/format.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package format
2+
3+
import (
4+
"fmt"
5+
"maps"
6+
"slices"
7+
"sort"
8+
)
9+
10+
var formatters = map[string]func(any) (string, error){
11+
"bash": Bash,
12+
"dot": Dot,
13+
"json": Json,
14+
"xml": Xml,
15+
"yaml": Yaml,
16+
"query": Query,
17+
"toml": Toml,
18+
}
19+
20+
// Formatters are the names of data formats that please can output
21+
var Formatters []string
22+
23+
func init() {
24+
Formatters = slices.Collect(maps.Keys(formatters))
25+
sort.Strings(Formatters)
26+
}
27+
28+
// Format converts input data into format as a byte array
29+
func Format(format string, input any) (string, error) {
30+
formatter, ok := formatters[format]
31+
if !ok {
32+
return "", fmt.Errorf("Invalid formatter: %s", format)
33+
}
34+
35+
return formatter(input)
36+
}
File renamed without changes.

parse/parse.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package parse
2+
3+
import (
4+
"fmt"
5+
"maps"
6+
"slices"
7+
"sort"
8+
)
9+
10+
var parsers = map[string]func([]byte) (any, error){
11+
"csv": Csv,
12+
"html": Html,
13+
"json": Json,
14+
"mime": Mime,
15+
"xml": Xml,
16+
"yaml": Yaml,
17+
"query": Query,
18+
"toml": Toml,
19+
}
20+
21+
// Parsers are the names of data formats that please can read
22+
var Parsers []string
23+
24+
func init() {
25+
Parsers = slices.Collect(maps.Keys(parsers))
26+
sort.Strings(Parsers)
27+
}
28+
29+
// Parse reads input as format and returns data
30+
func Parse(format string, input []byte) (any, error) {
31+
parser, ok := parsers[format]
32+
if !ok {
33+
return nil, fmt.Errorf("Invalid parser: %s", format)
34+
}
35+
36+
return parser(input)
37+
}

please.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

0 commit comments

Comments
 (0)