Skip to content

Commit

Permalink
adding debug logging toggle
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Collins <[email protected]>
  • Loading branch information
clcollins committed Nov 2, 2023
1 parent ebc898e commit ab82e07
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
10 changes: 7 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,22 @@ but rather a simple tool to make on-call tasks easier.`,
}

var ctx = context.Background()
var config = &pd.Config{}
var tuiConfig = &tui.Config{
Debug: Debug,
Verbose: false,
}
var pdConfig = &pd.Config{}

token := viper.GetString("token")
teams := viper.GetStringSlice("teams")
silentuser := viper.GetString("silentuser")

err := config.PopulateConfig(ctx, token, teams, silentuser)
err := pdConfig.PopulateConfig(ctx, token, teams, silentuser)
if err != nil {
log.Fatal(err)
}

p := tea.NewProgram(tui.InitialModel(ctx, config))
p := tea.NewProgram(tui.InitialModel(ctx, tuiConfig, pdConfig))
_, err = p.Run()
if err != nil {
log.Fatal(err)
Expand Down
11 changes: 4 additions & 7 deletions pkg/tui/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ package tui

import (
"context"
"log"

"github.com/PagerDuty/go-pagerduty"
tea "github.com/charmbracelet/bubbletea"
"github.com/clcollins/srepd/pkg/pd"
)

var logLevel = "warn"

type getCurrentUserMsg string
type gotCurrentUserMsg *pagerduty.User

func getCurrentUser(ctx context.Context, pdConfig *pd.Config) tea.Cmd {
if debug {
log.Printf("getCurrentUser")
}
debug("getCurrentUser")
return func() tea.Msg {
u, err := pdConfig.Client.GetCurrentUserWithContext(ctx, pagerduty.GetCurrentUserOptions{})
if err != nil {
Expand Down Expand Up @@ -51,9 +50,7 @@ type getIncidentsMsg string
type gotIncidentsMsg []pagerduty.Incident

func getIncidents(ctx context.Context, pdConfig *pd.Config) tea.Cmd {
if debug {
log.Printf("getIncidents")
}
debug("getIncidents")
return func() tea.Msg {
opts := pagerduty.ListIncidentsOptions{
TeamIDs: pdConfig.DefaultListOpts.TeamIDs,
Expand Down
2 changes: 2 additions & 0 deletions pkg/tui/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/charmbracelet/lipgloss"
)

// TODO: Figure out how to dynamically resize the table and columns based on terminal size
// and resize tea.Msg events received from the terminal
const (
// Standard terminal size is 80x24
// so we have 78 columns to work with after subtracting for the table borders
Expand Down
34 changes: 23 additions & 11 deletions pkg/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ const (
gettingSilentUserMsg = "getting 'Silent' user info..."
)

var debug bool
type Config struct {
Debug bool
Verbose bool
}

var errorLog []error

// Type and function for capturing error messages with tea.Msg
Expand All @@ -32,8 +36,9 @@ func (e errMsg) Error() string { return e.err.Error() }

type model struct {
help help.Model
context context.Context
cliConfig *Config
pdConfig *pd.Config
context context.Context
currentUser *pagerduty.User
incidentList []pagerduty.Incident
selectedIncident *pagerduty.Incident
Expand All @@ -45,18 +50,20 @@ type model struct {
// confirm bool
}

func InitialModel(ctx context.Context, pdConfig *pd.Config) model {
func InitialModel(ctx context.Context, config *Config, pdConfig *pd.Config) model {
return model{
help: help.New(),
context: ctx,
pdConfig: pdConfig,
help: help.New(),
context: ctx,
cliConfig: config,
pdConfig: pdConfig,
}
}

func (m model) Init() tea.Cmd {
if debug {
log.Print("init")
if m.cliConfig.Debug {
logLevel = "debug"
}

return tea.Batch(
func() tea.Msg { return tea.ClearScreen() },
func() tea.Msg { return createTableWithStylesMsg("create table") },
Expand All @@ -68,9 +75,8 @@ func (m model) Init() tea.Cmd {
}

func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if debug {
log.Print("Update: ", msg)
}
debug(fmt.Sprintf("Update: %s", msg))

switch msg := msg.(type) {

case tea.WindowSizeMsg:
Expand Down Expand Up @@ -227,3 +233,9 @@ func (m model) View() string {
}
return m.renderIncidentTable()
}

func debug(s string) {
if logLevel == "debug" {
log.Print(s)
}
}
2 changes: 1 addition & 1 deletion pkg/tui/views.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (m model) renderIncidentTable() string {
}

//s.WriteString(renderAssigneeArea(assignedTo))
//s.WriteString(renderStatusArea(m.statusMessage))
s.WriteString(renderStatusArea(m.statusMessage))
s.WriteString(renderTableArea(m.table))
//s.WriteString(renderHelpArea(m.help.View(defaultKeyMap)))

Expand Down

0 comments on commit ab82e07

Please sign in to comment.