Files
kopia/cli/command_mount_browse.go
Jarek Kowalski 9a6dea898b Linter upgrade to v1.30.0 (#526)
* fixed godot linter errors
* reformatted source with gofumpt
* disabled some linters
* fixed nolintlint warnings
* fixed gci warnings
* lint: fixed 'nestif' warnings
* lint: fixed 'exhaustive' warnings
* lint: fixed 'gocritic' warnings
* lint: fixed 'noctx' warnings
* lint: fixed 'wsl' warnings
* lint: fixed 'goerr113' warnings
* lint: fixed 'gosec' warnings
* lint: upgraded linter to 1.30.0
* lint: more 'exhaustive' warnings

Co-authored-by: Nick <nick@kasten.io>
2020-08-12 19:28:53 -07:00

79 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)
}
}