2018-08-12 11:31:27 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2019-03-16 02:31:09 +02:00
|
|
|
"bufio"
|
2019-03-02 11:32:16 +02:00
|
|
|
"fmt"
|
2018-08-12 11:31:27 +02:00
|
|
|
"io"
|
2018-08-13 15:35:01 +02:00
|
|
|
"io/ioutil"
|
2018-08-13 13:16:21 +02:00
|
|
|
"os"
|
2018-12-08 07:54:54 +02:00
|
|
|
"path/filepath"
|
2019-02-19 14:36:29 +02:00
|
|
|
"strings"
|
2018-08-12 11:31:27 +02:00
|
|
|
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/config"
|
2018-08-13 12:26:02 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui"
|
2018-08-14 15:26:25 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/i18n"
|
2018-08-19 15:28:29 +02:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/updates"
|
2019-06-29 12:52:49 +02:00
|
|
|
"github.com/jesseduffield/rollrus"
|
2018-12-08 07:54:54 +02:00
|
|
|
"github.com/shibukawa/configdir"
|
2018-08-25 07:55:49 +02:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-08-12 11:31:27 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// App struct
|
|
|
|
type App struct {
|
|
|
|
closers []io.Closer
|
|
|
|
|
2019-03-03 03:44:10 +02:00
|
|
|
Config config.AppConfigurer
|
|
|
|
Log *logrus.Entry
|
|
|
|
OSCommand *commands.OSCommand
|
|
|
|
GitCommand *commands.GitCommand
|
|
|
|
Gui *gui.Gui
|
|
|
|
Tr *i18n.Localizer
|
|
|
|
Updater *updates.Updater // may only need this on the Gui
|
|
|
|
ClientContext string
|
2018-08-13 13:16:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 07:46:18 +02:00
|
|
|
func newProductionLogger(config config.AppConfigurer) *logrus.Logger {
|
2018-08-13 13:16:21 +02:00
|
|
|
log := logrus.New()
|
2018-08-26 07:46:18 +02:00
|
|
|
log.Out = ioutil.Discard
|
2019-03-16 01:37:31 +02:00
|
|
|
log.SetLevel(logrus.ErrorLevel)
|
2018-08-26 07:46:18 +02:00
|
|
|
return log
|
|
|
|
}
|
|
|
|
|
2018-12-08 07:54:54 +02:00
|
|
|
func globalConfigDir() string {
|
|
|
|
configDirs := configdir.New("jesseduffield", "lazygit")
|
|
|
|
configDir := configDirs.QueryFolders(configdir.Global)[0]
|
|
|
|
return configDir.Path
|
|
|
|
}
|
|
|
|
|
2019-03-16 01:37:31 +02:00
|
|
|
func getLogLevel() logrus.Level {
|
|
|
|
strLevel := os.Getenv("LOG_LEVEL")
|
|
|
|
level, err := logrus.ParseLevel(strLevel)
|
|
|
|
if err != nil {
|
|
|
|
return logrus.DebugLevel
|
|
|
|
}
|
|
|
|
return level
|
|
|
|
}
|
|
|
|
|
2018-12-08 07:54:54 +02:00
|
|
|
func newDevelopmentLogger(config config.AppConfigurer) *logrus.Logger {
|
2018-08-26 07:46:18 +02:00
|
|
|
log := logrus.New()
|
2019-03-16 01:37:31 +02:00
|
|
|
log.SetLevel(getLogLevel())
|
2018-12-08 07:54:54 +02:00
|
|
|
file, err := os.OpenFile(filepath.Join(globalConfigDir(), "development.log"), os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
|
2018-08-13 13:16:21 +02:00
|
|
|
if err != nil {
|
|
|
|
panic("unable to log to file") // TODO: don't panic (also, remove this call to the `panic` function)
|
|
|
|
}
|
2018-08-14 10:02:27 +02:00
|
|
|
log.SetOutput(file)
|
2018-08-13 13:16:21 +02:00
|
|
|
return log
|
2018-08-12 11:31:27 +02:00
|
|
|
}
|
|
|
|
|
2018-08-26 07:46:18 +02:00
|
|
|
func newLogger(config config.AppConfigurer) *logrus.Entry {
|
|
|
|
var log *logrus.Logger
|
2018-08-28 10:01:53 +02:00
|
|
|
environment := "production"
|
2019-02-18 12:29:43 +02:00
|
|
|
if config.GetDebug() || os.Getenv("DEBUG") == "TRUE" {
|
2018-08-28 10:01:53 +02:00
|
|
|
environment = "development"
|
2018-12-08 07:54:54 +02:00
|
|
|
log = newDevelopmentLogger(config)
|
2018-08-26 07:46:18 +02:00
|
|
|
} else {
|
|
|
|
log = newProductionLogger(config)
|
|
|
|
}
|
2018-12-07 09:52:31 +02:00
|
|
|
|
|
|
|
// highly recommended: tail -f development.log | humanlog
|
|
|
|
// https://github.com/aybabtme/humanlog
|
2018-12-04 10:50:11 +02:00
|
|
|
log.Formatter = &logrus.JSONFormatter{}
|
|
|
|
|
2019-06-29 12:51:28 +02:00
|
|
|
if config.GetUserConfig().GetString("reporting") == "on" {
|
2018-08-28 10:01:53 +02:00
|
|
|
// this isn't really a secret token: it only has permission to push new rollbar items
|
|
|
|
hook := rollrus.NewHook("23432119147a4367abf7c0de2aa99a2d", environment)
|
|
|
|
log.Hooks.Add(hook)
|
|
|
|
}
|
2018-08-26 07:46:18 +02:00
|
|
|
return log.WithFields(logrus.Fields{
|
|
|
|
"debug": config.GetDebug(),
|
|
|
|
"version": config.GetVersion(),
|
|
|
|
"commit": config.GetCommit(),
|
|
|
|
"buildDate": config.GetBuildDate(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-18 10:42:23 +02:00
|
|
|
// NewApp bootstrap a new application
|
|
|
|
func NewApp(config config.AppConfigurer) (*App, error) {
|
2018-08-12 11:31:27 +02:00
|
|
|
app := &App{
|
|
|
|
closers: []io.Closer{},
|
|
|
|
Config: config,
|
|
|
|
}
|
|
|
|
var err error
|
2018-08-13 13:16:21 +02:00
|
|
|
app.Log = newLogger(config)
|
2018-08-20 21:04:04 +02:00
|
|
|
app.Tr = i18n.NewLocalizer(app.Log)
|
2018-08-14 14:12:07 +02:00
|
|
|
|
2019-02-18 12:29:43 +02:00
|
|
|
// if we are being called in 'demon' mode, we can just return here
|
2019-03-03 03:44:10 +02:00
|
|
|
app.ClientContext = os.Getenv("LAZYGIT_CLIENT_COMMAND")
|
|
|
|
if app.ClientContext != "" {
|
2019-02-18 12:29:43 +02:00
|
|
|
return app, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
app.OSCommand = commands.NewOSCommand(app.Log, config)
|
|
|
|
|
2018-09-07 01:41:15 +02:00
|
|
|
app.Updater, err = updates.NewUpdater(app.Log, config, app.OSCommand, app.Tr)
|
2018-08-12 11:31:27 +02:00
|
|
|
if err != nil {
|
2018-08-18 11:43:58 +02:00
|
|
|
return app, err
|
2018-08-12 11:31:27 +02:00
|
|
|
}
|
2019-03-16 02:31:09 +02:00
|
|
|
|
|
|
|
if err := app.setupRepo(); err != nil {
|
|
|
|
return app, err
|
|
|
|
}
|
|
|
|
|
2019-02-18 12:29:43 +02:00
|
|
|
app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand, app.Tr, app.Config)
|
2018-08-19 15:28:29 +02:00
|
|
|
if err != nil {
|
|
|
|
return app, err
|
|
|
|
}
|
|
|
|
app.Gui, err = gui.NewGui(app.Log, app.GitCommand, app.OSCommand, app.Tr, config, app.Updater)
|
2018-08-13 12:26:02 +02:00
|
|
|
if err != nil {
|
2018-08-18 11:43:58 +02:00
|
|
|
return app, err
|
2018-08-13 12:26:02 +02:00
|
|
|
}
|
2018-08-12 11:31:27 +02:00
|
|
|
return app, nil
|
|
|
|
}
|
|
|
|
|
2019-03-16 02:31:09 +02:00
|
|
|
func (app *App) setupRepo() error {
|
|
|
|
// if we are not in a git repo, we ask if we want to `git init`
|
|
|
|
if err := app.OSCommand.RunCommand("git status"); err != nil {
|
|
|
|
if !strings.Contains(err.Error(), "Not a git repository") {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Print(app.Tr.SLocalize("CreateRepo"))
|
|
|
|
response, _ := bufio.NewReader(os.Stdin).ReadString('\n')
|
|
|
|
if strings.Trim(response, " \n") != "y" {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if err := app.OSCommand.RunCommand("git init"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-18 10:42:23 +02:00
|
|
|
func (app *App) Run() error {
|
2019-03-03 03:44:10 +02:00
|
|
|
if app.ClientContext == "INTERACTIVE_REBASE" {
|
2019-02-18 12:29:43 +02:00
|
|
|
return app.Rebase()
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:44:10 +02:00
|
|
|
if app.ClientContext == "EXIT_IMMEDIATELY" {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2019-02-18 10:42:23 +02:00
|
|
|
return app.Gui.RunWithSubprocesses()
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:36:29 +02:00
|
|
|
// Rebase contains logic for when we've been run in demon mode, meaning we've
|
|
|
|
// given lazygit as a command for git to call e.g. to edit a file
|
2019-02-18 12:29:43 +02:00
|
|
|
func (app *App) Rebase() error {
|
2019-02-19 14:36:29 +02:00
|
|
|
app.Log.Info("Lazygit invoked as interactive rebase demon")
|
|
|
|
app.Log.Info("args: ", os.Args)
|
|
|
|
|
|
|
|
if strings.HasSuffix(os.Args[1], "git-rebase-todo") {
|
2019-03-02 04:22:02 +02:00
|
|
|
if err := ioutil.WriteFile(os.Args[1], []byte(os.Getenv("LAZYGIT_REBASE_TODO")), 0644); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-19 14:36:29 +02:00
|
|
|
} else if strings.HasSuffix(os.Args[1], ".git/COMMIT_EDITMSG") {
|
|
|
|
// if we are rebasing and squashing, we'll see a COMMIT_EDITMSG
|
|
|
|
// but in this case we don't need to edit it, so we'll just return
|
|
|
|
} else {
|
|
|
|
app.Log.Info("Lazygit demon did not match on any use cases")
|
|
|
|
}
|
2019-02-18 12:29:43 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-08-12 11:31:27 +02:00
|
|
|
// Close closes any resources
|
|
|
|
func (app *App) Close() error {
|
|
|
|
for _, closer := range app.closers {
|
|
|
|
err := closer.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|