Skip to content

Commit

Permalink
Refactor codebase to use existing log API via RigLogger.Channel.*
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Ross committed Nov 16, 2017
1 parent 75ca4c5 commit fcfb442
Show file tree
Hide file tree
Showing 21 changed files with 187 additions and 185 deletions.
12 changes: 6 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ Here are a few conventions:
* **Starting a task that could take more than 5 seconds:**
* `cmd.out.Spin("Preparing the sauce")`
* **Use the correct method to log operational results: (Pick one)**
* `cmd.out.Success("Sauce is Ready.")`
* `cmd.out.Warn("Sauce is burnt on the bottom.")`
* `cmd.out.Oops("Discard this sauce and try again.")`
* `cmd.out.Info("Sauce is Ready.")`
* `cmd.out.Warning("Sauce is burnt on the bottom.")`
* `cmd.out.Error("Discard this sauce and try again.")`
* **Going to send some contextual notes to the user**:
1. `cmd.out.NoSpin()` if currently using the spinner.
2. `cmd.out.Status("Sauce exists.")`
3. `cmd.out.Note("This is a list item.")`
4. `cmd.out.Details("The ingredients of the sauce include tomato, salt, black pepper, garlic...")`
4. `cmd.out.Verbose("The ingredients of the sauce include tomato, salt, black pepper, garlic...")`
* **Command has executed and is successful. We do not want a notification:**
```
cmd.out.Success("Enjoy your dinner.")
cmd.out.Info("Enjoy your dinner.")
return cmd.Success("")
```
* **Command has executed and is successful. Get a notification too!**
Expand All @@ -46,7 +46,7 @@ Here are a few conventions:
* **Command failed:**
```
message := "Cooking sauce is hard, we failed"
cmd.out.Oops(fmt.Sprintf("%s: %s", message, err.Error()))
cmd.out.Error(fmt.Sprintf("%s: %s", message, err.Error()))
return cmd.Error(message)
```

