Skip to content

Commit

Permalink
feat: version flag
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Jul 10, 2024
1 parent 95b731e commit b65277a
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 70 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ dist
.DS_Store
mock
2sp
internal/version/version
internal/version/dirty

# Used in ci for goreleaser
.goreleaser-artifacts
125 changes: 55 additions & 70 deletions cmd/2sp/main.go
Original file line number Diff line number Diff line change
@@ -1,91 +1,76 @@
package main

import (
"encoding/json"
"context"
"fmt"
"runtime/debug"
"os"

"github.com/carlmjohnson/versioninfo"
tea "github.com/charmbracelet/bubbletea"
"github.com/jonboulle/clockwork"
"go.uber.org/zap"

"github.com/six78/2-story-points-cli/cmd/2sp/demo"
"github.com/six78/2-story-points-cli/internal/config"
"github.com/six78/2-story-points-cli/internal/transport"
"github.com/six78/2-story-points-cli/internal/version"
"github.com/six78/2-story-points-cli/internal/view"
"github.com/six78/2-story-points-cli/pkg/game"
"github.com/six78/2-story-points-cli/pkg/storage"
)

func main() {
config.ParseArguments()
config.SetupLogger()

info, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("No build info")
} else {
infoJson, err := json.MarshalIndent(info, "", "\t")
if err != nil {
fmt.Println("Error marshalling build info:", err)
} else {
fmt.Println(string(infoJson))
}
if config.Version() {
fmt.Printf("2-story-points version: %s\n", version.Version())
return
}

fmt.Print("\n----------\n")
ctx, quit := context.WithCancel(context.Background())
defer quit()

short := versioninfo.Short()
fmt.Println(short)
waku := transport.NewNode(ctx, config.Logger)
defer waku.Stop()

fmt.Println("Version:", versioninfo.Version)
fmt.Println("Revision:", versioninfo.Revision)
fmt.Println("DirtyBuild:", versioninfo.DirtyBuild)
fmt.Println("LastCommit:", versioninfo.LastCommit)
options := []game.Option{
game.WithContext(ctx),
game.WithTransport(waku),
game.WithStorage(createStorage()),
game.WithLogger(config.Logger.Named("game")),
game.WithPlayerName(config.PlayerName()),
game.WithOnlineMessagePeriod(config.OnlineMessagePeriod),
game.WithStateMessagePeriod(config.StateMessagePeriod),
game.WithEnableSymmetricEncryption(config.EnableSymmetricEncryption),
game.WithClock(clockwork.NewRealClock()),
}

game := game.NewGame(options)
if game == nil {
config.Logger.Fatal("could not create game")
}
defer game.Stop()

// Create UI model and program
model := view.InitialModel(game, waku)
program := tea.NewProgram(model)

//program.Println("ok: %w", ok)
// Run demo if enabled
if config.Demo() {
demonstration := demo.New(ctx, game, program)
go func() {
demonstration.Routine()
program.Quit()
}()
}

if _, err := program.Run(); err != nil {
config.Logger.Error("error running program", zap.Error(err))
os.Exit(1)
return
}

//
//config.ParseArguments()
//config.SetupLogger()
//
//ctx, quit := context.WithCancel(context.Background())
//defer quit()
//
//waku := transport.NewNode(ctx, config.Logger)
//defer waku.Stop()
//
//options := []game.Option{
// game.WithContext(ctx),
// game.WithTransport(waku),
// game.WithStorage(createStorage()),
// game.WithLogger(config.Logger.Named("game")),
// game.WithPlayerName(config.PlayerName()),
// game.WithOnlineMessagePeriod(config.OnlineMessagePeriod),
// game.WithStateMessagePeriod(config.StateMessagePeriod),
// game.WithEnableSymmetricEncryption(config.EnableSymmetricEncryption),
// game.WithClock(clockwork.NewRealClock()),
//}
//
//game := game.NewGame(options)
//if game == nil {
// config.Logger.Fatal("could not create game")
//}
//defer game.Stop()
//
//// Create UI model and program
//model := view.InitialModel(game, waku)
//program := tea.NewProgram(model)
//
//
//// Run demo if enabled
//if config.Demo() {
// demonstration := demo.New(ctx, game, program)
// go func() {
// demonstration.Routine()
// program.Quit()
// }()
//}
//
//if _, err := program.Run(); err != nil {
// config.Logger.Error("error running program", zap.Error(err))
// os.Exit(1)
// return
//}
//
//os.Exit(0)
os.Exit(0)
}

func createStorage() storage.Service {
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var wakuLightMode bool
var wakuDiscV5 bool
var wakuDnsDiscovery bool
var demo bool
var version bool

var Logger *zap.Logger
var LogFilePath string
Expand Down Expand Up @@ -108,6 +109,7 @@ func ParseArguments() {
flag.BoolVar(&wakuDiscV5, "waku.discv5", true, "Enable DiscV5 discovery")
flag.BoolVar(&wakuDnsDiscovery, "waku.dnsdiscovery", true, "Enable DNS discovery")
flag.BoolVar(&demo, "demo", false, "Run demo and quit")
flag.BoolVar(&version, "version", false, "Print version and quit")
flag.Parse()

initialAction = strings.Join(flag.Args(), " ")
Expand Down Expand Up @@ -160,3 +162,5 @@ func WakuDnsDiscovery() bool {
func Demo() bool {
return demo
}

func Version() bool { return version }
48 changes: 48 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package version

import (
_ "embed"
"fmt"
"runtime/debug"
)

//go:generate sh -c "printf %s $(git describe --tags) > version"
//go:generate sh -c "git diff-index --quiet HEAD || echo -n dirty > dirty"

var (
//go:embed version
tag string

//go:embed dirty

Check failure on line 16 in internal/version/version.go

View workflow job for this annotation

GitHub Actions / lint

pattern dirty: no matching files found (typecheck)

Check failure on line 16 in internal/version/version.go

View workflow job for this annotation

GitHub Actions / test

pattern dirty: no matching files found

Check failure on line 16 in internal/version/version.go

View workflow job for this annotation

GitHub Actions / test

pattern dirty: no matching files found
dirty string

buildInfo string
)

func Version() string {
v := tag
if dirty != "" {
v += "-dirty"
}
return fmt.Sprintf("%s %s", v, buildInfo)
}

func init() {
info, ok := debug.ReadBuildInfo()
if !ok {
fmt.Println("No build info")
return
}

var goos, goarch string
for _, s := range info.Settings {
switch s.Key {
case "GOOS":
goos = s.Value
case "GOARCH":
goarch = s.Value
}
}

buildInfo = fmt.Sprintf("%s/%s", goos, goarch)
}

0 comments on commit b65277a

Please sign in to comment.