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

118 lines
3.0 KiB
Go
Raw Normal View History

package app
import (
"io"
"io/ioutil"
2018-08-13 13:16:21 +02:00
"os"
2018-12-08 07:54:54 +02:00
"path/filepath"
2018-08-26 07:46:18 +02:00
"github.com/heroku/rollrus"
"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"
"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"
)
// App struct
type App struct {
closers []io.Closer
Config config.AppConfigurer
2018-08-26 07:46:18 +02:00
Log *logrus.Entry
OSCommand *commands.OSCommand
GitCommand *commands.GitCommand
2018-08-13 13:16:21 +02:00
Gui *gui.Gui
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)
}
log.SetOutput(file)
2018-08-13 13:16:21 +02:00
return log
}
2018-08-26 07:46:18 +02:00
func newLogger(config config.AppConfigurer) *logrus.Entry {
var log *logrus.Logger
environment := "production"
2018-08-26 07:46:18 +02:00
if config.GetDebug() {
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
log.Formatter = &logrus.JSONFormatter{}
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) {
app := &App{
closers: []io.Closer{},
Config: config,
}
var err error
2018-08-13 13:16:21 +02:00
app.Log = newLogger(config)
app.OSCommand = commands.NewOSCommand(app.Log, config)
app.Tr = i18n.NewLocalizer(app.Log)
2018-09-07 01:41:15 +02:00
app.Updater, err = updates.NewUpdater(app.Log, config, app.OSCommand, app.Tr)
if err != nil {
2018-08-18 11:43:58 +02:00
return app, err
}
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
}
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
}