Skip to content

Commit

Permalink
internal: init & rename public pkgs
Browse files Browse the repository at this point in the history
  • Loading branch information
jzelinskie committed Apr 20, 2021
1 parent ba3a48d commit b5e8c4e
Show file tree
Hide file tree
Showing 21 changed files with 46 additions and 43 deletions.
11 changes: 6 additions & 5 deletions cmd/faq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...]",
Expand Down
10 changes: 5 additions & 5 deletions cmd/faq/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
25 changes: 13 additions & 12 deletions pkg/faq/faq.go → internal/faq/faq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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")
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/faq/faq_test.go → internal/faq/faq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
"testing"

"github.com/jzelinskie/faq/pkg/formats"
"github.com/jzelinskie/faq/pkg/objconv"
)

func TestProcessEachFile(t *testing.T) {
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion pkg/formats/bencode.go → pkg/objconv/bencode.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package formats
package objconv

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package formats
package objconv

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/bson.go → pkg/objconv/bson.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package formats
package objconv

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/formats.go → pkg/objconv/formats.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package formats
package objconv

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/json.go → pkg/objconv/json.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package formats
package objconv

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/plist.go → pkg/objconv/plist.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package formats
package objconv

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/toml.go → pkg/objconv/toml.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package formats
package objconv

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/util.go → pkg/objconv/util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package formats
package objconv

import "os"

Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/xml.go → pkg/objconv/xml.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package formats
package objconv

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/xml_test.go → pkg/objconv/xml_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package formats
package objconv

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion pkg/formats/yaml.go → pkg/objconv/yaml.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package formats
package objconv

import (
"bytes"
Expand Down
9 changes: 5 additions & 4 deletions pkg/formats/yaml_helpers.go → pkg/objconv/yaml_helpers.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
4 changes: 2 additions & 2 deletions pkg/flagutil/flags.go → pkg/pflagutil/pflagutil.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package flagutil
package pflagutil

import (
"encoding/json"
Expand All @@ -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{}
Expand Down

0 comments on commit b5e8c4e

Please sign in to comment.