Files
kopia/cli/command_mount_browse.go
Jarek Kowalski c8fcae93aa logging: refactored logging
This is mostly mechanical and changes how loggers are instantiated.

Logger is now associated with a context, passed around all methods,
(most methods had ctx, but had to add it in a few missing places).

By default Kopia does not produce any logs, but it can be overridden,
either locally for a nested context, by calling

ctx = logging.WithLogger(ctx, newLoggerFunc)

To override logs globally, call logging.SetDefaultLogger(newLoggerFunc)

This refactoring allowed removing dependency from Kopia repo
and go-logging library (the CLI still uses it, though).

It is now also possible to have all test methods emit logs using
t.Logf() so that they show up in failure reports, which should make
debugging of test failures suck less.
2020-02-25 17:24:44 -08:00

81 lines
1.6 KiB
Go

package cli
import (
"context"
"os"
"os/exec"
"github.com/pkg/errors"
"github.com/skratchdot/open-golang/open"
)
var (
mountBrowser = mountCommand.Flag("browse", "Browse mounted filesystem using the provided method").Default("OS").Enum("NONE", "WEB", "OS")
)
var mountBrowsers = map[string]func(ctx context.Context, mountPoint, addr string) error{
"NONE": nil,
"WEB": openInWebBrowser,
"OS": openInOSBrowser,
}
func browseMount(ctx context.Context, mountPoint, addr string) error {
b := mountBrowsers[*mountBrowser]
if b == nil {
waitForCtrlC()
return nil
}
return b(ctx, mountPoint, addr)
}
// nolint:unparam
func openInWebBrowser(ctx context.Context, mountPoint, addr string) error {
startWebBrowser(ctx, addr)
waitForCtrlC()
return nil
}
func openInOSBrowser(ctx context.Context, mountPoint, addr string) error {
if isWindows() {
return netUSE(ctx, mountPoint, addr)
}
startWebBrowser(ctx, addr)
waitForCtrlC()
return nil
}
func netUSE(ctx context.Context, mountPoint, addr string) error {
c := exec.Command("net", "use", mountPoint, addr) // nolint:gosec
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Stdin = os.Stdin
if err := c.Run(); err != nil {
return errors.Wrap(err, "unable to mount")
}
startWebBrowser(ctx, "x:\\")
waitForCtrlC()
c = exec.Command("net", "use", mountPoint, "/d") // nolint:gosec
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Stdin = os.Stdin
if err := c.Run(); err != nil {
return errors.Wrap(err, "unable to unmount")
}
return nil
}
func startWebBrowser(ctx context.Context, url string) {
if err := open.Start(url); err != nil {
log(ctx).Warningf("unable to start web browser: %v", err)
}
}