mirror of
https://github.com/caddyserver/xcaddy.git
synced 2024-10-07 11:40:11 +01:00
cmd: Add xcaddy version
command
cmd: Fix tests
This commit is contained in:
parent
b8a0d46648
commit
82e7b6aff2
2 changed files with 48 additions and 3 deletions
|
@ -12,7 +12,7 @@
|
|||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package main
|
||||
package xcaddycmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
@ -24,6 +24,7 @@ import (
|
|||
"path"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"strings"
|
||||
|
||||
"github.com/caddyserver/xcaddy"
|
||||
|
@ -37,7 +38,7 @@ var (
|
|||
buildDebugOutput = os.Getenv("XCADDY_DEBUG") == "1"
|
||||
)
|
||||
|
||||
func main() {
|
||||
func Main() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go trapSignals(ctx, cancel)
|
||||
|
@ -49,6 +50,11 @@ func main() {
|
|||
return
|
||||
}
|
||||
|
||||
if len(os.Args) > 1 && os.Args[1] == "version" {
|
||||
fmt.Println(xcaddyVersion())
|
||||
return
|
||||
}
|
||||
|
||||
if err := runDev(ctx, os.Args[1:]); err != nil {
|
||||
log.Fatalf("[ERROR] %v", err)
|
||||
}
|
||||
|
@ -319,3 +325,42 @@ func splitWith(arg string) (module, version, replace string, err error) {
|
|||
|
||||
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
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package main
|
||||
package xcaddycmd
|
||||
|
||||
import (
|
||||
"runtime"
|
Loading…
Reference in a new issue