From 31ab737bf26deb9315ffedf99a8bb7d43f620700 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Fri, 28 Jun 2019 19:28:28 -0600 Subject: Refactor code related to getting current version And set version in CertMagic for User-Agent purposes --- cmd/commands.go | 11 +++++++++-- cmd/main.go | 21 --------------------- 2 files changed, 9 insertions(+), 23 deletions(-) (limited to 'cmd') diff --git a/cmd/commands.go b/cmd/commands.go index 4745af7..3f866d1 100644 --- a/cmd/commands.go +++ b/cmd/commands.go @@ -10,8 +10,10 @@ import ( "os" "os/exec" "path/filepath" + "strings" "github.com/caddyserver/caddy" + "github.com/mholt/certmagic" "github.com/mitchellh/go-ps" ) @@ -126,6 +128,11 @@ func cmdRun() (int, error) { } } + // set a fitting User-Agent for ACME requests + goModule := caddy.GoModule() + cleanModVersion := strings.TrimPrefix(goModule.Version, "v") + certmagic.UserAgent = "Caddy/" + cleanModVersion + // start the admin endpoint along with any initial config err := caddy.StartAdmin(config) if err != nil { @@ -180,10 +187,10 @@ func cmdStop() (int, error) { } func cmdVersion() (int, error) { - goModule := getGoBuildModule() + goModule := caddy.GoModule() if goModule.Sum != "" { // a build with a known version will also have a checksum - fmt.Printf("%s (%s)\n", goModule.Version, goModule.Sum) + fmt.Printf("%s %s\n", goModule.Version, goModule.Sum) } else { fmt.Println(goModule.Version) } diff --git a/cmd/main.go b/cmd/main.go index b9baf84..9cd2ae0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -9,7 +9,6 @@ import ( "log" "net" "os" - "runtime/debug" ) // Main executes the main function of the caddy command. @@ -66,23 +65,3 @@ func handlePingbackConn(conn net.Conn, expect []byte) error { } return nil } - -// getGoBuildModule returns the build info of Caddy -// from debug.BuildInfo (requires Go modules). If -// no version information is available, a non-nil -// value will still be returned, but with an -// unknown version. -func getGoBuildModule() *debug.Module { - bi, ok := debug.ReadBuildInfo() - if ok { - // The recommended way to build Caddy involves - // creating a separate main module, which - // TODO: track related Go issue: https://github.com/golang/go/issues/29228 - for _, mod := range bi.Deps { - if mod.Path == "github.com/mholt/caddy" { - return mod - } - } - } - return &debug.Module{Version: "unknown"} -} -- cgit v1.2.3