cmd: Add xcaddy version command

cmd: Fix tests
This commit is contained in:
Francis Lavoie 2021-08-26 20:23:24 -04:00 committed by Mohammed Al Sahaf
parent b8a0d46648
commit 82e7b6aff2
2 changed files with 48 additions and 3 deletions

View file

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package main package xcaddycmd
import ( import (
"context" "context"
@ -24,6 +24,7 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"runtime/debug"
"strings" "strings"
"github.com/caddyserver/xcaddy" "github.com/caddyserver/xcaddy"
@ -37,7 +38,7 @@ var (
buildDebugOutput = os.Getenv("XCADDY_DEBUG") == "1" buildDebugOutput = os.Getenv("XCADDY_DEBUG") == "1"
) )
func main() { func Main() {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
go trapSignals(ctx, cancel) go trapSignals(ctx, cancel)
@ -49,6 +50,11 @@ func main() {
return return
} }
if len(os.Args) > 1 && os.Args[1] == "version" {
fmt.Println(xcaddyVersion())
return
}
if err := runDev(ctx, os.Args[1:]); err != nil { if err := runDev(ctx, os.Args[1:]); err != nil {
log.Fatalf("[ERROR] %v", err) log.Fatalf("[ERROR] %v", err)
} }
@ -319,3 +325,42 @@ func splitWith(arg string) (module, version, replace string, err error) {
return return
} }
// xcaddyVersion returns a detailed version string, if available.
func xcaddyVersion() string {
mod := goModule()
ver := mod.Version
if mod.Sum != "" {
ver += " " + mod.Sum
}
if mod.Replace != nil {
ver += " => " + mod.Replace.Path
if mod.Replace.Version != "" {
ver += "@" + mod.Replace.Version
}
if mod.Replace.Sum != "" {
ver += " " + mod.Replace.Sum
}
}
return ver
}
func goModule() *debug.Module {
mod := &debug.Module{}
mod.Version = "unknown"
bi, ok := debug.ReadBuildInfo()
if ok {
mod.Path = bi.Main.Path
// The recommended way to build xcaddy involves
// creating a separate main module, which
// TODO: track related Go issue: https://github.com/golang/go/issues/29228
// once that issue is fixed, we should just be able to use bi.Main... hopefully.
for _, dep := range bi.Deps {
if dep.Path == "github.com/caddyserver/xcaddy" {
return dep
}
}
return &bi.Main
}
return mod
}

View file

@ -1,4 +1,4 @@
package main package xcaddycmd
import ( import (
"runtime" "runtime"