Skip to content

Commit

Permalink
Show Version in header and add wander --version command (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
robinovitch61 authored Jun 26, 2022
1 parent b017a60 commit 082cbd1
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ builds:
- binary: wander
env:
- CGO_ENABLED=0
ldflags: -s -w -X github.com/robinovitch61/wander/cmd.Version=v{{ .Version }} -X github.com/robinovitch61/wander/cmd.CommitSHA={{ .Commit }}
goos:
- darwin
- freebsd
Expand Down Expand Up @@ -63,4 +64,3 @@ brews:
email: [email protected]
install: |
bin.install "wander"
14 changes: 8 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
"github.com/robinovitch61/wander/internal/dev"
"github.com/robinovitch61/wander/internal/tui/components/app"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
Expand All @@ -15,6 +14,8 @@ type arg struct {
}

var (
version, sha string

// Used for flags.
cfgFile string

Expand All @@ -34,10 +35,11 @@ view jobs, allocations, tasks, logs, and more, all from the terminal
in a productivity-focused UI.`

rootCmd = &cobra.Command{
Use: "wander",
Short: "A terminal application for Nomad by HashiCorp",
Long: description,
Run: mainEntrypoint,
Use: "wander",
Short: "A terminal application for Nomad by HashiCorp",
Long: description,
Run: mainEntrypoint,
Version: getVersion(),
}
)

Expand Down Expand Up @@ -91,7 +93,7 @@ func initConfig() {
func mainEntrypoint(cmd *cobra.Command, args []string) {
nomadAddr := retrieveAssertExists(cmd, addrArg.cliLong, addrArg.config)
nomadToken := retrieveAssertExists(cmd, tokenArg.cliLong, tokenArg.config)
program := tea.NewProgram(app.InitialModel(nomadAddr, nomadToken), tea.WithAltScreen())
program := tea.NewProgram(initialModel(nomadAddr, nomadToken), tea.WithAltScreen())

dev.Debug("~STARTING UP~")
if err := program.Start(); err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/charmbracelet/wish"
bm "github.com/charmbracelet/wish/bubbletea"
"github.com/gliderlabs/ssh"
"github.com/robinovitch61/wander/internal/tui/components/app"
"github.com/spf13/cobra"
"log"
"os"
Expand Down Expand Up @@ -85,6 +84,6 @@ func generateTeaHandler(cmd *cobra.Command) func(ssh.Session) (tea.Model, []tea.
if sshCommands := s.Command(); len(sshCommands) == 1 {
nomadToken = strings.TrimSpace(sshCommands[0])
}
return app.InitialModel(nomadAddr, nomadToken), []tea.ProgramOption{tea.WithAltScreen()}
return initialModel(nomadAddr, nomadToken), []tea.ProgramOption{tea.WithAltScreen()}
}
}
19 changes: 19 additions & 0 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/charmbracelet/wish"
"github.com/gliderlabs/ssh"
"github.com/robinovitch61/wander/internal/tui/components/app"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
Expand All @@ -12,6 +13,16 @@ import (
"time"
)

var (
// Version contains the application version number. It's set via ldflags
// in the .goreleaser.yaml file when building
Version = ""

// CommitSHA contains the SHA of the commit that this application was built
// against. It's set via ldflags in the .goreleaser.yaml file when building
CommitSHA = ""
)

func retrieveAssertExists(cmd *cobra.Command, short, long string) string {
val := cmd.Flag(short).Value.String()
if val == "" {
Expand Down Expand Up @@ -40,3 +51,11 @@ func CustomLoggingMiddleware() wish.Middleware {
}
}
}

func initialModel(addr, token string) app.Model {
return app.InitialModel(Version, CommitSHA, addr, token)
}

func getVersion() string {
return Version
}
11 changes: 9 additions & 2 deletions internal/tui/components/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ type Model struct {
err error
}

func InitialModel(url, token string) Model {
func InitialModel(version, sha, url, token string) Model {
firstPage := nomad.JobsPage
initialHeader := header.New(constants.LogoString, url, "")
initialHeader := header.New(constants.LogoString, url, getVersionString(version, sha), "")

return Model{
nomadUrl: url,
Expand Down Expand Up @@ -276,3 +276,10 @@ func (m Model) currentPageViewportSaving() bool {
func (m Model) getFilterPrefix(page nomad.Page) string {
return page.GetFilterPrefix(m.jobID, m.taskName, m.allocID)
}

func getVersionString(v, s string) string {
if v == "" {
return "built from source"
}
return fmt.Sprintf("%s (%s)", v, s[:7])
}
19 changes: 4 additions & 15 deletions internal/tui/components/header/header.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
package header

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/robinovitch61/wander/internal/tui/style"
"strings"
)

type Model struct {
logo string
nomadUrl string
KeyHelp string
logo, nomadUrl, version, KeyHelp string
}

func New(logo string, nomadUrl, keyHelp string) (m Model) {
return Model{logo: logo, nomadUrl: nomadUrl, KeyHelp: keyHelp}
}

func (m Model) Init() tea.Cmd {
return nil
}

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, nil
func New(logo string, nomadUrl, version, keyHelp string) (m Model) {
return Model{logo: logo, nomadUrl: nomadUrl, version: version, KeyHelp: keyHelp}
}

func (m Model) View() string {
logo := style.Logo.Render(m.logo)
clusterUrl := style.ClusterUrl.Render(m.nomadUrl)
left := style.Header.Render(lipgloss.JoinVertical(lipgloss.Center, logo, clusterUrl))
left := style.Header.Render(lipgloss.JoinVertical(lipgloss.Center, logo, clusterUrl, m.version))
styledKeyHelp := style.KeyHelp.Render(m.KeyHelp)
return lipgloss.JoinHorizontal(lipgloss.Center, left, styledKeyHelp)
}
Expand Down

0 comments on commit 082cbd1

Please sign in to comment.