git » gofer » commit 48496ca

config: Simplify String conversion

author Alberto Bertogli
2023-01-28 21:39:58 UTC
committer Alberto Bertogli
2023-01-30 10:48:29 UTC
parent af33cb2a6581f8d8445a14bdfe739168709c16e1

config: Simplify String conversion

The conversion from a parsed config to a string is always valid (and
there's an internal handler just in case it isn't), so remove some
unnecessary complexity when printing the parsed config.

config/config.go +2 -7
debug/debug.go +1 -7
gofer.go +1 -5

diff --git a/config/config.go b/config/config.go
index b4498cf..be481c0 100644
--- a/config/config.go
+++ b/config/config.go
@@ -73,16 +73,11 @@ type ReqLog struct {
 }
 
 func (c Config) String() string {
-	s, err := c.ToString()
+	d, err := yaml.Marshal(&c)
 	if err != nil {
 		return fmt.Sprintf("<error: %v>", err)
 	}
-	return s
-}
-
-func (c Config) ToString() (string, error) {
-	d, err := yaml.Marshal(&c)
-	return string(d), err
+	return string(d)
 }
 
 func (c Config) Check() []error {
diff --git a/debug/debug.go b/debug/debug.go
index 6c072b1..546ca9f 100644
--- a/debug/debug.go
+++ b/debug/debug.go
@@ -113,14 +113,8 @@ func ServeDebugging(addr string, conf *config.Config) error {
 
 func DumpConfigFunc(conf *config.Config) http.HandlerFunc {
 	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-		s, err := conf.ToString()
-		if err != nil {
-			http.Error(w, err.Error(), http.StatusInternalServerError)
-			return
-		}
-
 		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
-		w.Write([]byte(s))
+		w.Write([]byte(conf.String()))
 	})
 }
 
diff --git a/gofer.go b/gofer.go
index e065ad6..2e9e37a 100644
--- a/gofer.go
+++ b/gofer.go
@@ -41,11 +41,7 @@ func main() {
 		log.Fatalf("invalid configuration")
 	}
 	if *configPrint {
-		s, err := conf.ToString()
-		if err != nil {
-			log.Fatalf("%v", err)
-		}
-		fmt.Print(s)
+		fmt.Print(conf.String())
 		return
 	}
 	if *configCheck {