Skip to content

Commit 1a035db

Browse files
karimkhaleelstefanhaller
authored andcommitted
Add UserConfig jsonschema generation script
1 parent df5b369 commit 1a035db

File tree

5 files changed

+1681
-48
lines changed

5 files changed

+1681
-48
lines changed

pkg/commands/git_commands/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (self *ConfigCommands) GetPager(width int) string {
5353
"columnWidth": strconv.Itoa(width/2 - 6),
5454
}
5555

56-
pagerTemplate := self.UserConfig.Git.Paging.Pager
56+
pagerTemplate := string(self.UserConfig.Git.Paging.Pager)
5757
return utils.ResolvePlaceholderString(pagerTemplate, templateValues)
5858
}
5959

pkg/config/user_config.go

Lines changed: 59 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package config
22

33
import (
44
"time"
5+
6+
"github.com/karimkhaleel/jsonschema"
57
)
68

79
type UserConfig struct {
@@ -25,26 +27,26 @@ type UserConfig struct {
2527
// Lazygit sets this to true upon first runninng the program so that you don't see introductory popups every time you open the program.
2628
DisableStartupPopups bool `yaml:"disableStartupPopups"`
2729
// User-configured commands that can be invoked from within Lazygit
28-
CustomCommands []CustomCommand `yaml:"customCommands"`
30+
CustomCommands []CustomCommand `yaml:"customCommands" jsonschema:"uniqueItems=true"`
2931
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-pull-request-urls
3032
Services map[string]string `yaml:"services"`
3133
// What to do when opening Lazygit outside of a git repo.
3234
// - 'prompt': (default) ask whether to initialize a new repo or open in the most recent repo
3335
// - 'create': initialize a new repo
3436
// - 'skip': open most recent repo
3537
// - 'quit': exit Lazygit
36-
NotARepository string `yaml:"notARepository"`
38+
NotARepository string `yaml:"notARepository" jsonschema:"enum=prompt,enum=create,enum=skip,enum=quit"`
3739
// If true, display a confirmation when subprocess terminates. This allows you to view the output of the subprocess before returning to Lazygit.
3840
PromptToReturnFromSubprocess bool `yaml:"promptToReturnFromSubprocess"`
3941
}
4042

4143
type RefresherConfig struct {
4244
// File/submodule refresh interval in seconds.
4345
// Auto-refresh can be disabled via option 'git.autoRefresh'.
44-
RefreshInterval int `yaml:"refreshInterval"`
46+
RefreshInterval int `yaml:"refreshInterval" jsonschema:"minimum=0"`
4547
// Re-fetch interval in seconds.
4648
// Auto-fetch can be disabled via option 'git.autoFetch'.
47-
FetchInterval int `yaml:"fetchInterval"`
49+
FetchInterval int `yaml:"fetchInterval" jsonschema:"minimum=0"`
4850
}
4951

5052
type GuiConfig struct {
@@ -53,7 +55,7 @@ type GuiConfig struct {
5355
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#custom-branch-color
5456
BranchColors map[string]string `yaml:"branchColors"`
5557
// The number of lines you scroll by when scrolling the main window
56-
ScrollHeight int `yaml:"scrollHeight"`
58+
ScrollHeight int `yaml:"scrollHeight" jsonschema:"minimum=1"`
5759
// If true, allow scrolling past the bottom of the content in the main window
5860
ScrollPastBottom bool `yaml:"scrollPastBottom"`
5961
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#scroll-off-margin
@@ -73,17 +75,17 @@ type GuiConfig struct {
7375
SkipRewordInEditorWarning bool `yaml:"skipRewordInEditorWarning"`
7476
// Fraction of the total screen width to use for the left side section. You may want to pick a small number (e.g. 0.2) if you're using a narrow screen, so that you can see more of the main section.
7577
// Number from 0 to 1.0.
76-
SidePanelWidth float64 `yaml:"sidePanelWidth"`
78+
SidePanelWidth float64 `yaml:"sidePanelWidth" jsonschema:"maximum=1,minimum=0"`
7779
// If true, increase the height of the focused side window; creating an accordion effect.
7880
ExpandFocusedSidePanel bool `yaml:"expandFocusedSidePanel"`
7981
// Sometimes the main window is split in two (e.g. when the selected file has both staged and unstaged changes). This setting controls how the two sections are split.
8082
// Options are:
8183
// - 'horizontal': split the window horizontally
8284
// - 'vertical': split the window vertically
8385
// - 'flexible': (default) split the window horizontally if the window is wide enough, otherwise split vertically
84-
MainPanelSplitMode string `yaml:"mainPanelSplitMode"`
86+
MainPanelSplitMode string `yaml:"mainPanelSplitMode" jsonschema:"enum=horizontal,enum=flexible,enum=vertical"`
8587
// One of 'auto' (default) | 'en' | 'zh-CN' | 'zh-TW' | 'pl' | 'nl' | 'ja' | 'ko' | 'ru'
86-
Language string `yaml:"language"`
88+
Language string `yaml:"language" jsonschema:"enum=auto,enum=en,enum=zh-TW,enum=zh-CN,enum=pl,enum=nl,enum=ja,enum=ko,enum=ru"`
8789
// Format used when displaying time e.g. commit time.
8890
// Uses Go's time format syntax: https://pkg.go.dev/time#Time.Format
8991
TimeFormat string `yaml:"timeFormat"`
@@ -113,21 +115,21 @@ type GuiConfig struct {
113115
// Nerd fonts version to use.
114116
// One of: '2' | '3' | empty string (default)
115117
// If empty, do not show icons.
116-
NerdFontsVersion string `yaml:"nerdFontsVersion"`
118+
NerdFontsVersion string `yaml:"nerdFontsVersion" jsonschema:"enum=2,enum=3,enum="`
117119
// If true, show commit hashes alongside branch names in the branches view.
118120
ShowBranchCommitHash bool `yaml:"showBranchCommitHash"`
119121
// Height of the command log view
120-
CommandLogSize int `yaml:"commandLogSize"`
122+
CommandLogSize int `yaml:"commandLogSize" jsonschema:"minimum=0"`
121123
// Whether to split the main window when viewing file changes.
122124
// One of: 'auto' | 'always'
123125
// If 'auto', only split the main window when a file has both staged and unstaged changes
124-
SplitDiff string `yaml:"splitDiff"`
126+
SplitDiff string `yaml:"splitDiff" jsonschema:"enum=auto,enum=always"`
125127
// Default size for focused window. Window size can be changed from within Lazygit with '+' and '_' (but this won't change the default).
126128
// One of: 'normal' (default) | 'half' | 'full'
127-
WindowSize string `yaml:"windowSize"`
129+
WindowSize string `yaml:"windowSize" jsonschema:"enum=normal,enum=half,enum=full"`
128130
// Window border style.
129131
// One of 'rounded' (default) | 'single' | 'double' | 'hidden'
130-
Border string `yaml:"border"`
132+
Border string `yaml:"border" jsonschema:"enum=single,enum=double,enum=rounded,enum=hidden"`
131133
// If true, show a seriously epic explosion animation when nuking the working tree.
132134
AnimateExplosion bool `yaml:"animateExplosion"`
133135
// Whether to stack UI components on top of each other.
@@ -137,31 +139,31 @@ type GuiConfig struct {
137139

138140
type ThemeConfig struct {
139141
// Border color of focused window
140-
ActiveBorderColor []string `yaml:"activeBorderColor"`
142+
ActiveBorderColor []string `yaml:"activeBorderColor" jsonschema:"minItems=1,uniqueItems=true"`
141143
// Border color of non-focused windows
142-
InactiveBorderColor []string `yaml:"inactiveBorderColor"`
144+
InactiveBorderColor []string `yaml:"inactiveBorderColor" jsonschema:"minItems=1,uniqueItems=true"`
143145
// Border color of focused window when searching in that window
144-
SearchingActiveBorderColor []string `yaml:"searchingActiveBorderColor"`
146+
SearchingActiveBorderColor []string `yaml:"searchingActiveBorderColor" jsonschema:"minItems=1,uniqueItems=true"`
145147
// Color of keybindings help text in the bottom line
146-
OptionsTextColor []string `yaml:"optionsTextColor"`
148+
OptionsTextColor []string `yaml:"optionsTextColor" jsonschema:"minItems=1,uniqueItems=true"`
147149
// Background color of selected line.
148150
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#highlighting-the-selected-line
149-
SelectedLineBgColor []string `yaml:"selectedLineBgColor"`
151+
SelectedLineBgColor []string `yaml:"selectedLineBgColor" jsonschema:"minItems=1,uniqueItems=true"`
150152
// Background color of selected range
151153
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#highlighting-the-selected-line
152-
SelectedRangeBgColor []string `yaml:"selectedRangeBgColor"`
154+
SelectedRangeBgColor []string `yaml:"selectedRangeBgColor" jsonschema:"minItems=1,uniqueItems=true"`
153155
// Foreground color of copied commit
154-
CherryPickedCommitFgColor []string `yaml:"cherryPickedCommitFgColor"`
156+
CherryPickedCommitFgColor []string `yaml:"cherryPickedCommitFgColor" jsonschema:"minItems=1,uniqueItems=true"`
155157
// Background color of copied commit
156-
CherryPickedCommitBgColor []string `yaml:"cherryPickedCommitBgColor"`
158+
CherryPickedCommitBgColor []string `yaml:"cherryPickedCommitBgColor" jsonschema:"minItems=1,uniqueItems=true"`
157159
// Foreground color of marked base commit (for rebase)
158160
MarkedBaseCommitFgColor []string `yaml:"markedBaseCommitFgColor"`
159161
// Background color of marked base commit (for rebase)
160162
MarkedBaseCommitBgColor []string `yaml:"markedBaseCommitBgColor"`
161163
// Color for file with unstaged changes
162-
UnstagedChangesColor []string `yaml:"unstagedChangesColor"`
164+
UnstagedChangesColor []string `yaml:"unstagedChangesColor" jsonschema:"minItems=1,uniqueItems=true"`
163165
// Default text color
164-
DefaultFgColor []string `yaml:"defaultFgColor"`
166+
DefaultFgColor []string `yaml:"defaultFgColor" jsonschema:"minItems=1,uniqueItems=true"`
165167
}
166168

167169
type CommitLengthConfig struct {
@@ -177,7 +179,7 @@ type GitConfig struct {
177179
// Config relating to merging
178180
Merging MergingConfig `yaml:"merging"`
179181
// list of branches that are considered 'main' branches, used when displaying commits
180-
MainBranches []string `yaml:"mainBranches"`
182+
MainBranches []string `yaml:"mainBranches" jsonschema:"uniqueItems=true"`
181183
// Prefix to use when skipping hooks. E.g. if set to 'WIP', then pre-commit hooks will be skipped when the commit message starts with 'WIP'
182184
SkipHookPrefix string `yaml:"skipHookPrefix"`
183185
// If true, periodically fetch from remote
@@ -203,14 +205,24 @@ type GitConfig struct {
203205
Log LogConfig `yaml:"log"`
204206
}
205207

208+
type PagerType string
209+
210+
func (PagerType) JSONSchemaExtend(schema *jsonschema.Schema) {
211+
schema.Examples = []any{
212+
"delta --dark --paging=never",
213+
"diff-so-fancy",
214+
"ydiff -p cat -s --wrap --width={{columnWidth}}",
215+
}
216+
}
217+
206218
type PagingConfig struct {
207219
// Value of the --color arg in the git diff command. Some pagers want this to be set to 'always' and some want it set to 'never'
208-
ColorArg string `yaml:"colorArg"`
220+
ColorArg string `yaml:"colorArg" jsonschema:"enum=always,enum=never"`
209221
// e.g.
210222
// diff-so-fancy
211223
// delta --dark --paging=never
212224
// ydiff -p cat -s --wrap --width={{columnWidth}}
213-
Pager string `yaml:"pager"`
225+
Pager PagerType `yaml:"pager" jsonschema:"minLength=1"`
214226
// If true, Lazygit will use whatever pager is specified in `$GIT_PAGER`, `$PAGER`, or your *git config*. If the pager ends with something like ` | less` we will strip that part out, because less doesn't play nice with our rendering approach. If the custom pager uses less under the hood, that will also break rendering (hence the `--paging=never` flag for the `delta` pager).
215227
UseConfig bool `yaml:"useConfig"`
216228
// e.g. 'difft --color=always'
@@ -227,33 +239,33 @@ type MergingConfig struct {
227239
// Only applicable to unix users.
228240
ManualCommit bool `yaml:"manualCommit"`
229241
// Extra args passed to `git merge`, e.g. --no-ff
230-
Args string `yaml:"args"`
242+
Args string `yaml:"args" jsonschema:"example=--no-ff"`
231243
}
232244

233245
type LogConfig struct {
234-
// One of: 'date-order' | 'author-date-order' | 'topo-order'
246+
// One of: 'date-order' | 'author-date-order' | 'topo-order | default'
235247
// 'topo-order' makes it easier to read the git log graph, but commits may not
236248
// appear chronologically. See https://git-scm.com/docs/
237-
Order string `yaml:"order"`
249+
Order string `yaml:"order" jsonschema:"enum=date-order,enum=author-date-order,enum=topo-order,enum=default"`
238250
// This determines whether the git graph is rendered in the commits panel
239-
// One of 'always' | 'never' 'when-maximised'
240-
ShowGraph string `yaml:"showGraph"`
251+
// One of 'always' | 'never' | 'when-maximised'
252+
ShowGraph string `yaml:"showGraph" jsonschema:"enum=always,enum=never,enum=when-maximised"`
241253
// displays the whole git graph by default in the commits view (equivalent to passing the `--all` argument to `git log`)
242254
ShowWholeGraph bool `yaml:"showWholeGraph"`
243255
}
244256

245257
type CommitPrefixConfig struct {
246258
// pattern to match on. E.g. for 'feature/AB-123' to match on the AB-123 use "^\\w+\\/(\\w+-\\w+).*"
247-
Pattern string `yaml:"pattern"`
259+
Pattern string `yaml:"pattern" jsonschema:"example=^\\w+\\/(\\w+-\\w+).*,minLength=1"`
248260
// Replace directive. E.g. for 'feature/AB-123' to start the commit message with 'AB-123 ' use "[$1] "
249-
Replace string `yaml:"replace"`
261+
Replace string `yaml:"replace" jsonschema:"example=[$1] ,minLength=1"`
250262
}
251263

252264
type UpdateConfig struct {
253265
// One of: 'prompt' (default) | 'background' | 'never'
254-
Method string `yaml:"method"`
266+
Method string `yaml:"method" jsonschema:"enum=prompt,enum=background,enum=never"`
255267
// Period in days between update checks
256-
Days int64 `yaml:"days"`
268+
Days int64 `yaml:"days" jsonschema:"minimum=0"`
257269
}
258270

259271
type KeybindingConfig struct {
@@ -459,7 +471,7 @@ type OSConfig struct {
459471

460472
// A built-in preset that sets all of the above settings. Supported presets
461473
// are defined in the getPreset function in editor_presets.go.
462-
EditPreset string `yaml:"editPreset,omitempty"`
474+
EditPreset string `yaml:"editPreset,omitempty" jsonschema:"example=vim,example=nvim,example=emacs,example=nano,example=vscode,example=sublime,example=kakoune,example=helix,example=xcode"`
463475

464476
// Command for opening a file, as if the file is double-clicked. Should
465477
// contain "{{filename}}", but doesn't support "{{line}}".
@@ -504,15 +516,15 @@ type CustomCommand struct {
504516
// The key to trigger the command. Use a single letter or one of the values from https://github.com/jesseduffield/lazygit/blob/master/docs/keybindings/Custom_Keybindings.md
505517
Key string `yaml:"key"`
506518
// The context in which to listen for the key
507-
Context string `yaml:"context"`
519+
Context string `yaml:"context" jsonschema:"enum=status,enum=files,enum=worktrees,enum=localBranches,enum=remotes,enum=remoteBranches,enum=tags,enum=commits,enum=reflogCommits,enum=subCommits,enum=commitFiles,enum=stash,enum=global"`
508520
// The command to run (using Go template syntax for placeholder values)
509-
Command string `yaml:"command"`
521+
Command string `yaml:"command" jsonschema:"example=git fetch {{.Form.Remote}} {{.Form.Branch}} && git checkout FETCH_HEAD"`
510522
// If true, run the command in a subprocess (e.g. if the command requires user input)
511523
Subprocess bool `yaml:"subprocess"`
512524
// A list of prompts that will request user input before running the final command
513525
Prompts []CustomCommandPrompt `yaml:"prompts"`
514526
// Text to display while waiting for command to finish
515-
LoadingText string `yaml:"loadingText"`
527+
LoadingText string `yaml:"loadingText" jsonschema:"example=Loading..."`
516528
// Label for the custom command when displayed in the keybindings menu
517529
Description string `yaml:"description"`
518530
// If true, stream the command's output to the Command Log panel
@@ -540,31 +552,31 @@ type CustomCommandPrompt struct {
540552

541553
// The message of the confirmation prompt.
542554
// Only for confirm prompts.
543-
Body string `yaml:"body"`
555+
Body string `yaml:"body" jsonschema:"example=Are you sure you want to push to the remote?"`
544556

545557
// Menu options.
546558
// Only for menu prompts.
547559
Options []CustomCommandMenuOption
548560

549561
// The command to run to generate menu options
550562
// Only for menuFromCommand prompts.
551-
Command string `yaml:"command"`
563+
Command string `yaml:"command" jsonschema:"example=git fetch {{.Form.Remote}} {{.Form.Branch}} && git checkout FETCH_HEAD"`
552564
// The regexp to run specifying groups which are going to be kept from the command's output.
553565
// Only for menuFromCommand prompts.
554-
Filter string `yaml:"filter"`
566+
Filter string `yaml:"filter" jsonschema:"example=.*{{.SelectedRemote.Name }}/(?P<branch>.*)"`
555567
// How to format matched groups from the filter to construct a menu item's value.
556568
// Only for menuFromCommand prompts.
557-
ValueFormat string `yaml:"valueFormat"`
569+
ValueFormat string `yaml:"valueFormat" jsonschema:"example={{ .branch }}"`
558570
// Like valueFormat but for the labels. If `labelFormat` is not specified, `valueFormat` is shown instead.
559571
// Only for menuFromCommand prompts.
560-
LabelFormat string `yaml:"labelFormat"`
572+
LabelFormat string `yaml:"labelFormat" jsonschema:"example={{ .branch | green }}"`
561573
}
562574

563575
type CustomCommandSuggestions struct {
564576
// Uses built-in logic to obtain the suggestions. One of 'authors' | 'branches' | 'files' | 'refs' | 'remotes' | 'remoteBranches' | 'tags'
565-
Preset string `yaml:"preset"`
577+
Preset string `yaml:"preset" jsonschema:"enum=authors,enum=branches,enum=files,enum=refs,enum=remotes,enum=remoteBranches,enum=tags"`
566578
// Command to run such that each line in the output becomes a suggestion. Mutually exclusive with 'preset' field.
567-
Command string `yaml:"command"`
579+
Command string `yaml:"command" jsonschema:"example=git fetch {{.Form.Remote}} {{.Form.Branch}} && git checkout FETCH_HEAD"`
568580
}
569581

570582
type CustomCommandMenuOption struct {
@@ -573,7 +585,7 @@ type CustomCommandMenuOption struct {
573585
// The second part of the label
574586
Description string `yaml:"description"`
575587
// The value that will be used in the command
576-
Value string `yaml:"value"`
588+
Value string `yaml:"value" jsonschema:"example=feature,minLength=1"`
577589
}
578590

579591
func GetDefaultConfig() *UserConfig {

0 commit comments

Comments
 (0)