refactoring the config struct

This commit is contained in:
Jesse Duffield 2021-12-29 12:03:35 +11:00
parent 01ea5813a8
commit 192a548c99
9 changed files with 26 additions and 40 deletions

View file

@ -96,13 +96,15 @@ func newLogger(config config.AppConfigurer) *logrus.Entry {
// NewApp bootstrap a new application // NewApp bootstrap a new application
func NewApp(config config.AppConfigurer, filterPath string) (*App, error) { func NewApp(config config.AppConfigurer, filterPath string) (*App, error) {
userConfig := config.GetUserConfig()
app := &App{ app := &App{
closers: []io.Closer{}, closers: []io.Closer{},
Config: config, Config: config,
} }
var err error var err error
log := newLogger(config) log := newLogger(config)
tr, err := i18n.NewTranslationSetFromConfig(log, config.GetUserConfig().Gui.Language) tr, err := i18n.NewTranslationSetFromConfig(log, userConfig.Gui.Language)
if err != nil { if err != nil {
return app, err return app, err
} }
@ -110,7 +112,7 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) {
app.Common = &common.Common{ app.Common = &common.Common{
Log: log, Log: log,
Tr: tr, Tr: tr,
UserConfig: config.GetUserConfig(), UserConfig: userConfig,
Debug: config.GetDebug(), Debug: config.GetDebug(),
} }
@ -122,7 +124,7 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) {
app.OSCommand = oscommands.NewOSCommand(app.Common) app.OSCommand = oscommands.NewOSCommand(app.Common)
app.Updater, err = updates.NewUpdater(log, config, app.OSCommand, app.Tr) app.Updater, err = updates.NewUpdater(app.Common, config, app.OSCommand)
if err != nil { if err != nil {
return app, err return app, err
} }
@ -135,7 +137,6 @@ func NewApp(config config.AppConfigurer, filterPath string) (*App, error) {
app.GitCommand, err = commands.NewGitCommand( app.GitCommand, err = commands.NewGitCommand(
app.Common, app.Common,
app.OSCommand, app.OSCommand,
app.Config,
git_config.NewStdCachedGitConfig(app.Log), git_config.NewStdCachedGitConfig(app.Log),
) )
if err != nil { if err != nil {
@ -207,7 +208,7 @@ func (app *App) setupRepo() (bool, error) {
} }
shouldInitRepo := true shouldInitRepo := true
notARepository := app.Config.GetUserConfig().NotARepository notARepository := app.UserConfig.NotARepository
if notARepository == "prompt" { if notARepository == "prompt" {
// Offer to initialize a new repository in current directory. // Offer to initialize a new repository in current directory.
fmt.Print(app.Tr.CreateRepo) fmt.Print(app.Tr.CreateRepo)

View file

@ -15,7 +15,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/commands/patch" "github.com/jesseduffield/lazygit/pkg/commands/patch"
"github.com/jesseduffield/lazygit/pkg/common" "github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/env" "github.com/jesseduffield/lazygit/pkg/env"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
@ -49,7 +48,6 @@ type GitCommand struct {
func NewGitCommand( func NewGitCommand(
cmn *common.Common, cmn *common.Common,
osCommand *oscommands.OSCommand, osCommand *oscommands.OSCommand,
config config.AppConfigurer,
gitConfig git_config.IGitConfig, gitConfig git_config.IGitConfig,
) (*GitCommand, error) { ) (*GitCommand, error) {
var repo *gogit.Repository var repo *gogit.Repository

View file

@ -10,8 +10,6 @@ import (
gogit "github.com/jesseduffield/go-git/v5" gogit "github.com/jesseduffield/go-git/v5"
"github.com/jesseduffield/lazygit/pkg/commands/git_config" "github.com/jesseduffield/lazygit/pkg/commands/git_config"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
@ -210,8 +208,7 @@ func TestNewGitCommand(t *testing.T) {
for _, s := range scenarios { for _, s := range scenarios {
t.Run(s.testName, func(t *testing.T) { t.Run(s.testName, func(t *testing.T) {
s.setup() s.setup()
newAppConfig := config.NewDummyAppConfig() s.test(NewGitCommand(utils.NewDummyCommon(), oscommands.NewDummyOSCommand(), git_config.NewFakeGitConfig(nil)))
s.test(NewGitCommand(utils.NewDummyLog(), oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language), newAppConfig, git_config.NewFakeGitConfig(nil)))
}) })
} }
} }

View file

@ -31,18 +31,21 @@ type AppConfig struct {
// from AppConfig and still be used by lazygit. // from AppConfig and still be used by lazygit.
type AppConfigurer interface { type AppConfigurer interface {
GetDebug() bool GetDebug() bool
// build info
GetVersion() string GetVersion() string
GetCommit() string GetCommit() string
GetBuildDate() string GetBuildDate() string
GetName() string GetName() string
GetBuildSource() string GetBuildSource() string
GetUserConfig() *UserConfig GetUserConfig() *UserConfig
GetUserConfigPaths() []string GetUserConfigPaths() []string
GetUserConfigDir() string GetUserConfigDir() string
ReloadUserConfig() error
GetAppState() *AppState GetAppState() *AppState
SaveAppState() error SaveAppState() error
ReloadUserConfig() error
ShowCommandLogOnStartup() bool
} }
// NewAppConfig makes a new app config // NewAppConfig makes a new app config
@ -255,17 +258,6 @@ func (c *AppConfig) SaveAppState() error {
return err return err
} }
// originally we could only hide the command log permanently via the config
// but now we do it via state. So we need to still support the config for the
// sake of backwards compatibility
func (c *AppConfig) ShowCommandLogOnStartup() bool {
if !c.UserConfig.Gui.ShowCommandLog {
return false
}
return !c.AppState.HideCommandLog
}
// loadAppState loads recorded AppState from file // loadAppState loads recorded AppState from file
func loadAppState() (*AppState, error) { func loadAppState() (*AppState, error) {
filepath, err := configFilePath("state.yml") filepath, err := configFilePath("state.yml")

View file

@ -4,7 +4,6 @@ import (
"github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/updates" "github.com/jesseduffield/lazygit/pkg/updates"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
) )
@ -12,7 +11,7 @@ import (
// NewDummyGui creates a new dummy GUI for testing // NewDummyGui creates a new dummy GUI for testing
func NewDummyUpdater() *updates.Updater { func NewDummyUpdater() *updates.Updater {
newAppConfig := config.NewDummyAppConfig() newAppConfig := config.NewDummyAppConfig()
dummyUpdater, _ := updates.NewUpdater(utils.NewDummyLog(), newAppConfig, oscommands.NewDummyOSCommand(), i18n.NewTranslationSet(utils.NewDummyLog(), newAppConfig.GetUserConfig().Gui.Language)) dummyUpdater, _ := updates.NewUpdater(utils.NewDummyCommon(), newAppConfig, oscommands.NewDummyOSCommand())
return dummyUpdater return dummyUpdater
} }

View file

@ -432,6 +432,7 @@ func (gui *Gui) resetState(filterPath string, reuseState bool) {
// for now the split view will always be on // for now the split view will always be on
// NewGui builds a new gui handler // NewGui builds a new gui handler
func NewGui(cmn *common.Common, gitCommand *commands.GitCommand, oSCommand *oscommands.OSCommand, config config.AppConfigurer, updater *updates.Updater, filterPath string, showRecentRepos bool) (*Gui, error) { func NewGui(cmn *common.Common, gitCommand *commands.GitCommand, oSCommand *oscommands.OSCommand, config config.AppConfigurer, updater *updates.Updater, filterPath string, showRecentRepos bool) (*Gui, error) {
gui := &Gui{ gui := &Gui{
Common: cmn, Common: cmn,
GitCommand: gitCommand, GitCommand: gitCommand,
@ -444,8 +445,12 @@ func NewGui(cmn *common.Common, gitCommand *commands.GitCommand, oSCommand *osco
RepoPathStack: []string{}, RepoPathStack: []string{},
RepoStateMap: map[Repo]*guiState{}, RepoStateMap: map[Repo]*guiState{},
CmdLog: []string{}, CmdLog: []string{},
ShowExtrasWindow: config.ShowCommandLogOnStartup(),
suggestionsAsyncHandler: tasks.NewAsyncHandler(), suggestionsAsyncHandler: tasks.NewAsyncHandler(),
// originally we could only hide the command log permanently via the config
// but now we do it via state. So we need to still support the config for the
// sake of backwards compatibility. We're making use of short circuiting here
ShowExtrasWindow: cmn.UserConfig.Gui.ShowCommandLog && !config.GetAppState().HideCommandLog,
} }
gui.resetState(filterPath, false) gui.resetState(filterPath, false)

View file

@ -73,7 +73,7 @@ func (gui *Gui) dispatchSwitchToRepo(path string, reuse bool) error {
return err return err
} }
newGitCommand, err := commands.NewGitCommand(gui.Common, gui.OSCommand, gui.Config, git_config.NewStdCachedGitConfig(gui.Log)) newGitCommand, err := commands.NewGitCommand(gui.Common, gui.OSCommand, git_config.NewStdCachedGitConfig(gui.Log))
if err != nil { if err != nil {
return err return err
} }

View file

@ -16,19 +16,17 @@ import (
"github.com/kardianos/osext" "github.com/kardianos/osext"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands" "github.com/jesseduffield/lazygit/pkg/commands/oscommands"
"github.com/jesseduffield/lazygit/pkg/common"
"github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/config"
"github.com/jesseduffield/lazygit/pkg/constants" "github.com/jesseduffield/lazygit/pkg/constants"
"github.com/jesseduffield/lazygit/pkg/i18n"
"github.com/jesseduffield/lazygit/pkg/utils" "github.com/jesseduffield/lazygit/pkg/utils"
"github.com/sirupsen/logrus"
) )
// Updater checks for updates and does updates // Updater checks for updates and does updates
type Updater struct { type Updater struct {
Log *logrus.Entry *common.Common
Config config.AppConfigurer Config config.AppConfigurer
OSCommand *oscommands.OSCommand OSCommand *oscommands.OSCommand
Tr *i18n.TranslationSet
} }
// Updaterer implements the check and update methods // Updaterer implements the check and update methods
@ -38,14 +36,11 @@ type Updaterer interface {
} }
// NewUpdater creates a new updater // NewUpdater creates a new updater
func NewUpdater(log *logrus.Entry, config config.AppConfigurer, osCommand *oscommands.OSCommand, tr *i18n.TranslationSet) (*Updater, error) { func NewUpdater(cmn *common.Common, config config.AppConfigurer, osCommand *oscommands.OSCommand) (*Updater, error) {
contextLogger := log.WithField("context", "updates")
return &Updater{ return &Updater{
Log: contextLogger, Common: cmn,
Config: config, Config: config,
OSCommand: osCommand, OSCommand: osCommand,
Tr: tr,
}, nil }, nil
} }
@ -177,7 +172,7 @@ func (u *Updater) skipUpdateCheck() bool {
return true return true
} }
userConfig := u.Config.GetUserConfig() userConfig := u.UserConfig
if userConfig.Update.Method == "never" { if userConfig.Update.Method == "never" {
u.Log.Info("Update method is set to never so we won't check for an update") u.Log.Info("Update method is set to never so we won't check for an update")
return true return true

View file

@ -18,10 +18,9 @@ func NewDummyLog() *logrus.Entry {
func NewDummyCommon() *common.Common { func NewDummyCommon() *common.Common {
tr := i18n.EnglishTranslationSet() tr := i18n.EnglishTranslationSet()
newAppConfig := config.NewDummyAppConfig()
return &common.Common{ return &common.Common{
Log: NewDummyLog(), Log: NewDummyLog(),
Tr: &tr, Tr: &tr,
UserConfig: newAppConfig.GetUserConfig(), UserConfig: config.GetDefaultConfig(),
} }
} }