Skip to content

Commit c265551

Browse files
committed
refactor sandbox command
1 parent 25b9739 commit c265551

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

cmd/root.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"fmt"
2626
"os"
2727

28+
"github.com/ks6088ts/template-go/cmd/sandbox"
2829
"github.com/spf13/cobra"
2930
"github.com/spf13/viper"
3031
)
@@ -67,6 +68,9 @@ func init() {
6768
// Cobra also supports local flags, which will only run
6869
// when this action is called directly.
6970
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
71+
72+
// Register sub commands
73+
registerSubCommands()
7074
}
7175

7276
// initConfig reads in config file and ENV variables if set.
@@ -92,3 +96,9 @@ func initConfig() {
9296
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
9397
}
9498
}
99+
100+
// registerSubCommands registers sub commands
101+
func registerSubCommands() {
102+
rootCmd.AddCommand(sandbox.GetCommand())
103+
// Add other sub commands here
104+
}

cmd/sandbox.go renamed to cmd/sandbox/sandbox.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
THE SOFTWARE.
2121
*/
22-
package cmd
22+
package sandbox
2323

2424
import (
2525
"fmt"
@@ -30,28 +30,13 @@ import (
3030
// sandboxCmd represents the sandbox command
3131
var sandboxCmd = &cobra.Command{
3232
Use: "sandbox",
33-
Short: "A brief description of your command",
34-
Long: `A longer description that spans multiple lines and likely contains examples
35-
and usage of using your command. For example:
36-
37-
Cobra is a CLI library for Go that empowers applications.
38-
This application is a tool to generate the needed files
39-
to quickly create a Cobra application.`,
33+
Short: "SandBox command",
34+
Long: `This is a sandbox command.`,
4035
Run: func(cmd *cobra.Command, args []string) {
4136
fmt.Println("sandbox called")
4237
},
4338
}
4439

45-
func init() {
46-
rootCmd.AddCommand(sandboxCmd)
47-
48-
// Here you will define your flags and configuration settings.
49-
50-
// Cobra supports Persistent Flags which will work for this command
51-
// and all subcommands, e.g.:
52-
// sandboxCmd.PersistentFlags().String("foo", "", "A help for foo")
53-
54-
// Cobra supports local flags which will only run when this command
55-
// is called directly, e.g.:
56-
// sandboxCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
40+
func GetCommand() *cobra.Command {
41+
return sandboxCmd
5742
}

0 commit comments

Comments
 (0)