Issue #195: Compile mousetrap only on Windows
* Create command_win.go and command_notwin.go for windows only code * Move call to mousetrap hook into separate preExecHook() function
This commit is contained in:
parent
b167d9beaa
commit
193b182195
8
cobra.go
8
cobra.go
@ -40,14 +40,6 @@ var initializers []func()
|
|||||||
// Set this to true to enable it
|
// Set this to true to enable it
|
||||||
var EnablePrefixMatching bool = false
|
var EnablePrefixMatching bool = false
|
||||||
|
|
||||||
// enables an information splash screen on Windows if the CLI is started from explorer.exe.
|
|
||||||
var EnableWindowsMouseTrap bool = true
|
|
||||||
|
|
||||||
var MousetrapHelpText string = `This is a command line tool
|
|
||||||
|
|
||||||
You need to open cmd.exe and run it from there.
|
|
||||||
`
|
|
||||||
|
|
||||||
//AddTemplateFunc adds a template function that's available to Usage and Help
|
//AddTemplateFunc adds a template function that's available to Usage and Help
|
||||||
//template generation.
|
//template generation.
|
||||||
func AddTemplateFunc(name string, tmplFunc interface{}) {
|
func AddTemplateFunc(name string, tmplFunc interface{}) {
|
||||||
|
12
command.go
12
command.go
@ -21,11 +21,8 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/inconshreveable/mousetrap"
|
|
||||||
flag "github.com/spf13/pflag"
|
flag "github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -626,12 +623,9 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
|
|||||||
return c.Root().ExecuteC()
|
return c.Root().ExecuteC()
|
||||||
}
|
}
|
||||||
|
|
||||||
if EnableWindowsMouseTrap && runtime.GOOS == "windows" {
|
// windows hook
|
||||||
if mousetrap.StartedByExplorer() {
|
if preExecHookFn != nil {
|
||||||
c.Print(MousetrapHelpText)
|
preExecHookFn(c)
|
||||||
time.Sleep(5 * time.Second)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// initialize help as the last point possible to allow for user
|
// initialize help as the last point possible to allow for user
|
||||||
|
5
command_notwin.go
Normal file
5
command_notwin.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
// +build !windows
|
||||||
|
|
||||||
|
package cobra
|
||||||
|
|
||||||
|
var preExecHookFn func(*Command) = nil
|
26
command_win.go
Normal file
26
command_win.go
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
// +build windows
|
||||||
|
|
||||||
|
package cobra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/inconshreveable/mousetrap"
|
||||||
|
)
|
||||||
|
|
||||||
|
var preExecHookFn = preExecHook
|
||||||
|
|
||||||
|
// enables an information splash screen on Windows if the CLI is started from explorer.exe.
|
||||||
|
var MousetrapHelpText string = `This is a command line tool
|
||||||
|
|
||||||
|
You need to open cmd.exe and run it from there.
|
||||||
|
`
|
||||||
|
|
||||||
|
func preExecHook(c *Command) {
|
||||||
|
if mousetrap.StartedByExplorer() {
|
||||||
|
c.Print(MousetrapHelpText)
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user