From 7bc5e89fbaa5c613b8853ff7b69fae570bd0b56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 9 Apr 2018 18:38:21 +0200 Subject: [PATCH] commands: Make more commands non-global See #4598 --- commands/benchmark.go | 8 ++--- commands/check.go | 19 +++++++++-- commands/hugo.go | 2 +- commands/limit_darwin.go | 69 ++++++++++++++++++++++++---------------- 4 files changed, 63 insertions(+), 35 deletions(-) diff --git a/commands/benchmark.go b/commands/benchmark.go index a31f679e9..ae5d436e8 100644 --- a/commands/benchmark.go +++ b/commands/benchmark.go @@ -25,10 +25,6 @@ import ( var _ cmder = (*benchmarkCmd)(nil) -type cmder interface { - getCommand() *cobra.Command -} - type benchmarkCmd struct { benchmarkTimes int cpuProfileFile string @@ -37,6 +33,10 @@ type benchmarkCmd struct { cmd *cobra.Command } +type cmder interface { + getCommand() *cobra.Command +} + func (c *benchmarkCmd) getCommand() *cobra.Command { return c.cmd } diff --git a/commands/check.go b/commands/check.go index e5dbc1ffa..5812bb6aa 100644 --- a/commands/check.go +++ b/commands/check.go @@ -17,7 +17,20 @@ import ( "github.com/spf13/cobra" ) -var checkCmd = &cobra.Command{ - Use: "check", - Short: "Contains some verification checks", +var _ cmder = (*checkCmd)(nil) + +type checkCmd struct { + cmd *cobra.Command +} + +func newCheckCmd() *checkCmd { + return &checkCmd{cmd: &cobra.Command{ + Use: "check", + Short: "Contains some verification checks", + }, + } +} + +func (c *checkCmd) getCommand() *cobra.Command { + return c.cmd } diff --git a/commands/hugo.go b/commands/hugo.go index e6c2771fa..5db5239ea 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -197,7 +197,7 @@ func AddCommands() { HugoCmd.AddCommand(versionCmd) HugoCmd.AddCommand(envCmd) HugoCmd.AddCommand(configCmd) - HugoCmd.AddCommand(checkCmd) + HugoCmd.AddCommand(newCheckCmd().getCommand()) HugoCmd.AddCommand(newBenchmarkCmd().getCommand()) HugoCmd.AddCommand(convertCmd) HugoCmd.AddCommand(newCmd) diff --git a/commands/limit_darwin.go b/commands/limit_darwin.go index 9246f4497..e35c24de4 100644 --- a/commands/limit_darwin.go +++ b/commands/limit_darwin.go @@ -33,39 +33,54 @@ import ( jww "github.com/spf13/jwalterweatherman" ) -func init() { - checkCmd.AddCommand(limit) +var _ cmder = (*limitCmd)(nil) + +type limitCmd struct { + cmd *cobra.Command } -var limit = &cobra.Command{ - Use: "ulimit", - Short: "Check system ulimit settings", - Long: `Hugo will inspect the current ulimit settings on the system. +func newLimitCmd() *limitCmd { + ccmd := &cobra.Command{ + Use: "ulimit", + Short: "Check system ulimit settings", + Long: `Hugo will inspect the current ulimit settings on the system. This is primarily to ensure that Hugo can watch enough files on some OSs`, - RunE: func(cmd *cobra.Command, args []string) error { - var rLimit syscall.Rlimit - err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) - if err != nil { - return newSystemError("Error Getting Rlimit ", err) - } + RunE: func(cmd *cobra.Command, args []string) error { + var rLimit syscall.Rlimit + err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) + if err != nil { + return newSystemError("Error Getting Rlimit ", err) + } - jww.FEEDBACK.Println("Current rLimit:", rLimit) + jww.FEEDBACK.Println("Current rLimit:", rLimit) - jww.FEEDBACK.Println("Attempting to increase limit") - rLimit.Max = 999999 - rLimit.Cur = 999999 - err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) - if err != nil { - return newSystemError("Error Setting rLimit ", err) - } - err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) - if err != nil { - return newSystemError("Error Getting rLimit ", err) - } - jww.FEEDBACK.Println("rLimit after change:", rLimit) + jww.FEEDBACK.Println("Attempting to increase limit") + rLimit.Max = 999999 + rLimit.Cur = 999999 + err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit) + if err != nil { + return newSystemError("Error Setting rLimit ", err) + } + err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit) + if err != nil { + return newSystemError("Error Getting rLimit ", err) + } + jww.FEEDBACK.Println("rLimit after change:", rLimit) - return nil - }, + return nil + }, + } + + return &limitCmd{cmd: ccmd} +} + +func (c *limitCmd) getCommand() *cobra.Command { + return c.cmd +} + +func init() { + // TODO(bep) cli refactor + //checkCmdOld.AddCommand(limit) } func tweakLimit() {