Compare commits

...

8 Commits

Author SHA1 Message Date
amery 202f2e6dfc jpictl: change dump to default to YAML output
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:48:01 +00:00
amery 20484a5061 zones: change toml tags to match yaml and json output
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:46:23 +00:00
amery 45b25c63d4 jpictl: refactor dump to support TOML, JSON and YAML
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:44:05 +00:00
amery c0e2ae9bf1 zones: annotate Machine for JSON encoding
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:32:34 +00:00
amery 080021b427 zones: annotate Machine for YAML encoding
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:25:32 +00:00
amery 4514b44211 wireguard: implement MarshalYAML for PrivateKey and PublicKey
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:24:17 +00:00
amery 49b82ace71 wireguard: implement MarshalJSON for PrivateKey and PublicKey
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:12:27 +00:00
amery 2207e4a4a4 zones: fix New() to handle relative paths on hackpadfs
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-08-25 16:02:00 +00:00
6 changed files with 110 additions and 9 deletions
+53 -2
View File
@@ -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 = YAMLEncoding
// 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
} }
+1
View File
@@ -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 (
+1
View File
@@ -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=
+38
View File
@@ -5,6 +5,7 @@ import (
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"errors" "errors"
"fmt"
"golang.org/x/crypto/curve25519" "golang.org/x/crypto/curve25519"
) )
@@ -50,6 +51,43 @@ func (pub PublicKey) String() string {
} }
} }
// MarshalJSON encodes the key for JSON, omiting empty.
func (key PrivateKey) MarshalJSON() ([]byte, error) {
return encodeKeyJSON(key.String())
}
// MarshalJSON encodes the key for JSON, omiting empty.
func (pub PublicKey) MarshalJSON() ([]byte, error) {
return encodeKeyJSON(pub.String())
}
func encodeKeyJSON(s string) ([]byte, error) {
var out []byte
if s != "" {
out = []byte(fmt.Sprintf("%q", s))
}
return out, nil
}
// MarshalYAML encodes the key for YAML, omiting empty.
func (key PrivateKey) MarshalYAML() (any, error) {
return encodeKeyYAML(key.String())
}
// MarshalYAML encodes the key for YAML, omiting empty.
func (pub PublicKey) MarshalYAML() (any, error) {
return encodeKeyYAML(pub.String())
}
func encodeKeyYAML(s string) (any, error) {
if s == "" {
return nil, nil
}
return s, nil
}
// IsZero tells if the key hasn't been set // IsZero tells if the key hasn't been set
func (key PrivateKey) IsZero() bool { func (key PrivateKey) IsZero() bool {
var zero PrivateKey var zero PrivateKey
+8 -4
View File
@@ -5,16 +5,20 @@ import (
"strings" "strings"
) )
// revive:disable:line-length-limit
// A Machine is a machine on a Zone // A Machine is a machine on a Zone
type Machine struct { type Machine struct {
zone *Zone zone *Zone
ID int ID int `toml:"id"`
Name string `toml:"name"` Name string `toml:"-" json:"-" yaml:"-"`
PublicAddresses []netip.Addr `toml:"public,omitempty"` PublicAddresses []netip.Addr `toml:"public,omitempty" json:"public,omitempty" yaml:"public,omitempty"`
Rings []*RingInfo `toml:"rings,omitempty"` Rings []*RingInfo `toml:"rings,omitempty" json:"rings,omitempty" yaml:"rings,omitempty"`
} }
// revive:enable:line-length-limit
func (m *Machine) String() string { func (m *Machine) String() string {
return m.Name return m.Name
} }
+9 -3
View File
@@ -3,6 +3,7 @@ package zones
import ( import (
"io/fs" "io/fs"
"path/filepath"
"github.com/hack-pad/hackpadfs/os" "github.com/hack-pad/hackpadfs/os"
@@ -13,8 +14,8 @@ import (
type Zone struct { type Zone struct {
zones *Zones zones *Zones
ID int ID int `toml:"id"`
Name string Name string `toml:"name"`
Machines []*Machine `toml:"machines"` Machines []*Machine `toml:"machines"`
} }
@@ -105,7 +106,12 @@ func NewFS(dir fs.FS, domain string) (*Zones, error) {
// New builds a [Zones] tree using the given directory // New builds a [Zones] tree using the given directory
func New(dir, domain string) (*Zones, error) { func New(dir, domain string) (*Zones, error) {
base, err := os.NewFS().Sub(dir) dir, err := filepath.Abs(dir)
if err != nil {
return nil, err
}
base, err := os.NewFS().Sub(dir[1:])
if err != nil { if err != nil {
return nil, err return nil, err
} }