Skip to content

Commit b3c4118

Browse files
authored
Merge pull request #4 from stilvoid/modernise
Modernise
2 parents acbe8ed + a4e28ec commit b3c4118

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1202
-1799
lines changed

.gitignore

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

README.md

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

TODO.md

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

build.sh

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

cmd/identify/identify.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package identify
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/stilvoid/please"
9+
"github.com/stilvoid/please/internal"
10+
)
11+
12+
var Cmd = &cobra.Command{
13+
Use: "identify (FILENAME)",
14+
Short: "Identify the format of some structured data from FILENAME or stdin if omitted",
15+
Args: cobra.RangeArgs(0, 1),
16+
Run: func(cmd *cobra.Command, args []string) {
17+
data, err := internal.ReadFileOrStdin(args...)
18+
if err != nil {
19+
if len(args) == 0 {
20+
cmd.Help()
21+
os.Exit(1)
22+
}
23+
cobra.CheckErr(err)
24+
}
25+
26+
format, _, err := Identify(data)
27+
cobra.CheckErr(err)
28+
29+
fmt.Println(format)
30+
},
31+
}
32+
33+
// These should be in order of least to most likely
34+
// i.e. more picky formats should be listed first
35+
var order = []string{
36+
"xml",
37+
"mime",
38+
"json",
39+
"toml",
40+
"yaml",
41+
}
42+
43+
// Identify tries to figure out the format of the structured data passed in
44+
// If successful, the name of the detected format and a copy of its data parsed into an any will be returned
45+
// If the data format could not be identified, an error will be returned
46+
func Identify(input []byte) (string, any, error) {
47+
for _, name := range order {
48+
output, err := please.Parse(name, input)
49+
if err != nil {
50+
continue
51+
}
52+
53+
return name, output, nil
54+
}
55+
56+
return "", nil, fmt.Errorf("input format could not be identified")
57+
}

cmd/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"github.com/stilvoid/please/cmd/please"
6+
)
7+
8+
func main() {
9+
cobra.CheckErr(please.Cmd.Execute())
10+
}

cmd/parse/parse.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package parse
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
8+
"github.com/jmespath/go-jmespath"
9+
"github.com/spf13/cobra"
10+
"github.com/stilvoid/please"
11+
"github.com/stilvoid/please/cmd/identify"
12+
"github.com/stilvoid/please/internal"
13+
)
14+
15+
var inFormat string
16+
var outFormat string
17+
var query string
18+
19+
func init() {
20+
Cmd.Flags().StringVarP(&inFormat, "from", "f", "auto", "input format (see please help parse for formats)")
21+
Cmd.Flags().StringVarP(&outFormat, "to", "t", "auto", "output format (see please help parse for formats)")
22+
Cmd.Flags().StringVarP(&query, "query", "q", "", "JMESPath query")
23+
24+
formats := strings.Builder{}
25+
formats.WriteString("Input formats:\n")
26+
for _, name := range please.Parsers {
27+
formats.WriteString(fmt.Sprintf(" %s\n", name))
28+
}
29+
formats.WriteString("\n")
30+
formats.WriteString("Output formats:\n")
31+
for _, name := range please.Formatters {
32+
formats.WriteString(fmt.Sprintf(" %s\n", name))
33+
}
34+
35+
Cmd.Long = "Parse and converted structured data from FILENAME or stdin if omitted.\n\n" + formats.String()
36+
}
37+
38+
var Cmd = &cobra.Command{
39+
Use: "parse (FILENAME)",
40+
Short: "Parse and convert structured data from FILENAME or stdin if omitted",
41+
Args: cobra.RangeArgs(0, 1),
42+
Run: func(cmd *cobra.Command, args []string) {
43+
input, err := internal.ReadFileOrStdin(args...)
44+
if err != nil {
45+
if len(args) == 0 {
46+
fmt.Fprintln(os.Stderr, cmd.Short+"\n")
47+
cmd.Usage()
48+
os.Exit(1)
49+
}
50+
cobra.CheckErr(err)
51+
}
52+
53+
// Try parsing
54+
var parsed any
55+
56+
// Deal with format detection
57+
if inFormat == "auto" {
58+
inFormat, parsed, err = identify.Identify(input)
59+
} else {
60+
// Try parsing
61+
parsed, err = please.Parse(inFormat, input)
62+
}
63+
64+
cobra.CheckErr(err)
65+
66+
// Path
67+
if query != "" {
68+
parsed, err = jmespath.Search(query, parsed)
69+
cobra.CheckErr(err)
70+
}
71+
72+
if outFormat == "auto" {
73+
outFormat = inFormat
74+
}
75+
76+
// ...and format back out :)
77+
output, err := please.Format(outFormat, parsed)
78+
cobra.CheckErr(err)
79+
80+
fmt.Println(output)
81+
},
82+
}

cmd/please/please.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package please
2+
3+
import (
4+
"github.com/spf13/cobra"
5+
"github.com/stilvoid/please/cmd/identify"
6+
"github.com/stilvoid/please/cmd/parse"
7+
"github.com/stilvoid/please/cmd/request"
8+
"github.com/stilvoid/please/cmd/respond"
9+
"github.com/stilvoid/please/cmd/serve"
10+
"github.com/stilvoid/please/internal"
11+
)
12+
13+
func init() {
14+
Cmd.AddCommand(identify.Cmd)
15+
Cmd.AddCommand(parse.Cmd)
16+
Cmd.AddCommand(request.Cmd)
17+
Cmd.AddCommand(respond.Cmd)
18+
Cmd.AddCommand(serve.Cmd)
19+
20+
Cmd.Root().CompletionOptions.DisableDefaultCmd = true
21+
}
22+
23+
var Cmd = &cobra.Command{
24+
Use: "please",
25+
Short: "Please is a utility for making and receiving web requests and parsing and reformatting the common data formats that are sent over them.",
26+
Version: internal.Version,
27+
}

0 commit comments

Comments
 (0)