Skip to content

Commit

Permalink
Merge pull request bcicen#152 from barthr/master
Browse files Browse the repository at this point in the history
Refactoring improvements based on linting issues
  • Loading branch information
bcicen authored Oct 26, 2018
2 parents 3405d19 + 77f5e6b commit a39b7a3
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 33 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ctop
.idea
.idea
/vendor/
2 changes: 1 addition & 1 deletion colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions config/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
Expand All @@ -33,7 +33,7 @@ func exportConfig() ConfigFile {
}

func Read() error {
var config ConfigFile
var config File

path, err := getConfigPath()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion config/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions connector/collector/docker_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:], " ")}
}
}()

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion connector/collector/mock_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions connector/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ 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)
}

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())
}
}
Expand Down
20 changes: 12 additions & 8 deletions container/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ var (
log = logging.Init()
)

const (
running = "running"
)

// Metrics and metadata representing a container
type Container struct {
models.Metrics
Expand Down Expand Up @@ -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()
}
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions cwidgets/compact/grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}
2 changes: 1 addition & 1 deletion cwidgets/compact/setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion cwidgets/compact/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package compact

import (
"fmt"

ui "github.com/gizak/termui"
)

Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions logging/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logging

import (
"fmt"
"io"
"net"
"sync"
)
Expand Down Expand Up @@ -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"))
}
2 changes: 1 addition & 1 deletion widgets/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a39b7a3

Please sign in to comment.