2018-08-12 11:31:27 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2018-08-12 11:31:27 +02:00
|
|
|
|
2018-08-26 07:46:18 +02:00
|
|
|
"github.com/heroku/rollrus"
|
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"
|
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
|
|
|
|
|
|
|
|
Config config.AppConfigurer
|
2018-08-26 07:46:18 +02:00
|
|
|
Log *logrus.Entry
|
2018-08-12 11:31:27 +02:00
|
|
|
OSCommand *commands.OSCommand
|
|
|
|
GitCommand *commands.GitCommand
|
2018-08-13 13:16:21 +02:00
|
|
|
Gui *gui.Gui
|
2018-08-14 14:12:07 +02:00
|
|
|
Tr *i18n.Localizer
|
2018-08-19 15:28:29 +02:00
|
|
|
Updater *updates.Updater // may only need this on the Gui
|
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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func newDevelopmentLogger(config config.AppConfigurer) *logrus.Logger {
|
2018-08-26 07:46:18 +02:00
|
|
|
log := logrus.New()
|
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"
|
2018-08-26 07:46:18 +02:00
|
|
|
if config.GetDebug() {
|
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{}
|
|
|
|
|
2018-08-28 10:01:53 +02:00
|
|
|
if config.GetUserConfig().GetString("reporting") == "on" {
|
|
|
|
// 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(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-02 17:18:33 +02:00
|
|
|
// Setup bootstrap a new application
|
|
|
|
func Setup(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-31 10:43:54 +02:00
|
|
|
app.OSCommand = commands.NewOSCommand(app.Log, config)
|
2018-08-14 14:12:07 +02:00
|
|
|
|
2018-08-20 21:04:04 +02:00
|
|
|
app.Tr = i18n.NewLocalizer(app.Log)
|
2018-08-14 14:12:07 +02:00
|
|
|
|
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
|
|
|
}
|
2018-09-07 01:41:15 +02:00
|
|
|
app.GitCommand, err = commands.NewGitCommand(app.Log, app.OSCommand, app.Tr)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes any resources
|
|
|
|
func (app *App) Close() error {
|
|
|
|
for _, closer := range app.closers {
|
|
|
|
err := closer.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|