jpictl: refactor dump to support TOML, JSON and YAML
Signed-off-by: Alejandro Mery <amery@jpi.io>
This commit is contained in:
+53
-2
@@ -2,27 +2,78 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/burntSushi/toml"
|
"github.com/burntSushi/toml"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"git.jpi.io/amery/jpictl/pkg/zones"
|
"git.jpi.io/amery/jpictl/pkg/zones"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Encoder represents an object that encodes another internally
|
||||||
|
type Encoder interface {
|
||||||
|
Encode(any) error
|
||||||
|
}
|
||||||
|
|
||||||
|
// Encoding represents a type of [Encoder]
|
||||||
|
type Encoding int
|
||||||
|
|
||||||
|
const (
|
||||||
|
// TOMLEncoding represents TOML encoding
|
||||||
|
TOMLEncoding Encoding = iota
|
||||||
|
// JSONEncoding represents JSON encoding
|
||||||
|
JSONEncoding
|
||||||
|
// YAMLEncoding represents YAML encoding
|
||||||
|
YAMLEncoding
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewJSONEncoder returns a JSON [Encoder] to work on the given [io.Writer]
|
||||||
|
func NewJSONEncoder(w io.Writer) Encoder {
|
||||||
|
enc := json.NewEncoder(w)
|
||||||
|
enc.SetIndent(``, ` `)
|
||||||
|
return enc
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewYAMLEncoder returns a YAML [Encoder] to work on the given [io.Writer]
|
||||||
|
func NewYAMLEncoder(w io.Writer) Encoder {
|
||||||
|
enc := yaml.NewEncoder(w)
|
||||||
|
enc.SetIndent(2)
|
||||||
|
return enc
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTOMLEncoder returns a TOML [Encoder] to work on the given [io.Writer]
|
||||||
|
func NewTOMLEncoder(w io.Writer) Encoder {
|
||||||
|
enc := toml.NewEncoder(w)
|
||||||
|
return enc
|
||||||
|
}
|
||||||
|
|
||||||
|
const encoding = TOMLEncoding
|
||||||
|
|
||||||
// Command
|
// Command
|
||||||
var dumpCmd = &cobra.Command{
|
var dumpCmd = &cobra.Command{
|
||||||
Use: "dump",
|
Use: "dump",
|
||||||
Short: "generates a toml representation of the config",
|
Short: "generates a text representation of the config",
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(_ *cobra.Command, _ []string) error {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
|
var enc Encoder
|
||||||
|
|
||||||
m, err := zones.New(cfg.Base, cfg.Domain)
|
m, err := zones.New(cfg.Base, cfg.Domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
enc := toml.NewEncoder(&buf)
|
switch encoding {
|
||||||
|
case JSONEncoding:
|
||||||
|
enc = NewJSONEncoder(&buf)
|
||||||
|
case YAMLEncoding:
|
||||||
|
enc = NewYAMLEncoder(&buf)
|
||||||
|
default:
|
||||||
|
enc = NewTOMLEncoder(&buf)
|
||||||
|
}
|
||||||
|
|
||||||
if err = enc.Encode(m); err != nil {
|
if err = enc.Encode(m); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ require (
|
|||||||
github.com/spf13/cobra v1.7.0
|
github.com/spf13/cobra v1.7.0
|
||||||
golang.org/x/crypto v0.12.0
|
golang.org/x/crypto v0.12.0
|
||||||
gopkg.in/gcfg.v1 v1.2.3
|
gopkg.in/gcfg.v1 v1.2.3
|
||||||
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|||||||
@@ -90,6 +90,7 @@ golang.org/x/text v0.12.0 h1:k+n5B8goJNdU7hSvEtMUz3d1Q6D/XW4COJSJR6fN0mc=
|
|||||||
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||||
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
|
golang.org/x/tools v0.12.0 h1:YW6HUoUmYBpwSgyaGaZq1fHjrBjX1rlpZ54T6mu2kss=
|
||||||
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
|
golang.org/x/tools v0.12.0/go.mod h1:Sc0INKfu04TlqNoRA1hgpFZbhYXHPr4V5DzpSBTPqQM=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/gcfg.v1 v1.2.3 h1:m8OOJ4ccYHnx2f4gQwpno8nAX5OGOh7RLaaz0pj3Ogs=
|
gopkg.in/gcfg.v1 v1.2.3 h1:m8OOJ4ccYHnx2f4gQwpno8nAX5OGOh7RLaaz0pj3Ogs=
|
||||||
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
|
gopkg.in/gcfg.v1 v1.2.3/go.mod h1:yesOnuUOFQAhST5vPY4nbZsb/huCgGGXlipJsBn0b3o=
|
||||||
|
|||||||
Reference in New Issue
Block a user