Преглед на файлове

:gear: Add methods to generate YAML/Base64 directly

Ettore Di Giacinto преди 3 години
родител
ревизия
0bb49d7839
променени са 2 файла, в които са добавени 14 реда и са изтрити 10 реда
  1. 2 10
      cmd/main.go
  2. 12 0
      pkg/edgevpn/options.go

+ 2 - 10
cmd/main.go

@@ -17,7 +17,6 @@ package cmd
 
 import (
 	"context"
-	"encoding/base64"
 	"fmt"
 	"os"
 	"time"
@@ -25,7 +24,6 @@ import (
 	"github.com/mudler/edgevpn/api"
 	"github.com/mudler/edgevpn/pkg/edgevpn"
 	"github.com/urfave/cli"
-	"gopkg.in/yaml.v2"
 )
 
 const Copyright string = `	edgevpn  Copyright (C) 2021 Ettore Di Giacinto
@@ -77,16 +75,10 @@ func Main() func(c *cli.Context) error {
 		if c.Bool("g") {
 			// Generates a new config and exit
 			newData := edgevpn.GenerateNewConnectionData()
-			bytesData, err := yaml.Marshal(newData)
-			if err != nil {
-				fmt.Println(err)
-				os.Exit(1)
-			}
-
 			if c.Bool("b") {
-				fmt.Print(base64.StdEncoding.EncodeToString(bytesData))
+				fmt.Print(newData.Base64())
 			} else {
-				fmt.Println(string(bytesData))
+				fmt.Println(newData.YAML())
 			}
 
 			os.Exit(0)

+ 12 - 0
pkg/edgevpn/options.go

@@ -281,6 +281,18 @@ type YAMLConnectionConfig struct {
 	MaxMessageSize int    `yaml:"max_message_size"`
 }
 
+// Base64 returns the base64 string representation of the connection
+func (y YAMLConnectionConfig) Base64() string {
+	bytesData, _ := yaml.Marshal(y)
+	return base64.StdEncoding.EncodeToString(bytesData)
+}
+
+// YAML returns the connection config as yaml string
+func (y YAMLConnectionConfig) YAML() string {
+	bytesData, _ := yaml.Marshal(y)
+	return string(bytesData)
+}
+
 func (y YAMLConnectionConfig) copy(mdns, dht bool, cfg *Config) {
 	d := &discovery.DHT{
 		RefreshDiscoveryTime: cfg.DiscoveryInterval,