Skip to content

Commit

Permalink
Colorize Pygmy output using Aurora (#289)
Browse files Browse the repository at this point in the history
* #286 - use aurora to colorize output
* #286 - gofmt cleanup
* Update aurora calls for compliance with SA1006
* Adjust goimports sorting for aurora

Signed-off-by: Karl Hepworth <karl.hepworth@gmail.com>
Co-authored-by: Karl Hepworth <karl.hepworth@gmail.com>
  • Loading branch information
twardnw and fubarhouse committed Feb 4, 2021
1 parent 5b51ce9 commit 7b28478
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 40 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
github.com/gorilla/mux v1.7.4 // indirect
github.com/imdario/mergo v0.3.11
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.3.0 // indirect
github.com/moby/term v0.0.0-20200611042045-63b9a826fb74 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
Expand Down Expand Up @@ -235,6 +237,7 @@ github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/cobra v1.1.1 h1:KfztREH0tPxJJ+geloSLaAkaPkr4ki2Er5quFV1TDo4=
github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk=
Expand Down
17 changes: 9 additions & 8 deletions service/interface/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/fubarhouse/pygmy-go/service/interface/docker"
. "github.com/logrusorgru/aurora"
)

// Setup will detect if the Service's image reference exists and will
Expand Down Expand Up @@ -62,7 +63,7 @@ func (Service *Service) Start() error {
}

if s && !Service.HostConfig.AutoRemove && !discrete {
fmt.Printf("Already running %v\n", name)
fmt.Print(Green(fmt.Sprintf("Already running %s\n", name)))
return nil
}

