diff --git a/.gitignore b/.gitignore index 0edf19c..42699a7 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ dist .DS_Store mock 2sp +internal/version/version +internal/version/dirty # Used in ci for goreleaser .goreleaser-artifacts diff --git a/cmd/2sp/main.go b/cmd/2sp/main.go index e3f2b60..70d87ce 100644 --- a/cmd/2sp/main.go +++ b/cmd/2sp/main.go @@ -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 { diff --git a/internal/config/config.go b/internal/config/config.go index 357f6fb..c74f9eb 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 @@ -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(), " ") @@ -160,3 +162,5 @@ func WakuDnsDiscovery() bool { func Demo() bool { return demo } + +func Version() bool { return version } diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000..7a970cc --- /dev/null +++ b/internal/version/version.go @@ -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 + 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) +}