Skip to content

Commit 32f77ce

Browse files
authored
Use lazy reader (#5)
1 parent dfe4b2e commit 32f77ce

4 files changed

Lines changed: 24 additions & 33 deletions

File tree

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: 2.1
22
executors:
33
base:
44
docker:
5-
- image: circleci/golang:1.12
5+
- image: circleci/golang:1.13
66
working_directory: /go/src/github.com/spatialcurrent/gocat
77
jobs:
88
pre_deps_golang:

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2019 Spatial Current, Inc.
3+
Copyright (c) 2020 Spatial Current, Inc.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ See the usage below or the following examples.
4141
```shell
4242
gocat is a super simple utility to concatenate files (local, remote, or on AWS S3) provided as positional arguments.
4343
Supports stdin (aka "-"), local files (path/to/file or file://path/to/file), remote files (http://path/to/file), or files on AWS S3 (s3://path/to/file).
44-
Supports the following compression algorithms: bzip2, flate, gzip, none, snappy, zip, zlib
44+
Supports the following compression algorithms: bzip2, flate, gzip, none, snappy, zip, zlib.
4545

4646
Usage:
4747
gocat [flags] [-|stdin|FILE|URI]...
@@ -92,7 +92,7 @@ The `make build_cli` script is used to build executables for Linux and Windows.
9292

9393
**Changing Destination**
9494

95-
The default destination for build artifacts is `go-reader-writer/bin`, but you can change the destination with an environment variable. For building on a Chromebook consider saving the artifacts in `/usr/local/go/bin`, e.g., `DEST=/usr/local/go/bin make build_cli`
95+
The default destination for build artifacts is `gocat/bin`, but you can change the destination with an environment variable. For building on a Chromebook consider saving the artifacts in `/usr/local/go/bin`, e.g., `DEST=/usr/local/go/bin make build_cli`
9696

9797
## Testing
9898

main.go

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// =================================================================
22
//
3-
// Copyright (C) 2019 Spatial Current, Inc. - All Rights Reserved
3+
// Copyright (C) 2020 Spatial Current, Inc. - All Rights Reserved
44
// Released as open source under the MIT License. See LICENSE file.
55
//
66
// =================================================================
@@ -11,19 +11,18 @@ import (
1111
"bytes"
1212
"fmt"
1313
"io"
14-
"io/ioutil"
1514
"os"
1615
"strings"
1716

1817
"github.com/aws/aws-sdk-go/aws"
1918
"github.com/aws/aws-sdk-go/aws/credentials"
2019
awssession "github.com/aws/aws-sdk-go/aws/session"
2120
"github.com/aws/aws-sdk-go/service/s3"
22-
"github.com/pkg/errors"
2321
"github.com/spf13/cobra"
2422
"github.com/spf13/pflag"
2523
"github.com/spf13/viper"
2624

25+
"github.com/spatialcurrent/go-lazy/pkg/lazy"
2726
"github.com/spatialcurrent/go-reader-writer/pkg/grw"
2827
)
2928

@@ -54,7 +53,7 @@ func initViper(cmd *cobra.Command) (*viper.Viper, error) {
5453
v := viper.New()
5554
err := v.BindPFlags(cmd.Flags())
5655
if err != nil {
57-
return v, errors.Wrap(err, "error binding flag set to viper")
56+
return v, fmt.Errorf("error binding flag set to viper: %w", err)
5857
}
5958
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
6059
v.AutomaticEnv() // set environment variables to overwrite config
@@ -76,11 +75,11 @@ func main() {
7675
Short: "gocat is a super simple utility to concatenate files (local, remote, or on AWS S3) provided as positional arguments.",
7776
Long: `gocat is a super simple utility to concatenate files (local, remote, or on AWS S3) provided as positional arguments.
7877
Supports stdin (aka "-"), local files (path/to/file or file://path/to/file), remote files (http://path/to/file), or files on AWS S3 (s3://path/to/file).
79-
Supports the following compression algorithms: ` + strings.Join(grw.Algorithms, ", "),
78+
Supports the following compression algorithms: ` + strings.Join(grw.Algorithms, ", ") + `.`,
8079
RunE: func(cmd *cobra.Command, args []string) error {
8180
v, err := initViper(cmd)
8281
if err != nil {
83-
return errors.Wrap(err, "error initializing viper")
82+
return fmt.Errorf("error initializing viper: %w", err)
8483
}
8584

8685
if len(args) == 0 {
@@ -94,14 +93,13 @@ Supports the following compression algorithms: ` + strings.Join(grw.Algorithms,
9493
bufferSize := v.GetInt(flagBufferSize)
9594
appendNewlines := v.GetBool(flagAppendNewlines)
9695

97-
stdinBytes := make([]byte, 0)
98-
9996
var session *awssession.Session
10097

10198
var s3Client *s3.S3
10299

103100
inputReaders := make([]io.Reader, 0)
104101
for _, uri := range args {
102+
uri := uri
105103

106104
if uri == "-" {
107105
uri = "stdin"
@@ -142,28 +140,21 @@ Supports the following compression algorithms: ` + strings.Join(grw.Algorithms,
142140
}
143141

144142
if uri == "stdin" {
145-
if len(stdinBytes) == 0 {
146-
b, err := ioutil.ReadAll(os.Stdin)
143+
inputReaders = append(inputReaders, os.Stdin)
144+
} else {
145+
inputReaders = append(inputReaders, lazy.NewLazyReader(func() (io.Reader, error) {
146+
r, _, err := grw.ReadFromResource(&grw.ReadFromResourceInput{
147+
Uri: uri,
148+
Alg: "none",
149+
Dict: grw.NoDict,
150+
BufferSize: bufferSize,
151+
S3Client: s3Client,
152+
})
147153
if err != nil {
148-
return errors.Wrap(err, "error reading from stdin")
154+
return nil, fmt.Errorf("error reading from uri %q: %w", uri, err)
149155
}
150-
stdinBytes = b
151-
}
152-
if len(stdinBytes) > 0 {
153-
inputReaders = append(inputReaders, bytes.NewReader(stdinBytes))
154-
}
155-
} else {
156-
inputReader, _, err := grw.ReadFromResource(&grw.ReadFromResourceInput{
157-
Uri: uri,
158-
Alg: "none",
159-
Dict: grw.NoDict,
160-
BufferSize: bufferSize,
161-
S3Client: s3Client,
162-
})
163-
if err != nil {
164-
return errors.Wrap(err, fmt.Sprintf("error reading from uri %q", uri))
165-
}
166-
inputReaders = append(inputReaders, inputReader)
156+
return r, nil
157+
}))
167158
}
168159

169160
if appendNewlines {
@@ -173,7 +164,7 @@ Supports the following compression algorithms: ` + strings.Join(grw.Algorithms,
173164
}
174165

175166
if _, err := io.Copy(os.Stdout, io.MultiReader(inputReaders...)); err != nil {
176-
return errors.Wrap(err, "error copying input")
167+
return fmt.Errorf("error copying input: %w", err)
177168
}
178169

179170
return nil

0 commit comments

Comments
 (0)