Expand All @@ -88,12 +89,12 @@ func (Service *Service) Start() error {

if c, err := Service.GetRunning(); c.ID != "" {
if !discrete {
fmt.Printf("Successfully started %v\n", name)
fmt.Print(Green(fmt.Sprintf("Successfully started %s\n", name)))
return nil
}
if err != nil {
// We cannot guarantee this container is running at this point if it is to be removed.
return fmt.Errorf("Failed to run %v: %v\n", name, err)
return fmt.Errorf(Sprintf(Red("Failed to run %s: %s\n"), Red(name), (err)))
}
}

Expand Down Expand Up @@ -154,17 +155,17 @@ func (Service *Service) Clean() error {
name := strings.TrimLeft(container.Names[0], "/")
if e := docker.DockerKill(container.ID); e == nil {
if !Service.HostConfig.AutoRemove {
fmt.Printf("Successfully killed %v\n", name)
fmt.Print(Green(fmt.Sprintf("Successfully killed %s\n", name)))
}
}
if e := docker.DockerStop(container.ID); e == nil {
if !Service.HostConfig.AutoRemove {
fmt.Printf("Successfully stopped %v\n", name)
fmt.Print(Green(fmt.Sprintf("Successfully stopped %s\n", name)))
}
}
if e := docker.DockerRemove(container.ID); e != nil {
if !Service.HostConfig.AutoRemove {
fmt.Printf("Successfully removed %v\n", name)
fmt.Print(Green(fmt.Sprintf("Successfully removed %s\n", name)))
}
}
}
Expand All @@ -186,7 +187,7 @@ func (Service *Service) Stop() error {
container, err := Service.GetRunning()
if err != nil {
if !discrete {
fmt.Printf("Not running %v\n", name)
fmt.Print(Red(fmt.Sprintf("Not running %s\n", name)))
}
return nil
}
Expand All @@ -196,7 +197,7 @@ func (Service *Service) Stop() error {
if e := docker.DockerRemove(container.ID); e == nil {
if !discrete {
containerName := strings.Trim(name, "/")
fmt.Printf("Successfully removed %v\n", containerName)
fmt.Print(Green(fmt.Sprintf("Successfully removed %s\n", containerName)))
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions service/library/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/fubarhouse/pygmy-go/service/interface/docker"
. "github.com/logrusorgru/aurora"
)

// Clean will forcibly kill and remove all of pygmy's containers in the daemon
Expand All @@ -28,12 +29,12 @@ func Clean(c Config) {
if target {
err := docker.DockerKill(Container.ID)
if err == nil {
fmt.Printf("Successfully killed %v.\n", Container.Names[0])
fmt.Print(Green(fmt.Sprintf("Successfully killed %s\n", Container.Names[0])))
}

err = docker.DockerRemove(Container.ID)
if err == nil {
fmt.Printf("Successfully removed %v.\n", Container.Names[0])
fmt.Print(Green(fmt.Sprintf("Successfully removed %s\n", Container.Names[0])))
}
}
}
Expand All @@ -49,9 +50,9 @@ func Clean(c Config) {
fmt.Println(e)
}
if s, _ := docker.DockerNetworkStatus(NetworksToClean[n]); !s {
fmt.Printf("Successfully removed network %v\n", NetworksToClean[n])
fmt.Print(Green(fmt.Sprintf("Successfully removed network %s\n", NetworksToClean[n])))
} else {
fmt.Printf("Network %v was not removed\n", NetworksToClean[n])
fmt.Print(Red(fmt.Sprintf("Successfully started %s\n", NetworksToClean[n])))
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions service/library/down.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/fubarhouse/pygmy-go/service/interface/docker"
. "github.com/logrusorgru/aurora"
)

// Down will bring pygmy down safely
Expand Down Expand Up @@ -35,9 +36,9 @@ func Down(c Config) {
fmt.Println(e)
}
if s, _ := docker.DockerNetworkStatus(network); !s {
fmt.Printf("Successfully removed network %v\n", network)
fmt.Print(Green(fmt.Sprintf("Successfully removed network %s\n", network)))
} else {
fmt.Printf("Network %v was not removed", network)
fmt.Print(Red(fmt.Sprintf("Network %s was not removed\n", network)))
}
}

Expand Down
25 changes: 15 additions & 10 deletions service/library/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/fubarhouse/pygmy-go/service/endpoint"
"github.com/fubarhouse/pygmy-go/service/interface/docker"
"github.com/fubarhouse/pygmy-go/service/resolv"
. "github.com/logrusorgru/aurora"
)

// Status will show the state of all the things Pygmy manages.
Expand All @@ -18,7 +19,11 @@ func Status(c Config) {

if len(checks) > 0 {
for _, check := range checks {
fmt.Println(check.Message)
if check.State {
fmt.Println(Green(check.Message))
} else {
fmt.Println(Red(check.Message))
}
}
fmt.Println()
}
Expand All @@ -38,9 +43,9 @@ func Status(c Config) {
}
if enabled && !discrete && name != "" {
if s, _ := Service.Status(); s {
fmt.Printf("[*] %v: Running as container %v\n", name, name)
fmt.Print(Green(fmt.Sprintf("[*] %s: Running as container %s", name, name)))
} else {
fmt.Printf("[ ] %v is not running\n", name)
fmt.Print(Red(fmt.Sprintf("[ ] %s is not running\n", name)))
}
}
}
Expand All @@ -53,35 +58,35 @@ func Status(c Config) {
name, _ := Service.GetFieldString("name")
discrete, _ := Service.GetFieldBool("discrete")
if !discrete {
fmt.Printf("[ ] %v is not running\n", name)
fmt.Print(Red(fmt.Sprintf("[ ] %s is not running\n", name)))
}
}
}

for _, Network := range c.Networks {
for _, Container := range Network.Containers {
if x, _ := docker.DockerNetworkConnected(Network.Name, Container.Name); !x {
fmt.Printf("[ ] %v is not connected to network %v\n", Container.Name, Network.Name)
fmt.Print(Red(fmt.Sprintf("[ ] %s is not connected to network %s\n", Container.Name, Network.Name)))
} else {
fmt.Printf("[*] %v is connected to network %v\n", Container.Name, Network.Name)
fmt.Print(Green(fmt.Sprintf("[*] %s is connected to network %s\n", Container.Name, Network.Name)))
}
}
}

for _, resolver := range c.Resolvers {
r := resolv.Resolv{Name: resolver.Name, Data: resolver.Data, Folder: resolver.Folder, File: resolver.File}
if s := r.Status(); s {
fmt.Printf("[*] Resolv %v is properly connected\n", resolver.Name)
fmt.Print(Green(fmt.Sprintf("[*] Resolv %v is properly connected\n", resolver.Name)))
} else {
fmt.Printf("[ ] Resolv %v is not properly connected\n", resolver.Name)
fmt.Print(Red(fmt.Sprintf("[ ] Resolv %v is not properly conected\n", resolver.Name)))
}
}

for _, volume := range c.Volumes {
if s, _ := docker.DockerVolumeExists(volume); s {
fmt.Printf("[*] Volume %v has been created\n", volume.Name)
fmt.Print(Green(fmt.Sprintf("[*] Volume %s has been created\n", volume.Name)))
} else {
fmt.Printf("[ ] Volume %v has not been created\n", volume.Name)
fmt.Print(Green(fmt.Sprintf("[ ] Volume %s has not ben created\n", volume.Name)))
}
}

Expand Down
15 changes: 8 additions & 7 deletions service/library/up.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/fubarhouse/pygmy-go/service/endpoint"
"github.com/fubarhouse/pygmy-go/service/interface/docker"
. "github.com/logrusorgru/aurora"
)

