Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit 571b23b

Browse files
committed
Simplify IO
1 parent c8e9348 commit 571b23b

File tree

4 files changed

+56
-29
lines changed

4 files changed

+56
-29
lines changed

cmd/identify/identify.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package identify
22

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/spf13/cobra"
87
"github.com/stilvoid/please"
8+
"github.com/stilvoid/please/internal"
99
)
1010

1111
var Cmd = &cobra.Command{
12-
Use: "identify [filename]",
13-
Short: "Identify the format of some structured data",
14-
Args: cobra.ExactArgs(1),
12+
Use: "identify (FILENAME)",
13+
Short: "Identify the format of some structured data from FILENAME or stdin if omitted",
14+
Args: cobra.RangeArgs(0, 1),
1515
Run: func(cmd *cobra.Command, args []string) {
16-
data, err := os.ReadFile(args[0])
16+
data, err := internal.ReadFileOrStdin(args...)
1717
cobra.CheckErr(err)
1818

1919
format, _, err := Identify(data)

cmd/parse/parse.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package parse
22

33
import (
4-
"errors"
54
"fmt"
6-
"os"
75

86
"github.com/jmespath/go-jmespath"
97
"github.com/spf13/cobra"
108
"github.com/stilvoid/please"
119
"github.com/stilvoid/please/cmd/identify"
10+
"github.com/stilvoid/please/internal"
1211
)
1312

1413
var inFormat string
@@ -24,8 +23,8 @@ func init() {
2423
}
2524

2625
var Cmd = &cobra.Command{
27-
Use: "parse [filename]",
28-
Short: "Parse and convert structured data",
26+
Use: "parse (FILENAME)",
27+
Short: "Parse and convert structured data from FILENAME or stdin if omitted",
2928
Args: cobra.RangeArgs(0, 1),
3029
Run: func(cmd *cobra.Command, args []string) {
3130
if listMode {
@@ -39,12 +38,10 @@ var Cmd = &cobra.Command{
3938
fmt.Printf(" %s\n", name)
4039
}
4140
return
42-
} else if len(args) != 1 {
43-
cobra.CheckErr(errors.New("You must supply a filename"))
4441
}
4542

46-
// Read from stdin
47-
input, err := os.ReadFile(args[0])
43+
// Read from stdin?
44+
input, err := internal.ReadFileOrStdin(args...)
4845
cobra.CheckErr(err)
4946

5047
// Try parsing

cmd/respond/respond.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ import (
99
"net"
1010
"net/http"
1111
"net/textproto"
12-
"os"
1312

14-
"github.com/andrew-d/go-termutil"
1513
"github.com/spf13/cobra"
1614
"github.com/stilvoid/please/internal"
1715
)
@@ -30,7 +28,7 @@ func init() {
3028
Cmd.Flags().BoolVarP(&includeHeaders, "output-headers", "o", false, "Output request headers")
3129
Cmd.Flags().BoolVarP(&includeMethod, "output-method", "m", false, "Output request method")
3230
Cmd.Flags().BoolVarP(&includeUrl, "output-url", "u", false, "Output request URL")
33-
Cmd.Flags().StringVarP(&bodyFn, "body", "b", "", "Filename to read the response body from. Use - for stdin.")
31+
Cmd.Flags().StringVarP(&bodyFn, "body", "b", "", "Filename to read the response body from. Use - or omit for stdin")
3432
Cmd.Flags().StringVarP(&address, "address", "a", "", "Address to listen on")
3533
Cmd.Flags().IntVarP(&port, "port", "p", 8000, "Port to listen on")
3634
Cmd.Flags().IntVarP(&status, "status", "s", 200, "Status code to respond with")
@@ -57,18 +55,12 @@ var Cmd = &cobra.Command{
5755
listener, err := net.Listen("tcp", address)
5856
cobra.CheckErr(err)
5957

60-
if bodyFn != "" {
61-
if bodyFn == "" {
62-
if termutil.Isatty(os.Stdin.Fd()) {
63-
cobra.CheckErr(errors.New("Unable to read from stdin"))
64-
} else {
65-
handler.data = os.Stdin
66-
}
67-
} else {
68-
var err error
69-
handler.data, err = os.Open(bodyFn)
70-
cobra.CheckErr(err)
71-
}
58+
if bodyFn == "" {
59+
handler.data, err = internal.StdinOrNothing()
60+
cobra.CheckErr(err)
61+
} else {
62+
handler.data, err = internal.FileOrStdin(bodyFn)
63+
cobra.CheckErr(err)
7264
}
7365

7466
server := &http.Server{Addr: address, Handler: handler}
@@ -96,7 +88,7 @@ type responder struct {
9688
includeMethod bool
9789
includeUrl bool
9890
headersIncluded bool
99-
data io.ReadSeeker
91+
data io.Reader
10092
}
10193

10294
func (h responder) ServeHTTP(w http.ResponseWriter, req *http.Request) {

internal/io.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package internal
2+
3+
import (
4+
"errors"
5+
"io"
6+
"os"
7+
8+
"github.com/andrew-d/go-termutil"
9+
)
10+
11+
func StdinOrNothing() (io.Reader, error) {
12+
if termutil.Isatty(os.Stdin.Fd()) {
13+
return nil, nil
14+
}
15+
16+
return os.Stdin, nil
17+
}
18+
19+
func FileOrStdin(args ...string) (io.Reader, error) {
20+
if len(args) == 0 || args[0] == "-" {
21+
if termutil.Isatty(os.Stdin.Fd()) {
22+
return nil, errors.New("Unable to read from stdin")
23+
}
24+
25+
return os.Stdin, nil
26+
}
27+
28+
return os.Open(args[0])
29+
}
30+
31+
func ReadFileOrStdin(args ...string) ([]byte, error) {
32+
reader, err := FileOrStdin(args...)
33+
if err != nil {
34+
return []byte{}, err
35+
}
36+
37+
return io.ReadAll(reader)
38+
}

0 commit comments

Comments
 (0)