diff options
author | Jesse Duffield <jessedduffield@gmail.com> | 2022-08-13 12:56:04 +1000 |
---|---|---|
committer | Jesse Duffield <jessedduffield@gmail.com> | 2022-08-13 13:56:50 +1000 |
commit | 304d74370e720473ee384e171a8e8f00956fc72f (patch) | |
tree | 79de40af6990ee00a426f67f12d1d9d7fcd8067b /pkg/app | |
parent | 2bdefe20498f58fe3e8e41a2be08650576ec6d6f (diff) |
refactor to ensure code doesn't depend on integration code
Diffstat (limited to 'pkg/app')
-rw-r--r-- | pkg/app/entry_point.go | 112 |
1 files changed, 110 insertions, 2 deletions
diff --git a/pkg/app/entry_point.go b/pkg/app/entry_point.go index 551b959a6..5a767bb94 100644 --- a/pkg/app/entry_point.go +++ b/pkg/app/entry_point.go @@ -7,18 +7,22 @@ import ( "os" "path/filepath" "runtime" + "runtime/debug" "strings" + "github.com/integrii/flaggy" "github.com/jesseduffield/lazygit/pkg/app/daemon" appTypes "github.com/jesseduffield/lazygit/pkg/app/types" "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/env" integrationTypes "github.com/jesseduffield/lazygit/pkg/integration/types" "github.com/jesseduffield/lazygit/pkg/logs" + "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/samber/lo" "gopkg.in/yaml.v3" ) -type CliArgs struct { +type cliArgs struct { RepoPath string FilterPath string GitArg string @@ -40,7 +44,10 @@ type BuildInfo struct { BuildSource string } -func Start(cliArgs *CliArgs, buildInfo *BuildInfo, integrationTest integrationTypes.IntegrationTest) { +func Start(buildInfo *BuildInfo, integrationTest integrationTypes.IntegrationTest) { + cliArgs := parseCliArgsAndEnvVars() + mergeBuildInfo(buildInfo) + if cliArgs.RepoPath != "" { if cliArgs.WorkTree != "" || cliArgs.GitDir != "" { log.Fatal("--path option is incompatible with the --work-tree and --git-dir options") @@ -132,6 +139,67 @@ func Start(cliArgs *CliArgs, buildInfo *BuildInfo, integrationTest integrationTy Run(appConfig, common, appTypes.NewStartArgs(cliArgs.FilterPath, parsedGitArg, integrationTest)) } +func parseCliArgsAndEnvVars() *cliArgs { + flaggy.DefaultParser.ShowVersionWithVersionFlag = false + + repoPath := "" + flaggy.String(&repoPath, "p", "path", "Path of git repo. (equivalent to --work-tree=<path> --git-dir=<path>/.git/)") + + filterPath := "" + flaggy.String(&filterPath, "f", "filter", "Path to filter on in `git log -- <path>`. When in filter mode, the commits, reflog, and stash are filtered based on the given path, and some operations are restricted") + + gitArg := "" + flaggy.AddPositionalValue(&gitArg, "git-arg", 1, false, "Panel to focus upon opening lazygit. Accepted values (based on git terminology): status, branch, log, stash. Ignored if --filter arg is passed.") + + printVersionInfo := false + flaggy.Bool(&printVersionInfo, "v", "version", "Print the current version") + + debug := false + flaggy.Bool(&debug, "d", "debug", "Run in debug mode with logging (see --logs flag below). Use the LOG_LEVEL env var to set the log level (debug/info/warn/error)") + + tailLogs := false + flaggy.Bool(&tailLogs, "l", "logs", "Tail lazygit logs (intended to be used when `lazygit --debug` is called in a separate terminal tab)") + + printDefaultConfig := false + flaggy.Bool(&printDefaultConfig, "c", "config", "Print the default config") + + printConfigDir := false + flaggy.Bool(&printConfigDir, "cd", "print-config-dir", "Print the config directory") + + useConfigDir := "" + flaggy.String(&useConfigDir, "ucd", "use-config-dir", "override default config directory with provided directory") + + workTree := "" + flaggy.String(&workTree, "w", "work-tree", "equivalent of the --work-tree git argument") + + gitDir := "" + flaggy.String(&gitDir, "g", "git-dir", "equivalent of the --git-dir git argument") + + customConfigFile := "" + flaggy.String(&customConfigFile, "ucf", "use-config-file", "Comma separated list to custom config file(s)") + + flaggy.Parse() + + if os.Getenv("DEBUG") == "TRUE" { + debug = true + } + + return &cliArgs{ + RepoPath: repoPath, + FilterPath: filterPath, + GitArg: gitArg, + PrintVersionInfo: printVersionInfo, + Debug: debug, + TailLogs: tailLogs, + PrintDefaultConfig: printDefaultConfig, + PrintConfigDir: printConfigDir, + UseConfigDir: useConfigDir, + WorkTree: workTree, + GitDir: gitDir, + CustomConfigFile: customConfigFile, + } +} + func parseGitArg(gitArg string) appTypes.GitArg { typedArg := appTypes.GitArg(gitArg) @@ -155,3 +223,43 @@ func parseGitArg(gitArg string) appTypes.GitArg { panic("unreachable") } + +// the buildInfo struct we get passed in is based on what's baked into the lazygit +// binary via the LDFLAGS argument. Some lazygit distributions will make use of these +// arguments and some will not. Go recently started baking in build info +// into the binary by default e.g. the git commit hash. So in this function +// we merge the two together, giving priority to the stuff set by LDFLAGS. +// Note: this mutates the argument passed in +func mergeBuildInfo(buildInfo *BuildInfo) { + // if the version has already been set by build flags then we'll honour that. + // chances are it's something like v0.31.0 which is more informative than a + // commit hash. + if buildInfo.Version != "" { + return + } + + buildInfo.Version = "unversioned" + + goBuildInfo, ok := debug.ReadBuildInfo() + if !ok { + return + } + + revision, ok := lo.Find(goBuildInfo.Settings, func(setting debug.BuildSetting) bool { + return setting.Key == "vcs.revision" + }) + if ok { + buildInfo.Commit = revision.Value + // if lazygit was built from source we'll show the version as the + // abbreviated commit hash + buildInfo.Version = utils.ShortSha(revision.Value) + } + + // if version hasn't been set we assume that neither has the date + time, ok := lo.Find(goBuildInfo.Settings, func(setting debug.BuildSetting) bool { + return setting.Key == "vcs.time" + }) + if ok { + buildInfo.Date = time.Value + } +} |