// Up will bring Pygmy up.
Expand All @@ -32,12 +33,12 @@ func Up(c Config) {
if s, _ := docker.DockerVolumeExists(volume); !s {
_, err := docker.DockerVolumeCreate(volume)
if err == nil {
fmt.Printf("Created volume %v\n", volume.Name)
fmt.Print(Green(fmt.Sprintf("Created volume %s\n", volume.Name)))
} else {
fmt.Println(err)
}
} else {
fmt.Printf("Already created volume %v\n", volume.Name)
fmt.Print(Green(fmt.Sprintf("Already created volume %s\n", volume.Name)))
}
}

Expand Down Expand Up @@ -93,9 +94,9 @@ func Up(c Config) {
netVal, _ := docker.DockerNetworkStatus(Network.Name)
if !netVal {
if err := NetworkCreate(Network); err == nil {
fmt.Printf("Successfully created network %v\n", Network.Name)
fmt.Print(Green(fmt.Sprintf("Successfully created network %s\n", Network.Name)))
} else {
fmt.Printf("Could not create network %v\n", Network.Name)
fmt.Print(Red(fmt.Sprintf("Could not create network %s\n", Network.Name)))
}
}
}
Expand All @@ -109,15 +110,15 @@ func Up(c Config) {
if Network, _ := service.GetFieldString("network"); Network != "" && nameErr == nil {
if s, _ := docker.DockerNetworkConnected(Network, name); !s {
if s := NetworkConnect(Network, name); s == nil {
fmt.Printf("Successfully connected %v to %v\n", name, Network)
fmt.Print(Green(fmt.Sprintf("Successfully connected %s to %s\n", name, Network)))
} else {
discrete, _ := service.GetFieldBool("discrete")
if !discrete {
fmt.Printf("Could not connect %v to %v\n", name, Network)
fmt.Print(Red(fmt.Sprintf("Could not connect %s to %s\n", name, Network)))
}
}
} else {
fmt.Printf("Already connected %v to %v\n", name, Network)
fmt.Print(Green(fmt.Sprintf("Already connected %s to %s\n", name, Network)))
}
}
}
Expand Down
20 changes: 11 additions & 9 deletions service/resolv/resolv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os/exec"
"runtime"
"strings"

. "github.com/logrusorgru/aurora"
)

// run will run a shell command and is not exported.
Expand Down Expand Up @@ -38,7 +40,7 @@ func (resolv Resolv) Configure() {
return
}
if resolv.Status() {
fmt.Printf("Already configured resolvr %v\n", resolv.Name)
fmt.Print(Green(fmt.Sprintf("Already configured resolvr %s\n", resolv.Name)))
} else {
fullPath := fmt.Sprintf("%v%v%v", resolv.Folder, string(os.PathSeparator), resolv.File)
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
Expand Down Expand Up @@ -107,16 +109,16 @@ func (resolv Resolv) Configure() {
if runtime.GOOS == "darwin" {
ifConfig := exec.Command("/bin/sh", "-c", "sudo ifconfig lo0 alias 172.16.172.16")
if err := ifConfig.Run(); err != nil {
fmt.Println("error creating loopback UP alias")
fmt.Println(Sprintf(Red("error creating loopback UP alias")))
}
killAll := exec.Command("/bin/sh", "-c", "sudo killall mDNSResponder")
if err := killAll.Run(); err != nil {
fmt.Println("error restarting mDNSResponder")
fmt.Println(Sprintf(Red("error restarting mDNSResponder")))
}
}

if resolv.Status() {
fmt.Printf("Successfully configured resolvr %v\n", resolv.Name)
fmt.Print(Green(fmt.Sprintf("Successfully configured resolvr %s\n", resolv.Name)))
}
}
}
Expand Down Expand Up @@ -162,7 +164,7 @@ func (resolv Resolv) Clean() {
fmt.Println(err)
}
if !resolv.statusFile() {
fmt.Println("Successfully removed resolver file")
fmt.Println(Sprintf(Green("Successfully removed resolver file")))
}
}

Expand All @@ -173,20 +175,20 @@ func (resolv Resolv) Clean() {
ifConfig := exec.Command("/bin/sh", "-c", "sudo ifconfig lo0 -alias 172.16.172.16")
err := ifConfig.Run()
if err != nil {
fmt.Println("error removing loopback UP alias", err)
fmt.Println(Sprintf(Red("error removing loopback UP alias"), Red(err)))
} else {
if !resolv.statusNet() {
fmt.Println("Successfully removed loopback alias IP.")
fmt.Println(Sprintf(Green("Successfully removed loopback alias IP.")))
}
}
}

killAll := exec.Command("/bin/sh", "-c", "sudo killall mDNSResponder")
err := killAll.Run()
if err != nil {
fmt.Println("error restarting mDNSResponder")
fmt.Println(Sprintf(Red("error restarting mDNSResponder")))
} else {
fmt.Println("Successfully restarted mDNSResponder")
fmt.Println(Sprintf(Green("Successfully restarted mDNSResponder")))
}
}
}
Expand Down

0 comments on commit 7b28478

Please sign in to comment.