1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-12 11:15:00 +02:00
lazygit/main.go

152 lines
3.8 KiB
Go
Raw Normal View History

2018-05-19 03:16:34 +02:00
package main
2018-06-02 00:35:49 +02:00
import (
2020-10-03 06:54:55 +02:00
"bytes"
"fmt"
"log"
"os"
2020-09-27 07:36:04 +02:00
"path/filepath"
"runtime"
"github.com/go-errors/errors"
"github.com/integrii/flaggy"
"github.com/jesseduffield/lazygit/pkg/app"
"github.com/jesseduffield/lazygit/pkg/config"
2021-04-11 15:32:20 +02:00
"github.com/jesseduffield/lazygit/pkg/constants"
"github.com/jesseduffield/lazygit/pkg/env"
2020-10-03 06:54:55 +02:00
yaml "github.com/jesseduffield/yaml"
2018-06-02 00:35:49 +02:00
)
2018-06-05 11:30:55 +02:00
var (
2018-08-25 07:55:49 +02:00
commit string
version = "unversioned"
date string
buildSource = "unknown"
2018-06-05 11:30:55 +02:00
)
2018-05-27 08:32:09 +02:00
2018-05-19 03:16:34 +02:00
func main() {
flaggy.DefaultParser.ShowVersionWithVersionFlag = false
2020-09-27 07:36:04 +02:00
repoPath := ""
flaggy.String(&repoPath, "p", "path", "Path of git repo. (equivalent to --work-tree=<path> --git-dir=<path>/.git/)")
2020-03-29 01:11:15 +02:00
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")
dump := ""
flaggy.AddPositionalValue(&dump, "gitargs", 1, false, "Todo file")
flaggy.DefaultParser.PositionalFlags[0].Hidden = true
versionFlag := false
flaggy.Bool(&versionFlag, "v", "version", "Print the current version")
debuggingFlag := false
2020-09-26 02:52:04 +02:00
flaggy.Bool(&debuggingFlag, "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)")
logFlag := false
flaggy.Bool(&logFlag, "l", "logs", "Tail lazygit logs (intended to be used when `lazygit --debug` is called in a separate terminal tab)")
configFlag := false
2020-09-18 00:39:16 +02:00
flaggy.Bool(&configFlag, "c", "config", "Print the default config")
2020-10-04 00:53:56 +02:00
configDirFlag := false
2020-10-04 13:05:39 +02:00
flaggy.Bool(&configDirFlag, "cd", "print-config-dir", "Print the config directory")
useConfigDir := ""
flaggy.String(&useConfigDir, "ucd", "use-config-dir", "override default config directory with provided directory")
2020-10-04 00:53:56 +02:00
2020-09-27 07:36:04 +02:00
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")
2021-07-27 22:03:37 +02:00
customConfig := ""
2021-10-16 03:07:24 +02:00
flaggy.String(&customConfig, "ucf", "use-config-file", "Comma seperated list to custom config file(s)")
2021-07-27 22:03:37 +02:00
flaggy.Parse()
2020-09-27 07:36:04 +02:00
if repoPath != "" {
if workTree != "" || gitDir != "" {
log.Fatal("--path option is incompatible with the --work-tree and --git-dir options")
}
absRepoPath, err := filepath.Abs(repoPath)
if err != nil {
log.Fatal(err)
}
workTree = absRepoPath
gitDir = filepath.Join(absRepoPath, ".git")
2020-09-27 07:36:04 +02:00
}
2021-07-27 22:03:37 +02:00
if customConfig != "" {
os.Setenv("LG_CONFIG_FILE", customConfig)
}
2020-10-04 13:05:39 +02:00
if useConfigDir != "" {
os.Setenv("CONFIG_DIR", useConfigDir)
}
2020-09-27 07:36:04 +02:00
if workTree != "" {
env.SetGitWorkTreeEnv(workTree)
2020-09-27 07:36:04 +02:00
}
if gitDir != "" {
env.SetGitDirEnv(gitDir)
2020-09-27 07:36:04 +02:00
}
if versionFlag {
2018-08-25 07:55:49 +02:00
fmt.Printf("commit=%s, build date=%s, build source=%s, version=%s, os=%s, arch=%s\n", commit, date, buildSource, version, runtime.GOOS, runtime.GOARCH)
os.Exit(0)
}
if configFlag {
2020-10-03 06:54:55 +02:00
var buf bytes.Buffer
encoder := yaml.NewEncoder(&buf)
err := encoder.Encode(config.GetDefaultConfig())
if err != nil {
log.Fatal(err.Error())
}
2020-10-04 00:53:56 +02:00
fmt.Printf("%s\n", buf.String())
os.Exit(0)
}
if configDirFlag {
2020-11-22 15:51:48 +02:00
fmt.Printf("%s\n", config.ConfigDir())
2018-08-26 10:42:25 +02:00
os.Exit(0)
}
if logFlag {
app.TailLogs()
os.Exit(0)
}
2020-09-27 07:36:04 +02:00
if workTree != "" {
if err := os.Chdir(workTree); err != nil {
log.Fatal(err.Error())
}
}
appConfig, err := config.NewAppConfig("lazygit", version, commit, date, buildSource, debuggingFlag)
2018-08-15 13:34:25 +02:00
if err != nil {
log.Fatal(err.Error())
}
2018-08-15 13:34:25 +02:00
2020-03-29 01:11:15 +02:00
app, err := app.NewApp(appConfig, filterPath)
if err == nil {
err = app.Run()
2018-08-18 11:43:58 +02:00
}
if err != nil {
if errorMessage, known := app.KnownError(err); known {
log.Fatal(errorMessage)
}
newErr := errors.Wrap(err, 0)
stackTrace := newErr.ErrorStack()
app.Log.Error(stackTrace)
2021-04-11 15:32:20 +02:00
log.Fatal(fmt.Sprintf("%s: %s\n\n%s", app.Tr.ErrorOccurred, constants.Links.Issues, stackTrace))
}
2018-05-19 03:16:34 +02:00
}