-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
210 lines (187 loc) · 6 KB
/
Copy pathmain.go
File metadata and controls
210 lines (187 loc) · 6 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package main
import (
"fmt"
"os"
"os/exec"
tea "charm.land/bubbletea/v2"
"github.com/alecthomas/kong"
toml "github.com/pelletier/go-toml/v2"
"github.com/lazynop/lazyenv/internal/config"
"github.com/lazynop/lazyenv/internal/tui"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
var cli struct {
Path string `arg:"" optional:"" default:"." help:"Directory to scan." type:"existingdir"`
Recursive *bool `short:"r" help:"Scan subdirectories recursively."`
ShowAll *bool `short:"a" name:"show-all" help:"Show secrets in cleartext at startup."`
NoGitCheck *bool `short:"G" name:"no-git-check" help:"Disable .gitignore check."`
NoBackup *bool `short:"B" name:"no-backup" help:"Disable .bak backup before first save."`
Theme *string `name:"theme" help:"Built-in theme preset (see --list-themes). Overrides theme setting in config file."`
NoThemeBg *bool `name:"no-theme-bg" help:"Disable theme background color."`
NoMouse *bool `name:"no-mouse" help:"Disable mouse support."`
ReadOnly *bool `name:"read-only" help:"Disable all write operations."`
SessionSummary *bool `name:"session-summary" negatable:"" help:"Print a session summary on exit (default on). Use --no-session-summary to disable."`
Sort *string `short:"s" name:"sort" help:"Sort order: position or alphabetical." enum:"position,alphabetical"`
Group *bool `short:"g" name:"group" negatable:"" help:"Start with prefix grouping enabled (default off). Use --no-group to disable."`
FileListWidth *int `name:"file-list-width" help:"Width of the file list panel (0=auto)."`
Config string `short:"c" name:"config" help:"Path to configuration file." type:"existingfile"`
CheckConfig bool `name:"check-config" help:"Validate configuration file and exit."`
ShowConfig bool `name:"show-config" help:"Show effective configuration and exit."`
ListThemes bool `name:"list-themes" help:"List available built-in themes and exit."`
Themes bool `name:"themes" help:"Interactive theme preview."`
Version kong.VersionFlag `short:"v" help:"Show version."`
}
func applyCLIOverrides(cfg *config.Config) []string {
var warns []string
if cli.Recursive != nil {
cfg.Recursive = *cli.Recursive
}
if cli.ShowAll != nil {
cfg.ShowAll = *cli.ShowAll
}
if cli.NoBackup != nil {
cfg.NoBackup = *cli.NoBackup
}
if cli.Theme != nil {
cfg.Theme = *cli.Theme
if cfg.Theme != "" {
if _, ok := config.LookupTheme(cfg.Theme); !ok {
warns = append(warns, fmt.Sprintf("unknown theme %q (see --list-themes)", cfg.Theme))
}
}
}
if cli.NoThemeBg != nil {
cfg.NoThemeBg = *cli.NoThemeBg
}
if cli.NoMouse != nil {
cfg.NoMouse = *cli.NoMouse
}
if cli.ReadOnly != nil {
cfg.ReadOnly = *cli.ReadOnly
}
applySessionSummaryOverride(cfg)
warns = append(warns, applyFileListWidthOverride(cfg)...)
if cli.Sort != nil {
cfg.Sort = *cli.Sort
}
if cli.Group != nil {
cfg.Group = *cli.Group
}
if cli.NoGitCheck != nil {
cfg.NoGitCheck = *cli.NoGitCheck
} else if !cfg.NoGitCheck {
if _, err := exec.LookPath("git"); err != nil {
cfg.NoGitCheck = true
}
}
return warns
}
func applyFileListWidthOverride(cfg *config.Config) []string {
if cli.FileListWidth == nil {
return nil
}
v := *cli.FileListWidth
var warns []string
if v != 0 && v < config.FileListMinWidth {
warns = append(warns, fmt.Sprintf("--file-list-width %d is below minimum %d, using %d", v, config.FileListMinWidth, config.FileListMinWidth))
v = config.FileListMinWidth
}
cfg.Layout.FileListWidth = v
return warns
}
// applySessionSummaryOverride resolves the final SessionSummary value: CLI flag
// wins over config file, and --read-only forces it off.
func applySessionSummaryOverride(cfg *config.Config) {
if cli.SessionSummary != nil {
cfg.SessionSummary = *cli.SessionSummary
}
if cfg.ReadOnly {
cfg.SessionSummary = false
}
}
func checkConfig() {
r := config.LoadFull(".", cli.Config)
paths := config.ConfigSearchPaths(".", cli.Config)
fmt.Println("Search paths (highest priority first):")
for _, p := range paths {
if p == r.Path {
fmt.Printf(" ✓ %s\n", p)
} else {
fmt.Printf(" · %s\n", p)
}
}
fmt.Println()
if r.Path == "" {
fmt.Println("No configuration file found (defaults will be used).")
return
}
if len(r.Warnings) == 0 {
fmt.Printf("Config OK: %s\n", r.Path)
return
}
fmt.Fprintf(os.Stderr, "Config errors in %s:\n", r.Path)
for _, w := range r.Warnings {
fmt.Fprintf(os.Stderr, " ✗ %s\n", w)
}
os.Exit(1)
}
func main() {
kong.Parse(&cli,
kong.Name("lazyenv"),
kong.Description("TUI for managing .env files."),
kong.Vars{"version": fmt.Sprintf("%s (commit: %s, built: %s)", version, commit, date)},
)
if cli.Path == "" {
cli.Path = "."
}
if cli.CheckConfig {
checkConfig()
return
}
cfg, warnings := config.Load(".", cli.Config)
cfg.Dir = cli.Path
warnings = append(warnings, applyCLIOverrides(&cfg)...)
config.FinalizeColors(&cfg)
if cli.ListThemes {
for _, name := range config.ThemeNames() {
fmt.Println(name)
}
return
}
if cli.Themes {
selected, err := tui.RunThemePreview(cfg.NoMouse)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
if selected != "" {
fmt.Printf("To use this theme, add to your config file:\n\n theme = %q\n", selected)
}
return
}
if cli.ShowConfig {
enc := toml.NewEncoder(os.Stdout)
enc.SetIndentTables(true)
if err := enc.Encode(cfg); err != nil {
fmt.Fprintf(os.Stderr, "Error encoding config: %v\n", err)
os.Exit(1)
}
return
}
app := tui.NewApp(cfg, warnings)
p := tea.NewProgram(app)
finalModel, err := p.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(1)
}
if finalApp, ok := finalModel.(tui.App); ok {
if summary := finalApp.SessionSummary(); summary != "" {
fmt.Print(summary)
}
}
}