refactor: expose generated code and client business logic to share with Kapacitor (#103)
* refactor: take clients out of internal * refactor: move stdio to pkg * Move internal/api to api * refactor: final changes for Kapacitor to access shared functionality * chore: regenerate mocks * fix: bad automated refactor * chore: extra formatting not caught by make fmt
This commit is contained in:
56
clients/helpers.go
Normal file
56
clients/helpers.go
Normal file
@ -0,0 +1,56 @@
|
||||
package clients
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
// ReadQuery reads a Flux query into memory from a --file argument, args, or stdin
|
||||
func ReadQuery(ctx *cli.Context) (string, error) {
|
||||
nargs := ctx.NArg()
|
||||
file := ctx.String("file")
|
||||
|
||||
if nargs > 1 {
|
||||
return "", fmt.Errorf("at most 1 query string can be specified over the CLI, got %d", ctx.NArg())
|
||||
}
|
||||
if nargs == 1 && file != "" {
|
||||
return "", fmt.Errorf("query can be specified via --file or over the CLI, not both")
|
||||
}
|
||||
|
||||
readFile := func(path string) (string, error) {
|
||||
queryBytes, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read query from %q: %w", path, err)
|
||||
}
|
||||
return string(queryBytes), nil
|
||||
}
|
||||
|
||||
readStdin := func() (string, error) {
|
||||
queryBytes, err := ioutil.ReadAll(os.Stdin)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to read query from stdin: %w", err)
|
||||
}
|
||||
return string(queryBytes), err
|
||||
}
|
||||
|
||||
if file != "" {
|
||||
return readFile(file)
|
||||
}
|
||||
if nargs == 0 {
|
||||
return readStdin()
|
||||
}
|
||||
|
||||
arg := ctx.Args().Get(0)
|
||||
// Backwards compatibility.
|
||||
if strings.HasPrefix(arg, "@") {
|
||||
return readFile(arg[1:])
|
||||
} else if arg == "-" {
|
||||
return readStdin()
|
||||
} else {
|
||||
return arg, nil
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user