Skip to content
Myr

Merge pull request #197 from magiconair/issue-195-mousetrap-win-only

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Issue #195: Compile mousetrap only on Windows

authored by Eric Paris · committed by Eric Paris · parents 11265d6, 193b182 · Browse files · 4 files+34 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+34 added−17 removed

  • other:+34 added−17 removed

4 files+34 added−17 removed

modified:cobra.go-8 removed
38 unmodified lines
39// automatic prefix matching can be a dangerous thing to automatically enable in CLI tools.40// Set this to true to enable it41var EnablePrefixMatching bool = false4243// enables an information splash screen on Windows if the CLI is started from explorer.exe.44var EnableWindowsMouseTrap bool = true4546var MousetrapHelpText string = `This is a command line tool4748You need to open cmd.exe and run it from there.49`5043//AddTemplateFunc adds a template function that's available to Usage and Help44//template generation.45func AddTemplateFunc(name string, tmplFunc interface{}) {46 templateFuncs[name] = tmplFunc
116 unmodified lines
added:command_notwin.go+5 added
1// +build !windows23package cobra45var preExecHookFn func(*Command) = nil
added:command_win.go+26 added
1// +build windows23package cobra45import (6 "os"7 "time"89 "github.com/inconshreveable/mousetrap"10)1112var preExecHookFn = preExecHook1314// enables an information splash screen on Windows if the CLI is started from explorer.exe.15var MousetrapHelpText string = `This is a command line tool1617You need to open cmd.exe and run it from there.18`1920func preExecHook(c *Command) {21 if mousetrap.StartedByExplorer() {22 c.Print(MousetrapHelpText)23 time.Sleep(5 * time.Second)24 os.Exit(1)25 }26}
modified:command.go+3 added-9 removed
19 unmodified lines
20 "fmt"21 "io"22 "os"23 "path/filepath"24 "runtime"24 "strings"26 "time"2528 "github.com/inconshreveable/mousetrap"26 flag "github.com/spf13/pflag"27)2829// Command is just that, a command for your application.
592 unmodified lines
622 if c.HasParent() {623 return c.Root().ExecuteC()624 }625629 if EnableWindowsMouseTrap && runtime.GOOS == "windows" {630 if mousetrap.StartedByExplorer() {631 c.Print(MousetrapHelpText)632 time.Sleep(5 * time.Second)633 os.Exit(1)634 }626 // windows hook627 if preExecHookFn != nil {628 preExecHookFn(c)629 }630631 // initialize help as the last point possible to allow for user632 // overriding
562 unmodified lines