Skip to content

Commit

Permalink
make small enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
mhewedy committed Dec 14, 2020
1 parent 7e223e3 commit fc9c24b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 13 deletions.
10 changes: 6 additions & 4 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"github.com/mhewedy/vermin/log"
"github.com/mhewedy/vermin/progress"
"os"
"os/exec"
Expand Down Expand Up @@ -97,11 +98,12 @@ func prepend(x []string, y string) []string {
}

func (c *Cmd) log() {
if _, ok := os.LookupEnv("VERMIN_DEBUG_CMD"); ok {
fmt.Print("$ ", c.Command, " ")
if log.IsDebugEnabled() {
buf := bytes.NewBufferString("")
_, _ = fmt.Fprint(buf, "$ ", c.Command, " ")
for _, arg := range c.Args {
fmt.Print(arg, " ")
_, _ = fmt.Fprint(buf, arg, " ")
}
fmt.Println()
log.Debug(buf.String())
}
}
6 changes: 1 addition & 5 deletions command/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ var restartCmd = &cobra.Command{
Long: `Restart one or more VMs`,
Run: func(cmd *cobra.Command, args []string) {
for _, vmName := range args {
if err := vms.Stop(normalizeVmName(vmName)); err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := vms.Start(normalizeVmName(vmName)); err != nil {
if err := vms.Restart(normalizeVmName(vmName)); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down
2 changes: 1 addition & 1 deletion images/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
format = "%-40s%-10s\n"
format = "%-30s%-10s\n"
header = fmt.Sprintf(format, "IMAGE NAME", "DISK")
)

Expand Down
13 changes: 11 additions & 2 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,21 @@ import (
"time"
)

func IsDebugEnabled() bool {
_, ok := os.LookupEnv("VERMIN_DEBUG")
return ok
}

func Debug(format string, a ...interface{}) {
if _, ok := os.LookupEnv("VERMIN_DEBUG"); ok {
Info(format, a)
if IsDebugEnabled() {
Info("DEBUG: "+format, a...)
}
}

func Error(format string, a ...interface{}) {
Info("ERROR: "+format, a...)
}

func Info(format string, a ...interface{}) {
year, month, _ := time.Now().Date()
logFilePath := filepath.Join(db.BaseDir, fmt.Sprintf("log_%d-%d.log", year, month))
Expand Down
2 changes: 1 addition & 1 deletion vms/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

var (
format = "%-15s%-40s%-10s%-10s%-13s%s\n"
format = "%-15s%-30s%-10s%-10s%-13s%s\n"
header = fmt.Sprintf(format, "VM NAME", "IMAGE", "CPUS", "MEM", "DISK", "TAGS")
)

Expand Down
7 changes: 7 additions & 0 deletions vms/vms.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ func Stop(vmName string) error {
return nil
}

func Restart(vmName string) error {
if err := Stop(vmName); err != nil {
return nil
}
return start(vmName)
}

func SecureShell(vmName string) error {
if err := checkRunningVM(vmName); err != nil {
return err
Expand Down

0 comments on commit fc9c24b

Please sign in to comment.