diff --git a/cmd/faq/main.go b/cmd/faq/main.go index 8086fa8..accd31a 100644 --- a/cmd/faq/main.go +++ b/cmd/faq/main.go @@ -3,17 +3,18 @@ package main import ( "os" - "github.com/jzelinskie/faq/pkg/flagutil" "github.com/spf13/cobra" + + "github.com/jzelinskie/faq/pkg/pflagutil" ) func main() { var flags flags - stringKwargsFlag := flagutil.NewKwargStringFlag(&flags.Kwargs) - jsonKwargsFlag := flagutil.NewKwargJSONFlag(&flags.Jsonkwargs) - stringPositionalArgsFlag := flagutil.NewPositionalArgStringFlag(&flags.Args) - jsonPositionalArgsFlag := flagutil.NewPositionalArgJSONFlag(&flags.Jsonargs) + stringKwargsFlag := pflagutil.NewKwargStringFlag(&flags.Kwargs) + jsonKwargsFlag := pflagutil.NewKwargJSONFlag(&flags.Jsonkwargs) + stringPositionalArgsFlag := pflagutil.NewPositionalArgStringFlag(&flags.Args) + jsonPositionalArgsFlag := pflagutil.NewPositionalArgJSONFlag(&flags.Jsonargs) var rootCmd = &cobra.Command{ Use: "faq [flags] [filter string] [files...]", diff --git a/cmd/faq/root.go b/cmd/faq/root.go index 677a30a..a927e41 100644 --- a/cmd/faq/root.go +++ b/cmd/faq/root.go @@ -11,9 +11,9 @@ import ( "github.com/spf13/cobra" "golang.org/x/crypto/ssh/terminal" - "github.com/jzelinskie/faq/pkg/faq" - "github.com/jzelinskie/faq/pkg/formats" - "github.com/jzelinskie/faq/pkg/version" + "github.com/jzelinskie/faq/internal/faq" + "github.com/jzelinskie/faq/internal/version" + "github.com/jzelinskie/faq/pkg/objconv" ) func runCmdFunc(cmd *cobra.Command, args []string, flags flags) error { @@ -106,7 +106,7 @@ func runCmdFunc(cmd *cobra.Command, args []string, flags flags) error { if flags.OutputFormat == "auto" { flags.OutputFormat = "json" } - encoding, ok := formats.ByName(flags.OutputFormat) + encoding, ok := objconv.ByName(flags.OutputFormat) if !ok { return fmt.Errorf("invalid --output-format %s", flags.OutputFormat) } @@ -121,7 +121,7 @@ func runCmdFunc(cmd *cobra.Command, args []string, flags flags) error { if flags.OutputFormat == "" { return fmt.Errorf("must specify --output-format when using --slurp") } - encoding, ok := formats.ByName(flags.OutputFormat) + encoding, ok := objconv.ByName(flags.OutputFormat) if !ok { return fmt.Errorf("invalid --output-format %s", flags.OutputFormat) } diff --git a/pkg/faq/faq.go b/internal/faq/faq.go similarity index 92% rename from pkg/faq/faq.go rename to internal/faq/faq.go index 26bde0d..ae5b0bb 100644 --- a/pkg/faq/faq.go +++ b/internal/faq/faq.go @@ -12,14 +12,15 @@ import ( "unicode" "github.com/Azure/draft/pkg/linguist" - "github.com/jzelinskie/faq/pkg/formats" - "github.com/jzelinskie/faq/pkg/jq" "github.com/sirupsen/logrus" + + "github.com/jzelinskie/faq/internal/jq" + "github.com/jzelinskie/faq/pkg/objconv" ) // ProcessEachFile takes a list of files, and for each, attempts to convert it // to a JSON value and runs ExecuteProgram against each. -func ProcessEachFile(inputFormat string, files []File, program string, programArgs ProgramArguments, outputWriter io.Writer, outputEncoding formats.Encoding, outputConf OutputConfig, rawOutput bool) error { +func ProcessEachFile(inputFormat string, files []File, program string, programArgs ProgramArguments, outputWriter io.Writer, outputEncoding objconv.Encoding, outputConf OutputConfig, rawOutput bool) error { encoder := outputEncoding.NewEncoder(outputWriter) for _, file := range files { decoderEncoding, file, err := DetermineEncoding(inputFormat, file) @@ -55,7 +56,7 @@ func ProcessEachFile(inputFormat string, files []File, program string, programAr // SlurpAllFiles takes a list of files, and for each, attempts to convert it to // a JSON value and appends each JSON value to an array, and passes that array // as the input ExecuteProgram. -func SlurpAllFiles(inputFormat string, files []File, program string, programArgs ProgramArguments, outputWriter io.Writer, encoding formats.Encoding, outputConf OutputConfig, rawOutput bool) error { +func SlurpAllFiles(inputFormat string, files []File, program string, programArgs ProgramArguments, outputWriter io.Writer, encoding objconv.Encoding, outputConf OutputConfig, rawOutput bool) error { data, err := combineJSONFilesToJSONArray(files, inputFormat) if err != nil { return err @@ -73,12 +74,12 @@ func SlurpAllFiles(inputFormat string, files []File, program string, programArgs // ProcessInput takes input, a single JSON value, and runs program via libjq // against it, writing the results to outputWriter. -func ProcessInput(input *[]byte, program string, programArgs ProgramArguments, outputWriter io.Writer, encoding formats.Encoding, outputConf OutputConfig, rawOutput bool) error { +func ProcessInput(input *[]byte, program string, programArgs ProgramArguments, outputWriter io.Writer, encoding objconv.Encoding, outputConf OutputConfig, rawOutput bool) error { encoder := encoding.NewEncoder(outputWriter) return processInput(input, program, programArgs, encoder, outputConf, rawOutput) } -func processInput(input *[]byte, program string, programArgs ProgramArguments, encoder formats.Encoder, outputConf OutputConfig, rawOutput bool) error { +func processInput(input *[]byte, program string, programArgs ProgramArguments, encoder objconv.Encoder, outputConf OutputConfig, rawOutput bool) error { outputs, err := ExecuteProgram(input, program, programArgs, rawOutput) if err != nil { return err @@ -204,14 +205,14 @@ func marshalJqArgs(jsonBytes []byte, jqArgs ProgramArguments) ([]byte, error) { // DetermineEncoding returns an Encoding based on a file format and an input // file if input format is "auto". Since auto detection may consume the file, // DetermineEncoding returns a copy of the original File. -func DetermineEncoding(format string, file File) (formats.Encoding, File, error) { - var encoding formats.Encoding +func DetermineEncoding(format string, file File) (objconv.Encoding, File, error) { + var encoding objconv.Encoding var err error if format == "auto" { encoding, file, err = detectFormat(file) } else { var ok bool - encoding, ok = formats.ByName(format) + encoding, ok = objconv.ByName(format) if !ok { err = fmt.Errorf("no supported format found named %s", format) } @@ -225,9 +226,9 @@ func DetermineEncoding(format string, file File) (formats.Encoding, File, error) var yamlSeparator = []byte("---") -func detectFormat(file File) (formats.Encoding, File, error) { +func detectFormat(file File) (objconv.Encoding, File, error) { if ext := filepath.Ext(file.Path()); ext != "" { - if format, ok := formats.ByName(ext[1:]); ok { + if format, ok := objconv.ByName(ext[1:]); ok { return format, file, nil } } @@ -289,7 +290,7 @@ func detectFormat(file File) (formats.Encoding, File, error) { file = NewFile(file.Path(), ioutil.NopCloser(bytes.NewBuffer(fileBytes))) } - enc, ok := formats.ByName(format) + enc, ok := objconv.ByName(format) if !ok { return nil, nil, errors.New("failed to detect format of the input") } diff --git a/pkg/faq/faq_test.go b/internal/faq/faq_test.go similarity index 97% rename from pkg/faq/faq_test.go rename to internal/faq/faq_test.go index 515e288..894f6f8 100644 --- a/pkg/faq/faq_test.go +++ b/internal/faq/faq_test.go @@ -7,7 +7,7 @@ import ( "strings" "testing" - "github.com/jzelinskie/faq/pkg/formats" + "github.com/jzelinskie/faq/pkg/objconv" ) func TestProcessEachFile(t *testing.T) { @@ -197,7 +197,7 @@ bar: false files = append(files, newFileFromString("test-path-"+strconv.Itoa(i), fileContent)) } - encoding, ok := formats.ByName(testCase.outputFormat) + encoding, ok := objconv.ByName(testCase.outputFormat) if !ok { t.Errorf("invalid format: %s", testCase.outputFormat) } @@ -330,7 +330,7 @@ cats: dogs for i, fileContent := range testCase.inputFileContents { files = append(files, newFileFromString("test-path-"+strconv.Itoa(i), fileContent)) } - encoder, _ := formats.ByName(testCase.outputFormat) + encoder, _ := objconv.ByName(testCase.outputFormat) var outputBuf bytes.Buffer err := SlurpAllFiles(testCase.inputFormat, files, testCase.program, ProgramArguments{}, &outputBuf, encoder, OutputConfig{}, testCase.raw) if err != nil { @@ -368,7 +368,7 @@ func TestExecuteProgram(t *testing.T) { for _, testCase := range testCases { testCase := testCase t.Run(testCase.name, func(t *testing.T) { - encoder, _ := formats.ByName(testCase.outputFormat) + encoder, _ := objconv.ByName(testCase.outputFormat) var outputBuf bytes.Buffer err := ProcessInput(testCase.input, testCase.program, testCase.programArgs, &outputBuf, encoder, OutputConfig{}, testCase.raw) if err != nil { diff --git a/pkg/faq/file.go b/internal/faq/file.go similarity index 100% rename from pkg/faq/file.go rename to internal/faq/file.go diff --git a/pkg/jq/cgo.go b/internal/jq/cgo.go similarity index 100% rename from pkg/jq/cgo.go rename to internal/jq/cgo.go diff --git a/pkg/jq/exec.go b/internal/jq/exec.go similarity index 100% rename from pkg/jq/exec.go rename to internal/jq/exec.go diff --git a/pkg/version/version.go b/internal/version/version.go similarity index 100% rename from pkg/version/version.go rename to internal/version/version.go diff --git a/pkg/formats/bencode.go b/pkg/objconv/bencode.go similarity index 99% rename from pkg/formats/bencode.go rename to pkg/objconv/bencode.go index f71538d..67f188e 100644 --- a/pkg/formats/bencode.go +++ b/pkg/objconv/bencode.go @@ -1,4 +1,4 @@ -package formats +package objconv import ( "bytes" diff --git a/pkg/formats/bencode_test.go b/pkg/objconv/bencode_test.go similarity index 98% rename from pkg/formats/bencode_test.go rename to pkg/objconv/bencode_test.go index a2c7603..cb31231 100644 --- a/pkg/formats/bencode_test.go +++ b/pkg/objconv/bencode_test.go @@ -1,4 +1,4 @@ -package formats +package objconv import ( "bytes" diff --git a/pkg/formats/bson.go b/pkg/objconv/bson.go similarity index 98% rename from pkg/formats/bson.go rename to pkg/objconv/bson.go index cd4ce57..3f8cfa9 100644 --- a/pkg/formats/bson.go +++ b/pkg/objconv/bson.go @@ -1,4 +1,4 @@ -package formats +package objconv import ( "encoding/json" diff --git a/pkg/formats/formats.go b/pkg/objconv/formats.go similarity index 99% rename from pkg/formats/formats.go rename to pkg/objconv/formats.go index 48d58f5..063c833 100644 --- a/pkg/formats/formats.go +++ b/pkg/objconv/formats.go @@ -1,4 +1,4 @@ -package formats +package objconv import ( "bytes" diff --git a/pkg/formats/json.go b/pkg/objconv/json.go similarity index 99% rename from pkg/formats/json.go rename to pkg/objconv/json.go index abaa9ac..16d4569 100644 --- a/pkg/formats/json.go +++ b/pkg/objconv/json.go @@ -1,4 +1,4 @@ -package formats +package objconv import ( "bytes" diff --git a/pkg/formats/plist.go b/pkg/objconv/plist.go similarity index 99% rename from pkg/formats/plist.go rename to pkg/objconv/plist.go index 30766e8..8a212c2 100644 --- a/pkg/formats/plist.go +++ b/pkg/objconv/plist.go @@ -1,4 +1,4 @@ -package formats +package objconv import ( "bytes" diff --git a/pkg/formats/toml.go b/pkg/objconv/toml.go similarity index 99% rename from pkg/formats/toml.go rename to pkg/objconv/toml.go index f0dfd0e..032acb2 100644 --- a/pkg/formats/toml.go +++ b/pkg/objconv/toml.go @@ -1,4 +1,4 @@ -package formats +package objconv import ( "bytes" diff --git a/pkg/formats/util.go b/pkg/objconv/util.go similarity index 98% rename from pkg/formats/util.go rename to pkg/objconv/util.go index d7e503c..4f7853e 100644 --- a/pkg/formats/util.go +++ b/pkg/objconv/util.go @@ -1,4 +1,4 @@ -package formats +package objconv import "os" diff --git a/pkg/formats/xml.go b/pkg/objconv/xml.go similarity index 99% rename from pkg/formats/xml.go rename to pkg/objconv/xml.go index e9c1394..d607b9d 100644 --- a/pkg/formats/xml.go +++ b/pkg/objconv/xml.go @@ -1,4 +1,4 @@ -package formats +package objconv import ( "bytes" diff --git a/pkg/formats/xml_test.go b/pkg/objconv/xml_test.go similarity index 99% rename from pkg/formats/xml_test.go rename to pkg/objconv/xml_test.go index 7e51ec4..92baa2c 100644 --- a/pkg/formats/xml_test.go +++ b/pkg/objconv/xml_test.go @@ -1,4 +1,4 @@ -package formats +package objconv import ( "bytes" diff --git a/pkg/formats/yaml.go b/pkg/objconv/yaml.go similarity index 99% rename from pkg/formats/yaml.go rename to pkg/objconv/yaml.go index b58dd8d..2130e30 100644 --- a/pkg/formats/yaml.go +++ b/pkg/objconv/yaml.go @@ -1,4 +1,4 @@ -package formats +package objconv import ( "bytes" diff --git a/pkg/formats/yaml_helpers.go b/pkg/objconv/yaml_helpers.go similarity index 99% rename from pkg/formats/yaml_helpers.go rename to pkg/objconv/yaml_helpers.go index c6288bb..2d12d9f 100644 --- a/pkg/formats/yaml_helpers.go +++ b/pkg/objconv/yaml_helpers.go @@ -1,11 +1,12 @@ -// this file contains code from https://github.com/ghodss/yaml/tree/c7ce16629ff4cd059ed96ed06419dd3856fd3577 +// Package objconv contains code for converting formats isomorphic with JSON. +// +// This file contains code from https://github.com/ghodss/yaml/tree/c7ce16629ff4cd059ed96ed06419dd3856fd3577 // it contains convertToJSONableObject from yaml.go and all of fields.go - -// Package formats contains code for dealing with formats isomorphic with JSON. +// // Copyright 2013 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -package formats +package objconv import ( "bytes" diff --git a/pkg/flagutil/flags.go b/pkg/pflagutil/pflagutil.go similarity index 97% rename from pkg/flagutil/flags.go rename to pkg/pflagutil/pflagutil.go index 01f1133..f7b416c 100644 --- a/pkg/flagutil/flags.go +++ b/pkg/pflagutil/pflagutil.go @@ -1,4 +1,4 @@ -package flagutil +package pflagutil import ( "encoding/json" @@ -8,8 +8,8 @@ import ( "github.com/spf13/pflag" ) +// Enforce that these types implement the pflag.Value interface at compile time. var ( - // validate these types implement the pflag.Value interface at compile time _ pflag.Value = &KwargStringFlag{} _ pflag.Value = &KwargJSONFlag{} _ pflag.Value = &PositionalArgStringFlag{}