clean some things up

This commit is contained in:
Jesse Duffield 2019-06-09 10:03:04 +10:00
parent b65df625f4
commit 5d55939719
8 changed files with 29 additions and 50 deletions

View file

@ -20,7 +20,7 @@ type App struct {
OSCommand *commands.OSCommand OSCommand *commands.OSCommand
DockerCommand *commands.DockerCommand DockerCommand *commands.DockerCommand
Gui *gui.Gui Gui *gui.Gui
Tr i18n.TranslationSet Tr *i18n.TranslationSet
ErrorChan chan error ErrorChan chan error
} }

View file

@ -36,17 +36,21 @@ type Container struct {
Details Details Details Details
MonitoringStats bool MonitoringStats bool
} }
// RecordedStats contains both the container stats we've received from docker, and our own derived stats from those container stats. When configuring a graph, you're basically specifying the path of a value in this struct
type RecordedStats struct { type RecordedStats struct {
ClientStats ContainerStats ClientStats ContainerStats
DerivedStats DerivedStats DerivedStats DerivedStats
RecordedAt time.Time RecordedAt time.Time
} }
// DerivedStats contains some useful stats that we've calculated based on the raw container stats that we got back from docker
type DerivedStats struct { type DerivedStats struct {
CPUPercentage float64 CPUPercentage float64
MemoryPercentage float64 MemoryPercentage float64
} }
// Details is a struct containing what we get back from `docker inspect` on a container
type Details struct { type Details struct {
ID string `json:"Id"` ID string `json:"Id"`
Created time.Time `json:"Created"` Created time.Time `json:"Created"`

View file

@ -24,7 +24,7 @@ import (
type DockerCommand struct { type DockerCommand struct {
Log *logrus.Entry Log *logrus.Entry
OSCommand *OSCommand OSCommand *OSCommand
Tr i18n.TranslationSet Tr *i18n.TranslationSet
Config *config.AppConfig Config *config.AppConfig
Client *client.Client Client *client.Client
InDockerComposeProject bool InDockerComposeProject bool
@ -39,7 +39,7 @@ type DockerCommand struct {
} }
// NewDockerCommand it runs git commands // NewDockerCommand it runs git commands
func NewDockerCommand(log *logrus.Entry, osCommand *OSCommand, tr i18n.TranslationSet, config *config.AppConfig, errorChan chan error) (*DockerCommand, error) { func NewDockerCommand(log *logrus.Entry, osCommand *OSCommand, tr *i18n.TranslationSet, config *config.AppConfig, errorChan chan error) (*DockerCommand, error) {
cli, err := client.NewEnvClient() cli, err := client.NewEnvClient()
if err != nil { if err != nil {
return nil, err return nil, err
@ -56,6 +56,7 @@ func NewDockerCommand(log *logrus.Entry, osCommand *OSCommand, tr i18n.Translati
}, nil }, nil
} }
// MonitorContainerStats is a function
func (c *DockerCommand) MonitorContainerStats() { func (c *DockerCommand) MonitorContainerStats() {
go c.MonitorCLIContainerStats() go c.MonitorCLIContainerStats()
go c.MonitorClientContainerStats() go c.MonitorClientContainerStats()
@ -98,6 +99,7 @@ func (c *DockerCommand) MonitorCLIContainerStats() {
return return
} }
// MonitorClientContainerStats is a function
func (c *DockerCommand) MonitorClientContainerStats() { func (c *DockerCommand) MonitorClientContainerStats() {
// periodically loop through running containers and see if we need to create a monitor goroutine for any // periodically loop through running containers and see if we need to create a monitor goroutine for any
// every second we check if we need to spawn a new goroutine // every second we check if we need to spawn a new goroutine

View file

@ -31,12 +31,14 @@ type ComplexError struct {
frame xerrors.Frame frame xerrors.Frame
} }
// FormatError is a function
func (ce ComplexError) FormatError(p xerrors.Printer) error { func (ce ComplexError) FormatError(p xerrors.Printer) error {
p.Printf("%d %s", ce.Code, ce.Message) p.Printf("%d %s", ce.Code, ce.Message)
ce.frame.Format(p) ce.frame.Format(p)
return nil return nil
} }
// Format is a function
func (ce ComplexError) Format(f fmt.State, c rune) { func (ce ComplexError) Format(f fmt.State, c rune) {
xerrors.FormatError(ce, f, c) xerrors.FormatError(ce, f, c)
} }
@ -45,6 +47,7 @@ func (ce ComplexError) Error() string {
return fmt.Sprint(ce) return fmt.Sprint(ce)
} }
// HasErrorCode is a function
func HasErrorCode(err error, code int) bool { func HasErrorCode(err error, code int) bool {
var originalErr ComplexError var originalErr ComplexError
if xerrors.As(err, &originalErr) { if xerrors.As(err, &originalErr) {

View file

@ -59,7 +59,7 @@ type Gui struct {
SubProcess *exec.Cmd SubProcess *exec.Cmd
State guiState State guiState
Config *config.AppConfig Config *config.AppConfig
Tr i18n.TranslationSet Tr *i18n.TranslationSet
Errors SentinelErrors Errors SentinelErrors
statusManager *statusManager statusManager *statusManager
waitForIntro sync.WaitGroup waitForIntro sync.WaitGroup
@ -113,7 +113,7 @@ type guiState struct {
} }
// NewGui builds a new gui handler // NewGui builds a new gui handler
func NewGui(log *logrus.Entry, dockerCommand *commands.DockerCommand, oSCommand *commands.OSCommand, tr i18n.TranslationSet, config *config.AppConfig, errorChan chan error) (*Gui, error) { func NewGui(log *logrus.Entry, dockerCommand *commands.DockerCommand, oSCommand *commands.OSCommand, tr *i18n.TranslationSet, config *config.AppConfig, errorChan chan error) (*Gui, error) {
initialState := guiState{ initialState := guiState{
PreviousView: "services", PreviousView: "services",

View file

@ -17,7 +17,7 @@ type Localizer struct {
} }
// NewTranslationSet creates a new Localizer // NewTranslationSet creates a new Localizer
func NewTranslationSet(log *logrus.Entry) TranslationSet { func NewTranslationSet(log *logrus.Entry) *TranslationSet {
userLang := detectLanguage(jibber_jabber.DetectLanguage) userLang := detectLanguage(jibber_jabber.DetectLanguage)
log.Info("language: " + userLang) log.Info("language: " + userLang)
@ -34,7 +34,7 @@ func NewTranslationSet(log *logrus.Entry) TranslationSet {
mergo.Merge(&set, polishSet(), mergo.WithOverride) mergo.Merge(&set, polishSet(), mergo.WithOverride)
} }
return set return &set
} }
// detectLanguage extracts user language from environment // detectLanguage extracts user language from environment

View file

@ -1,31 +0,0 @@
package test
import (
"github.com/go-errors/errors"
"os"
"os/exec"
"path/filepath"
"github.com/jesseduffield/lazydocker/pkg/utils"
)
// GenerateRepo generates a repo from test/repos and changes the directory to be
// inside the newly made repo
func GenerateRepo(filename string) error {
reposDir := "/test/repos/"
testPath := utils.GetProjectRoot() + reposDir
// workaround for debian packaging
if _, err := os.Stat(testPath); os.IsNotExist(err) {
cwd, _ := os.Getwd()
testPath = filepath.Dir(filepath.Dir(cwd)) + reposDir
}
if err := os.Chdir(testPath); err != nil {
return err
}
if output, err := exec.Command("bash", filename).CombinedOutput(); err != nil {
return errors.New(string(output))
}
return os.Chdir(testPath + "repo")
}

View file

@ -50,8 +50,9 @@ func writeString(file *os.File, str string) {
} }
func localisedTitle(mApp *app.App, str string) string { func localisedTitle(mApp *app.App, str string) string {
viewTitle := strings.Title(str) + "Title" return ""
return mApp.Tr.ViewTitle // FIXME: this used to be dynamic but we can't make this dynamic again without reflection since we're now using a struct of translation strings // viewTitle := strings.Title(str) + "Title"
// return mApp.Tr.ViewTitle // FIXME: this used to be dynamic but we can't make this dynamic again without reflection since we're now using a struct of translation strings
} }
func formatTitle(title string) string { func formatTitle(title string) string {
@ -80,17 +81,17 @@ func getBindingSections(mApp *app.App) []*bindingSection {
bindingSections = addBinding(title, bindingSections, binding) bindingSections = addBinding(title, bindingSections, binding)
} }
for view, contexts := range mApp.Gui.GetContextMap() { // for view, contexts := range mApp.Gui.GetContextMap() {
for contextName, contextBindings := range contexts { // for contextName, contextBindings := range contexts {
translatedView := localisedTitle(mApp, view) // translatedView := localisedTitle(mApp, view)
translatedContextName := localisedTitle(mApp, contextName) // translatedContextName := localisedTitle(mApp, contextName)
title := fmt.Sprintf("%s (%s)", translatedView, translatedContextName) // title := fmt.Sprintf("%s (%s)", translatedView, translatedContextName)
for _, binding := range contextBindings { // for _, binding := range contextBindings {
bindingSections = addBinding(title, bindingSections, binding) // bindingSections = addBinding(title, bindingSections, binding)
} // }
} // }
} // }
return bindingSections return bindingSections
} }