document root command and add different execute options
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||||
"github.com/charmbracelet/fang"
|
"github.com/charmbracelet/fang"
|
||||||
@@ -10,35 +10,53 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// svc is a global variable that holds beads, config and stats services.
|
||||||
|
// Must be called before executing any commands to ensure services are available.
|
||||||
var svc *service.Services
|
var svc *service.Services
|
||||||
|
|
||||||
|
// rootCmd is the base command for the CLI application.
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Short: "Project Management CLI",
|
Short: "Project Management CLI",
|
||||||
Long: `Project Management CLI for managing issues and tasks.`,
|
Long: `Project Management CLI for managing issues and tasks.`,
|
||||||
}
|
}
|
||||||
|
|
||||||
func Execute(services *service.Services) error {
|
// SetServices sets the global services variable for use in command execution.
|
||||||
SetServices(services)
|
// Must be called before executing any commands to ensure services are available.
|
||||||
return fang.Execute(context.Background(), rootCmd,
|
|
||||||
fang.WithColorSchemeFunc(fang.AnsiColorScheme),
|
|
||||||
fang.WithNotifySignal(os.Interrupt, os.Kill))
|
|
||||||
}
|
|
||||||
|
|
||||||
func SetServices(services *service.Services) {
|
func SetServices(services *service.Services) {
|
||||||
svc = services
|
svc = services
|
||||||
rootCmd.Use = svc.Config.RootCmd
|
rootCmd.Use = svc.Config.RootCmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExecuteWithArgs(args []string) error {
|
// Execute executes the root command using the fang library.
|
||||||
|
func Execute() error {
|
||||||
|
return fang.Execute(context.Background(), rootCmd,
|
||||||
|
fang.WithColorSchemeFunc(fang.AnsiColorScheme))
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExecuteArgs executes the command with the given arguments using the fang library.
|
||||||
|
func ExecuteArgs(args []string) error {
|
||||||
rootCmd.SetArgs(args)
|
rootCmd.SetArgs(args)
|
||||||
return fang.Execute(context.Background(), rootCmd,
|
return fang.Execute(context.Background(), rootCmd,
|
||||||
fang.WithColorSchemeFunc(fang.AnsiColorScheme))
|
fang.WithColorSchemeFunc(fang.AnsiColorScheme))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExecuteArgsString executes the command with the given arguments and returns the output as a string.
|
||||||
|
// This is useful for testing command outputs and used in the REPL
|
||||||
|
func ExecuteArgsString(args []string) (string, error) {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
|
||||||
|
rootCmd.SetOut(buf)
|
||||||
|
rootCmd.SetErr(buf)
|
||||||
|
rootCmd.SetArgs(args)
|
||||||
|
|
||||||
|
err := rootCmd.Execute()
|
||||||
|
|
||||||
|
return buf.String(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// init function to set up the command hierarchy and options.
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(createCmd)
|
rootCmd.CompletionOptions.DisableDefaultCmd = true
|
||||||
rootCmd.AddCommand(deleteCmd)
|
|
||||||
rootCmd.CompletionOptions.DisableDefaultCmd = false
|
|
||||||
rootCmd.AddGroup(&cobra.Group{ID: "help", Title: "Helping Commands"})
|
rootCmd.AddGroup(&cobra.Group{ID: "help", Title: "Helping Commands"})
|
||||||
rootCmd.SetCompletionCommandGroupID("help")
|
rootCmd.SetCompletionCommandGroupID("help")
|
||||||
rootCmd.SetHelpCommandGroupID("help")
|
rootCmd.SetHelpCommandGroupID("help")
|
||||||
|
|||||||
Reference in New Issue
Block a user