Merge pull request #152 from barthr/master

Refactoring improvements based on linting issues
This commit is contained in:
bradley 2018-10-26 09:00:02 -05:00 committed by GitHub
commit a39b7a3a3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 36 additions and 33 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
ctop
.idea
.idea
/vendor/

View File

@ -52,7 +52,7 @@ var ColorMap = map[string]ui.Attribute{
func InvertColorMap() {
re := regexp.MustCompile(".*.fg")
for k, _ := range ColorMap {
for k := range ColorMap {
if re.FindAllString(k, 1) != nil {
ColorMap[k] = ui.ColorBlack
}

View File

@ -13,13 +13,13 @@ var (
xdgRe = regexp.MustCompile("^XDG_*")
)
type ConfigFile struct {
type File struct {
Options map[string]string `toml:"options"`
Toggles map[string]bool `toml:"toggles"`
}
func exportConfig() ConfigFile {
c := ConfigFile{
func exportConfig() File {
c := File{
Options: make(map[string]string),
Toggles: make(map[string]bool),
}
@ -33,7 +33,7 @@ func exportConfig() ConfigFile {
}
func Read() error {
var config ConfigFile
var config File
path, err := getConfigPath()
if err != nil {

View File

@ -61,7 +61,7 @@ func UpdateSwitch(k string, val bool) {
// Toggle a boolean switch
func Toggle(k string) {
sw := GetSwitch(k)
newVal := sw.Val != true
newVal := !sw.Val
log.Noticef("config change: %s: %t -> %t", k, sw.Val, newVal)
sw.Val = newVal
//log.Errorf("ignoring toggle for non-existant switch: %s", k)

View File

@ -48,7 +48,7 @@ func (l *DockerLogs) Stream() chan models.Log {
for scanner.Scan() {
parts := strings.Split(scanner.Text(), " ")
ts := l.parseTime(parts[0])
logCh <- models.Log{ts, strings.Join(parts[1:], " ")}
logCh <- models.Log{Timestamp: ts, Message: strings.Join(parts[1:], " ")}
}
}()
@ -62,10 +62,8 @@ func (l *DockerLogs) Stream() chan models.Log {
}()
go func() {
select {
case <-l.done:
cancel()
}
<-l.done
cancel()
}()
log.Infof("log reader started for container: %s", l.id)

View File

@ -20,7 +20,7 @@ func (l *MockLogs) Stream() chan models.Log {
case <-l.done:
break
default:
logCh <- models.Log{time.Now(), mockLog}
logCh <- models.Log{Timestamp: time.Now(), Message: mockLog}
time.Sleep(250 * time.Millisecond)
}
}

View File

@ -105,7 +105,7 @@ func (cm *Docker) refresh(c *container.Container) {
c.SetMeta("created", insp.Created.Format("Mon Jan 2 15:04:05 2006"))
c.SetMeta("health", insp.State.Health.Status)
for _, env := range insp.Config.Env {
c.SetMeta("[ENV-VAR]", string(env))
c.SetMeta("[ENV-VAR]", env)
}
c.SetState(insp.State.Status)
}
@ -113,7 +113,7 @@ func (cm *Docker) refresh(c *container.Container) {
func (cm *Docker) inspect(id string) *api.Container {
c, err := cm.client.InspectContainer(id)
if err != nil {
if _, ok := err.(*api.NoSuchContainer); ok == false {
if _, ok := err.(*api.NoSuchContainer); !ok {
log.Errorf(err.Error())
}
}

View File

@ -13,6 +13,10 @@ var (
log = logging.Init()
)
const (
running = "running"
)
// Metrics and metadata representing a container
type Container struct {
models.Metrics
@ -60,12 +64,12 @@ func (c *Container) GetMeta(k string) string {
func (c *Container) SetState(s string) {
c.SetMeta("state", s)
// start collector, if needed
if s == "running" && !c.collector.Running() {
if s == running && !c.collector.Running() {
c.collector.Start()
c.Read(c.collector.Stream())
}
// stop collector, if needed
if s != "running" && c.collector.Running() {
if s != running && c.collector.Running() {
c.collector.Stop()
}
}
@ -90,18 +94,18 @@ func (c *Container) Read(stream chan models.Metrics) {
}
func (c *Container) Start() {
if c.Meta["state"] != "running" {
if c.Meta["state"] != running {
if err := c.manager.Start(); err != nil {
log.Warningf("container %s: %v", c.Id, err)
log.StatusErr(err)
return
}
c.SetState("running")
c.SetState(running)
}
}
func (c *Container) Stop() {
if c.Meta["state"] == "running" {
if c.Meta["state"] == running {
if err := c.manager.Stop(); err != nil {
log.Warningf("container %s: %v", c.Id, err)
log.StatusErr(err)
@ -119,7 +123,7 @@ func (c *Container) Remove() {
}
func (c *Container) Pause() {
if c.Meta["state"] == "running" {
if c.Meta["state"] == running {
if err := c.manager.Pause(); err != nil {
log.Warningf("container %s: %v", c.Id, err)
log.StatusErr(err)
@ -136,12 +140,12 @@ func (c *Container) Unpause() {
log.StatusErr(err)
return
}
c.SetState("running")
c.SetState(running)
}
}
func (c *Container) Restart() {
if c.Meta["state"] == "running" {
if c.Meta["state"] == running {
if err := c.manager.Restart(); err != nil {
log.Warningf("container %s: %v", c.Id, err)
log.StatusErr(err)

View File

@ -57,7 +57,5 @@ func (cg *CompactGrid) Buffer() ui.Buffer {
}
func (cg *CompactGrid) AddRows(rows ...ui.GridBufferer) {
for _, r := range rows {
cg.Rows = append(cg.Rows, r)
}
cg.Rows = append(cg.Rows, rows...)
}

View File

@ -19,7 +19,7 @@ func (row *Compact) SetIO(read int64, write int64) {
}
func (row *Compact) SetPids(val int) {
label := fmt.Sprintf("%s", strconv.Itoa(val))
label := strconv.Itoa(val)
row.Pids.Set(label)
}

View File

@ -4,6 +4,7 @@ package compact
import (
"fmt"
ui "github.com/gizak/termui"
)
@ -28,7 +29,7 @@ func calcWidth(width int) int {
for _, w := range colWidths {
width -= w
if w == 0 {
staticCols += 1
staticCols++
}
}
return (width - spacing) / staticCols

View File

@ -2,6 +2,7 @@ package logging
import (
"fmt"
"io"
"net"
"sync"
)
@ -56,13 +57,13 @@ func StopServer() {
}
}
func handler(conn net.Conn) {
func handler(wc io.WriteCloser) {
server.wg.Add(1)
defer server.wg.Done()
defer conn.Close()
defer wc.Close()
for msg := range Log.tail() {
msg = fmt.Sprintf("%s\n", msg)
conn.Write([]byte(msg))
wc.Write([]byte(msg))
}
conn.Write([]byte("bye\n"))
wc.Write([]byte("bye\n"))
}

View File

@ -77,7 +77,7 @@ func (i *Input) KeyPress(e ui.Event) {
if len(i.Data) >= i.MaxLen {
return
}
if strings.Index(input_chars, ch) > -1 {
if strings.Contains(input_chars, ch) {
i.Data += ch
i.stream <- i.Data
ui.Render(i)