@ -16,7 +16,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
version semver.Version
|
version Version
|
||||||
versionFlag bool
|
versionFlag bool
|
||||||
versionCmd = &cobra.Command{
|
versionCmd = &cobra.Command{
|
||||||
Use: versionName,
|
Use: versionName,
|
||||||
@ -25,11 +25,24 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Version struct {
|
||||||
|
Full string
|
||||||
|
Branch string
|
||||||
|
Tag string
|
||||||
|
Commit string
|
||||||
|
CommitDate string
|
||||||
|
BuildDate string
|
||||||
|
Major string
|
||||||
|
Minor string
|
||||||
|
Patch string
|
||||||
|
PreRelease string
|
||||||
|
}
|
||||||
|
|
||||||
func addVersionFlag(cmd *cobra.Command) {
|
func addVersionFlag(cmd *cobra.Command) {
|
||||||
cmd.PersistentFlags().BoolVarP(&versionFlag, versionName, versionShortHand, false, versionUsage)
|
cmd.PersistentFlags().BoolVarP(&versionFlag, versionName, versionShortHand, false, versionUsage)
|
||||||
}
|
}
|
||||||
|
|
||||||
func configureVersionFlag(cmd *cobra.Command, v semver.Version) {
|
func configureVersionFlag(cmd *cobra.Command, v Version) {
|
||||||
version = v
|
version = v
|
||||||
cmd.AddCommand(versionCmd)
|
cmd.AddCommand(versionCmd)
|
||||||
addVersionFlag(cmd)
|
addVersionFlag(cmd)
|
||||||
@ -50,19 +63,28 @@ func printVersion(v semver.Version) string {
|
|||||||
if output != "" {
|
if output != "" {
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"Full: %s\nVersion: %s\nChannel: %s\nCommit: %s\nDate: %s",
|
"Full: %s\nBranch: %s\nTag: %s\nCommit: %s\nCommit date: %s\nBuild date: %s\nMajor: %s\nMinor: %s\nPatch: %s\nPreRelease: %s\n",
|
||||||
v.String(),
|
version.Full,
|
||||||
v.Number(),
|
version.Branch,
|
||||||
v.Release(),
|
version.Tag,
|
||||||
v.Commit(),
|
version.Commit,
|
||||||
v.Date(),
|
version.CommitDate,
|
||||||
|
version.BuildDate,
|
||||||
|
version.Major,
|
||||||
|
version.Minor,
|
||||||
|
version.Patch,
|
||||||
|
version.PreRelease,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func versionRunFuncE(cmd *cobra.Command, args []string) error {
|
func versionRunFuncE(cmd *cobra.Command, args []string) error {
|
||||||
if _, err := fmt.Fprintln(outWriter, printVersion(version)); err != nil {
|
var v semver.Version
|
||||||
|
var err error
|
||||||
|
if v, err = semver.Parse(version.Full); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err = fmt.Fprintln(outWriter, printVersion(v)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -2,47 +2,82 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"git.flexabyte.io/flexabyte/go-slogd/slogd"
|
|
||||||
|
|
||||||
"git.flexabyte.io/flexabyte/go-kit/application"
|
"git.flexabyte.io/flexabyte/go-kit/application"
|
||||||
|
"git.flexabyte.io/flexabyte/go-kit/httpd"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
version string = "0.1.0-alpha.0+metadata.20101112"
|
||||||
|
branch string = "0.1.0-dev"
|
||||||
|
tag string = "0.1.0-dev.0"
|
||||||
|
commit string = "aabbccddee"
|
||||||
|
commitDate string = time.Now().String()
|
||||||
|
buildDate string = time.Now().String()
|
||||||
|
|
||||||
|
major string = "0"
|
||||||
|
minor string = "1"
|
||||||
|
patch string = "0"
|
||||||
|
prerelease string = "dev"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var err error
|
var err error
|
||||||
slogd.Init(slogd.LevelTrace, true)
|
slogd.Init(application.GetLogLevelFromArgs(os.Args), false)
|
||||||
|
slogd.RegisterSink(slogd.HandlerText, slog.NewTextHandler(os.Stdout, slogd.HandlerOptions()), true)
|
||||||
|
ctx := slogd.WithContext(context.Background())
|
||||||
|
|
||||||
config := application.Config{
|
config := application.Config{
|
||||||
Name: "main",
|
Name: "main",
|
||||||
Title: "Main Test",
|
Title: "Main Test",
|
||||||
Banner: "",
|
Banner: "",
|
||||||
Version: "0.1.0-alpha.0+metadata.20101112",
|
// Version: "0.1.0-alpha.0+metadata.20101112",
|
||||||
|
Version: application.Version{
|
||||||
|
Full: version,
|
||||||
|
Branch: branch,
|
||||||
|
Tag: tag,
|
||||||
|
Commit: commit,
|
||||||
|
CommitDate: commitDate,
|
||||||
|
BuildDate: buildDate,
|
||||||
|
Major: major,
|
||||||
|
Minor: minor,
|
||||||
|
Patch: patch,
|
||||||
|
PreRelease: prerelease,
|
||||||
|
},
|
||||||
EnableGracefulShutdown: true,
|
EnableGracefulShutdown: true,
|
||||||
|
Logger: slogd.Logger(),
|
||||||
OverrideRunE: func(cmd *cobra.Command, args []string) error {
|
OverrideRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
fmt.Println("overrideRunE")
|
// fmt.Println("overrideRunE")
|
||||||
time.Sleep(5 * time.Second)
|
// time.Sleep(1 * time.Second)
|
||||||
fmt.Println("overrideRunE done")
|
// fmt.Println("overrideRunE done")
|
||||||
return nil
|
mux := http.NewServeMux()
|
||||||
|
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
slogd.FromContext(r.Context()).LogAttrs(r.Context(), slogd.LevelInfo, "request received", slog.String("method", r.Method), slog.String("url", r.URL.String()), slog.String("user-agent", r.UserAgent()))
|
||||||
|
})
|
||||||
|
return httpd.RunHttpServer(cmd.Context(), slogd.FromContext(cmd.Context()), "127.0.0.1", 28000, mux, 1*time.Second)
|
||||||
},
|
},
|
||||||
PersistentPreRunE: nil,
|
PersistentPreRunE: nil,
|
||||||
PersistentPostRunE: nil,
|
PersistentPostRunE: nil,
|
||||||
// ShutdownSignals: []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT},
|
ShutdownSignals: []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT},
|
||||||
ShutdownTimeout: 1 * time.Second,
|
ShutdownTimeout: 5 * time.Second,
|
||||||
SubCommands: nil,
|
SubCommands: nil,
|
||||||
SubCommandInitializeFunc: nil,
|
SubCommandInitializeFunc: nil,
|
||||||
ValidArgs: nil,
|
ValidArgs: nil,
|
||||||
}
|
}
|
||||||
|
|
||||||
var app application.Application
|
var app application.Application
|
||||||
if app, err = application.New(config); err != nil {
|
if app, err = application.New(config); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = app.Start(context.Background()); err != nil {
|
if err = app.ExecuteContext(ctx); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user