Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix print output text encoding #20

Merged
merged 1 commit into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"os"
"sort"

"github.com/maxnorth/nv/internal/resolver"
"github.com/spf13/cobra"
"gopkg.in/alessio/shellescape.v1"
"gopkg.in/yaml.v3"

"github.com/maxnorth/nv/internal/resolver"
)

func NewPrintCmd() *cobra.Command {
Expand Down Expand Up @@ -64,31 +67,32 @@ func printEnv(keys []string, output string) error {
return nil
}

var outputTemplate string
switch output {
case "yaml":
outputTemplate = "%s: \"%s\"\n"
case "env":
outputTemplate = "%s=%s\n"
case "shell":
outputTemplate = "export %s=\"%s\"\n"
if output == "yaml" {
yamlOutput, err := yaml.Marshal(values)
if err != nil {
return err
}
fmt.Print(string(yamlOutput))
return nil
}

if outputTemplate != "" {
if output == "shell" {
for _, key := range keys {
fmt.Printf(outputTemplate, key, values[key])
fmt.Printf("export %s=%s\n", key, shellescape.Quote(values[key]))
}
return nil
}

switch output {
case "keys":
outputTemplate = "%s\n"
if output == "env" {
for _, key := range keys {
fmt.Printf("%s=%s\n", key, values[key])
}
return nil
}

if outputTemplate != "" {
if output == "keys" {
for _, key := range keys {
fmt.Printf(outputTemplate, key)
fmt.Println(key)
}
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ require (
github.com/sergi/go-diff v1.3.1
github.com/spf13/cobra v1.8.0
github.com/tidwall/gjson v1.17.0
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/alessio/shellescape v1.4.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/match v1.1.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/alessio/shellescape v1.4.2 h1:MHPfaU+ddJ0/bYWpgIeUnQUqKrlJ1S7BfEYPM4uEoM0=
github.com/alessio/shellescape v1.4.2/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
Expand Down Expand Up @@ -30,6 +32,8 @@ github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JT
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61 h1:8ajkpB4hXVftY5ko905id+dOnmorcS2CHNxxHLLDcFM=
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61/go.mod h1:IfMagxm39Ys4ybJrDb7W3Ob8RwxftP0Yy+or/NVz1O8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
21 changes: 17 additions & 4 deletions test/e2e/cmd-print.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defaultFiles: &defaultFiles
.env: |
EXAMPLE_VALUE=nv+echo://something/or/other
STATIC_VALUE=this did not use a resolver
NUMBER_VALUE=3
nv.yaml: |
resolvers:
echo: echo $NV_URL_PATH
Expand All @@ -13,27 +14,39 @@ test:
files: *defaultFiles
out: |
EXAMPLE_VALUE=something/or/other
NUMBER_VALUE=3
STATIC_VALUE=this did not use a resolver
- it: can print in yaml
with:
cmd: nv print -o yaml
files: *defaultFiles
out: |
EXAMPLE_VALUE: "something/or/other"
STATIC_VALUE: "this did not use a resolver"
EXAMPLE_VALUE: something/or/other
NUMBER_VALUE: "3"
STATIC_VALUE: this did not use a resolver
- it: can print in json
with:
cmd: nv print -o json
files: *defaultFiles
out: |
{
"EXAMPLE_VALUE": "something/or/other",
"NUMBER_VALUE": "3",
"STATIC_VALUE": "this did not use a resolver"
}
- it: can print for shell eval
with:
cmd: nv print -o shell
files: *defaultFiles
out: |
export EXAMPLE_VALUE="something/or/other"
export STATIC_VALUE="this did not use a resolver"
export EXAMPLE_VALUE=something/or/other
export NUMBER_VALUE=3
export STATIC_VALUE='this did not use a resolver'
- it: can print list of names of modified env vars
with:
cmd: nv print -o keys
files: *defaultFiles
out: |
EXAMPLE_VALUE
NUMBER_VALUE
STATIC_VALUE