-
Notifications
You must be signed in to change notification settings - Fork 217
/
root.go
146 lines (127 loc) · 3.35 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
slog "log"
"os"
"runtime"
"runtime/debug"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/muesli/termenv"
"github.com/spf13/cobra"
"github.com/dlvhdr/gh-dash/v4/config"
"github.com/dlvhdr/gh-dash/v4/ui"
"github.com/dlvhdr/gh-dash/v4/ui/markdown"
)
var (
Version = "dev"
Commit = ""
Date = ""
BuiltBy = ""
)
var (
cfgFile string
rootCmd = &cobra.Command{
Use: "gh dash",
Short: "A gh extension that shows a configurable dashboard of pull requests and issues.",
Version: "",
Args: cobra.MaximumNArgs(1),
}
)
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}
func createModel(repoPath *string, configPath string, debug bool) (ui.Model, *os.File) {
var loggerFile *os.File
if debug {
var fileErr error
newConfigFile, fileErr := os.OpenFile("debug.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if fileErr == nil {
log.SetOutput(newConfigFile)
log.SetTimeFormat(time.Kitchen)
log.SetReportCaller(true)
log.SetLevel(log.DebugLevel)
log.Debug("Logging to debug.log")
} else {
loggerFile, _ = tea.LogToFile("debug.log", "debug")
slog.Print("Failed setting up logging", fileErr)
}
}
return ui.NewModel(repoPath, configPath), loggerFile
}
func buildVersion(version, commit, date, builtBy string) string {
result := version
if commit != "" {
result = fmt.Sprintf("%s\ncommit: %s", result, commit)
}
if date != "" {
result = fmt.Sprintf("%s\nbuilt at: %s", result, date)
}
if builtBy != "" {
result = fmt.Sprintf("%s\nbuilt by: %s", result, builtBy)
}
result = fmt.Sprintf("%s\ngoos: %s\ngoarch: %s", result, runtime.GOOS, runtime.GOARCH)
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
result = fmt.Sprintf("%s\nmodule version: %s, checksum: %s", result, info.Main.Version, info.Main.Sum)
}
return result
}
func init() {
rootCmd.PersistentFlags().StringVarP(
&cfgFile,
"config",
"c",
"",
"use this configuration file (default is $GH_DASH_CONFIG, or if not set, \n$XDG_CONFIG_HOME/gh-dash/config.yml)",
)
err := rootCmd.MarkPersistentFlagFilename("config", "yaml", "yml")
if err != nil {
log.Fatal("Cannot mark config flag as filename", err)
}
rootCmd.Version = buildVersion(Version, Commit, Date, BuiltBy)
rootCmd.SetVersionTemplate(`gh-dash {{printf "version %s\n" .Version}}`)
rootCmd.Flags().Bool(
"debug",
false,
"passing this flag will allow writing debug output to debug.log",
)
rootCmd.Flags().BoolP(
"help",
"h",
false,
"help for gh-dash",
)
rootCmd.Run = func(_ *cobra.Command, args []string) {
var repo *string
repos := config.IsFeatureEnabled(config.FF_REPO_VIEW)
if repos && len(args) > 0 {
repo = &args[0]
}
debug, err := rootCmd.Flags().GetBool("debug")
if err != nil {
log.Fatal("Cannot parse debug flag", err)
}
// see https://github.com/charmbracelet/lipgloss/issues/73
lipgloss.SetHasDarkBackground(termenv.HasDarkBackground())
markdown.InitializeMarkdownStyle(termenv.HasDarkBackground())
model, logger := createModel(repo, cfgFile, debug)
if logger != nil {
defer logger.Close()
}
p := tea.NewProgram(
model,
tea.WithAltScreen(),
)
if _, err := p.Run(); err != nil {
log.Fatal("Failed starting the TUI", err)
}
}
}