Skip to content

Commit

Permalink
Feat: add envlist format for print to make output evaluable for shell (
Browse files Browse the repository at this point in the history
…#221)

Signed-off-by: Jean-Yves NOLEN <[email protected]>
  • Loading branch information
jynolen authored Dec 3, 2024
1 parent 8066eeb commit 3592d1a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 8 additions & 2 deletions cli/flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cli

import "github.com/urfave/cli"
import (
"fmt"

"github.com/urfave/cli"
)

const (
// PathEnvKey ...
Expand Down Expand Up @@ -69,6 +73,8 @@ const (
OutputFormatRaw = "raw"
// OutputFormatJSON ...
OutputFormatJSON = "json"
// OutputFormatExport
OutputFormatEnvList = "envlist"
)

var (
Expand Down Expand Up @@ -123,7 +129,7 @@ var (
}
flFormat = cli.StringFlag{
Name: FormatKey,
Usage: "Output format (options: raw, json).",
Usage: fmt.Sprintf("Output format (options: %s, %s, %s).", OutputFormatRaw, OutputFormatJSON, OutputFormatEnvList),
}
flExpand = cli.BoolFlag{
Name: ExpandKey,
Expand Down
12 changes: 11 additions & 1 deletion cli/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func printCmd(c *cli.Context) error {
format := c.String(FormatKey)
if format == "" {
format = OutputFormatRaw
} else if !(format == OutputFormatRaw || format == OutputFormatJSON) {
} else if !(format == OutputFormatRaw || format == OutputFormatJSON || format == OutputFormatEnvList) {
log.Fatalf("Invalid format: %s", format)
}

Expand All @@ -32,6 +32,8 @@ func printCmd(c *cli.Context) error {
switch format {
case OutputFormatRaw:
printRawEnvs(envSet)
case OutputFormatEnvList:
printEnvsList(envSet)
case OutputFormatJSON:
if err := printJSONEnvs(envSet); err != nil {
log.Fatalf("Failed to print env list, err: %s", err)
Expand Down Expand Up @@ -116,3 +118,11 @@ func printRawEnvs(envList models.EnvsJSONListModel) {
}
fmt.Println()
}

func printEnvsList(envList models.EnvsJSONListModel) {
fmt.Println()
for key, value := range envList {
fmt.Printf("export %s=%q\n", key, value)
}
fmt.Println()
}

0 comments on commit 3592d1a

Please sign in to comment.