Expand Down
4 changes: 2 additions & 2 deletions commands/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (cmd *BaseCommand) Before(c *cli.Context) error {
func (cmd *BaseCommand) Success(message string) error {
// Handle success messaging.
if message != "" {
cmd.out.Success(message)
cmd.out.Info(message)
util.NotifySuccess(cmd.context, message)
}

Expand Down Expand Up @@ -73,6 +73,6 @@ func (cmd *BaseCommand) NewContext(name string, flags []cli.Flag, parent *cli.Co
// SetContextFlag set a flag on the provided context
func (cmd *BaseCommand) SetContextFlag(ctx *cli.Context, name string, value string) {
if err := ctx.Set(name, value); err != nil {
cmd.out.Error.Fatal(err)
cmd.out.Channel.Error.Fatal(err)
}
}
12 changes: 6 additions & 6 deletions commands/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (cmd *Dashboard) Commands() []cli.Command {
// Run executes the `rig dashboard` command
func (cmd *Dashboard) Run(ctx *cli.Context) error {
if cmd.machine.IsRunning() || util.SupportsNativeDocker() {
cmd.out.Success("Launching Dashboard")
cmd.out.Info("Launching Dashboard")
err := cmd.LaunchDashboard(cmd.machine)
if err != nil {
// Success may be presumed to only execute once per command execution.
Expand All @@ -54,16 +54,16 @@ func (cmd *Dashboard) LaunchDashboard(machine Machine) error {
// except to indicate the age of the image before update in the next section.
_, seconds, err := util.ImageOlderThan(dashboardImageName, 86400*30)
if err == nil {
cmd.out.Details(fmt.Sprintf("Local copy of the dashboardImageName '%s' was originally published %0.2f days ago.", dashboardImageName, seconds/86400))
cmd.out.Verbose(fmt.Sprintf("Local copy of the dashboardImageName '%s' was originally published %0.2f days ago.", dashboardImageName, seconds/86400))
}

// Updating the dashboard is rarely of interest to users so uses verbose logging.
// Per our user interaction practices, we would normally use a spinner here.
cmd.out.Details(fmt.Sprintf("Attempting to update %s", dashboardImageName))
cmd.out.Verbose(fmt.Sprintf("Attempting to update %s", dashboardImageName))
if err := util.StreamCommand("docker", "pull", dashboardImageName); err != nil {
cmd.out.Details("Failed to update dashboard image. Will use local cache if available.")
cmd.out.Verbose("Failed to update dashboard image. Will use local cache if available.")
} else {
cmd.out.Details("Successfully updated dashboard.")
cmd.out.Verbose("Successfully updated dashboard.")
}

dockerAPIVersion, _ := util.GetDockerServerAPIVersion()
Expand All @@ -85,7 +85,7 @@ func (cmd *Dashboard) LaunchDashboard(machine Machine) error {
} else if util.IsWindows() {
util.Command("start", "http://dashboard.outrigger.vm").Run()
} else {
cmd.out.Success("Outrigger Dashboard is now available at http://dashboard.outrigger.vm")
cmd.out.Info("Outrigger Dashboard is now available at http://dashboard.outrigger.vm")
}

return nil
Expand Down
8 changes: 4 additions & 4 deletions commands/data_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func (cmd *DataBackup) Run(c *cli.Context) error {
backupDir := c.String("backup-dir")
backupFile := fmt.Sprintf("%s%c%s.tgz", backupDir, os.PathSeparator, cmd.machine.Name)
if _, err := os.Stat(backupDir); err != nil {
cmd.out.Info.Printf("Creating backup directory: %s...", backupDir)
cmd.out.Info(fmt.Sprintf("Creating backup directory: %s...", backupDir))
if mkdirErr := util.Command("mkdir", "-p", backupDir).Run(); mkdirErr != nil {
cmd.out.Error.Println(mkdirErr)
cmd.out.Error(mkdirErr.Error())
return cmd.Error(fmt.Sprintf("Could not create backup directory %s", backupDir), "BACKUP-DIR-CREATE-FAILED", 12)
}
} else if _, err := os.Stat(backupFile); err == nil {
Expand All @@ -66,10 +66,10 @@ func (cmd *DataBackup) Run(c *cli.Context) error {
cmd.out.Spin(fmt.Sprintf("Backing up %s on '%s' to %s...", dataDir, cmd.machine.Name, backupFile))
archiveCmd := fmt.Sprintf("sudo tar czf - -C %s .", dataDir)
if err := util.StreamCommand("docker-machine", "ssh", cmd.machine.Name, archiveCmd, ">", backupFile); err != nil {
cmd.out.Oops(fmt.Sprintf("Backup failed: %s", err.Error()))
cmd.out.Error(fmt.Sprintf("Backup failed: %s", err.Error()))
return cmd.Error("Backup failed", "COMMAND-ERROR", 13)
}

cmd.out.Success(fmt.Sprintf("Backup complete: %s", backupFile))
cmd.out.Info(fmt.Sprintf("Backup complete: %s", backupFile))
return cmd.Success("Data Backup completed")
}
4 changes: 2 additions & 2 deletions commands/data_restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ func (cmd *DataRestore) Run(c *cli.Context) error {
// Send the archive via stdin and extract inline. Saves on disk & performance
extractCmd := fmt.Sprintf("cat %s | docker-machine ssh %s \"sudo tar xzf - -C %s\"", backupFile, cmd.machine.Name, dataDir)
if err := util.StreamCommand("bash", "-c", extractCmd); err != nil {
cmd.out.Oops(fmt.Sprintf("Data restore failed: %s", err.Error()))
cmd.out.Error(fmt.Sprintf("Data restore failed: %s", err.Error()))
return cmd.Error("Data restore failed", "COMMAND-ERROR", 13)
}

cmd.out.Success("Data restore complete")
cmd.out.Info("Data restore complete")
return cmd.Success("Data Restore was successful")
}
26 changes: 13 additions & 13 deletions commands/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func (cmd *DNS) Commands() []cli.Command {

// Run executes the `rig dns` command
func (cmd *DNS) Run(c *cli.Context) error {
cmd.out.Status("Configuring DNS")
cmd.out.Info("Configuring DNS")

if !util.SupportsNativeDocker() && !cmd.machine.IsRunning() {
return cmd.Error(fmt.Sprintf("Machine '%s' is not running.", cmd.machine.Name), "MACHINE-STOPPED", 12)
}

if err := cmd.StartDNS(cmd.machine, c.String("nameservers")); err != nil {
cmd.out.Oops("DNS is ready")
cmd.out.Error("DNS is ready")
return cmd.Error(err.Error(), "DNS-SETUP-FAILED", 13)
}

Expand All @@ -65,7 +65,7 @@ func (cmd *DNS) ConfigureRoutes(machine Machine) {
cmd.configureWindowsRoutes(machine)
}

cmd.out.Success("Local networking is ready")
cmd.out.Info("Local networking is ready")
}

// ConfigureMac configures DNS resolution and network routing
Expand All @@ -79,12 +79,12 @@ func (cmd *DNS) configureMacRoutes(machine Machine) {
util.StreamCommand("sudo", "route", "-n", "add", "172.17.0.0/16", machineIP)
if _, err := os.Stat("/usr/sbin/discoveryutil"); err == nil {
// Put this here for people running OS X 10.10.0 to 10.10.3 (oy vey.)
cmd.out.Verbose.Println("Restarting discoveryutil to flush DNS caches")
cmd.out.Verbose("Restarting discoveryutil to flush DNS caches")
util.StreamCommand("sudo", "launchctl", "unload", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist")
util.StreamCommand("sudo", "launchctl", "load", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist")
} else {
// Reset DNS cache. We have seen this suddenly make /etc/resolver/vm work.
cmd.out.Verbose.Println("Restarting mDNSResponder to flush DNS caches")
cmd.out.Verbose("Restarting mDNSResponder to flush DNS caches")
util.StreamCommand("sudo", "killall", "-HUP", "mDNSResponder")
}
}
Expand All @@ -94,7 +94,7 @@ func (cmd *DNS) removeHostFilter(ipAddr string) {
// #1: route -n get <machineIP> to find the interface name
routeData, err := util.Command("route", "-n", "get", ipAddr).CombinedOutput()
if err != nil {
cmd.out.Warning.Println("Unable to determine bridge interface to remove hostfilter")
cmd.out.Warning("Unable to determine bridge interface to remove hostfilter")
return
}
ifaceRegexp := regexp.MustCompile(`interface:\s(\w+)`)
Expand All @@ -103,7 +103,7 @@ func (cmd *DNS) removeHostFilter(ipAddr string) {
// #2: ifconfig <interface name> to get the details
ifaceData, err := util.Command("ifconfig", iface).CombinedOutput()
if err != nil {
cmd.out.Warning.Println("Unable to determine member to remove hostfilter")
cmd.out.Warning("Unable to determine member to remove hostfilter")
return
}
memberRegexp := regexp.MustCompile(`member:\s(\w+)\s`)
Expand Down Expand Up @@ -163,14 +163,14 @@ func (cmd *DNS) StartDNS(machine Machine, nameservers string) error {
} else if util.IsWindows() {
resolverReturn = cmd.configureWindowsResolver(machine)
}
cmd.out.Success("DNS resolution is ready")
cmd.out.Info("DNS resolution is ready")

return resolverReturn
}

// configureMacResolver configures DNS resolution and network routing
func (cmd *DNS) configureMacResolver(machine Machine) error {
cmd.out.Details("Configuring DNS resolution for macOS")
cmd.out.Verbose("Configuring DNS resolution for macOS")
bridgeIP := machine.GetBridgeIP()

if err := util.Command("sudo", "mkdir", "-p", "/etc/resolver").Run(); err != nil {
Expand All @@ -181,20 +181,20 @@ func (cmd *DNS) configureMacResolver(machine Machine) error {
}
if _, err := os.Stat("/usr/sbin/discoveryutil"); err == nil {
// Put this here for people running OS X 10.10.0 to 10.10.3 (oy vey.)
cmd.out.Details("Restarting discoveryutil to flush DNS caches")
cmd.out.Verbose("Restarting discoveryutil to flush DNS caches")
util.StreamCommand("sudo", "launchctl", "unload", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist")
util.StreamCommand("sudo", "launchctl", "load", "-w", "/System/Library/LaunchDaemons/com.apple.discoveryd.plist")
} else {
// Reset DNS cache. We have seen this suddenly make /etc/resolver/vm work.
cmd.out.Details("Restarting mDNSResponder to flush DNS caches")
cmd.out.Verbose("Restarting mDNSResponder to flush DNS caches")
util.StreamCommand("sudo", "killall", "-HUP", "mDNSResponder")
}
return nil
}

// configureLinuxResolver configures DNS resolution
func (cmd *DNS) configureLinuxResolver() error {
cmd.out.Details("Configuring DNS resolution for linux")
cmd.out.Verbose("Configuring DNS resolution for linux")
bridgeIP, err := util.GetBridgeIP()
if err != nil {
return err
Expand Down Expand Up @@ -223,7 +223,7 @@ func (cmd *DNS) configureLinuxResolver() error {
// configureWindowsResolver configures DNS resolution and network routing
func (cmd *DNS) configureWindowsResolver(machine Machine) error {
// TODO: Figure out Windows resolver configuration
cmd.out.Details("TODO: Configuring DNS resolution for windows")
cmd.out.Verbose("TODO: Configuring DNS resolution for windows")
return nil
}

Expand Down
Loading

0 comments on commit fcfb442

Please sign in to comment.