From 4514b4421150c604a6f05ce6fe552b6a4fc1859e Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Fri, 25 Aug 2023 16:24:17 +0000 Subject: [PATCH] wireguard: implement MarshalYAML for PrivateKey and PublicKey Signed-off-by: Alejandro Mery --- pkg/wireguard/keys.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/wireguard/keys.go b/pkg/wireguard/keys.go index 6e82a90..f3585c3 100644 --- a/pkg/wireguard/keys.go +++ b/pkg/wireguard/keys.go @@ -70,6 +70,24 @@ func encodeKeyJSON(s string) ([]byte, error) { 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 func (key PrivateKey) IsZero() bool { var zero PrivateKey