refactor to make cli better and simplify the code
This commit is contained in:
@@ -1,62 +0,0 @@
|
||||
// Package cli provides the command-line interface for the PM System.
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/LazyBachelor/LazyPM/internal/app"
|
||||
issues "github.com/LazyBachelor/LazyPM/internal/commands/issues"
|
||||
"github.com/LazyBachelor/LazyPM/internal/models"
|
||||
"github.com/charmbracelet/fang"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// Config is an alias for app.Config, used to configure the CLI.
|
||||
type Config = models.Config
|
||||
|
||||
type CLI struct {
|
||||
RootCmd *cobra.Command
|
||||
}
|
||||
|
||||
func New(rootCmd *cobra.Command) *CLI {
|
||||
return &CLI{
|
||||
RootCmd: rootCmd,
|
||||
}
|
||||
}
|
||||
|
||||
// Run initializes the services and executes the CLI commands.
|
||||
func (c *CLI) Run(ctx context.Context, config Config) error {
|
||||
app, cleanup, err := app.New(ctx, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer cleanup()
|
||||
|
||||
issues.SetApp(app)
|
||||
|
||||
if err := fang.Execute(ctx, c.RootCmd,
|
||||
fang.WithColorSchemeFunc(fang.AnsiColorScheme)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RunWithArgs initializes the services and executes the CLI commands with the provided arguments.
|
||||
func (c *CLI) RunWithArgs(ctx context.Context, config Config, args []string) error {
|
||||
app, cleanup, err := app.New(ctx, config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer cleanup()
|
||||
|
||||
issues.SetApp(app)
|
||||
|
||||
if err := issues.ExecuteArgs(args); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
issues "github.com/LazyBachelor/LazyPM/internal/commands/issues"
|
||||
"github.com/LazyBachelor/LazyPM/internal/models"
|
||||
"github.com/LazyBachelor/LazyPM/internal/style"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/cli"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||
"github.com/LazyBachelor/LazyPM/pkg/tui/styles"
|
||||
"github.com/c-bata/go-prompt"
|
||||
@@ -43,7 +42,7 @@ func New() *REPL {
|
||||
}
|
||||
|
||||
// Run starts the interactive Read-Eval-Print Loop for the PM CLI.
|
||||
func (r *REPL) Run(ctx context.Context, config cli.Config) error {
|
||||
func (r *REPL) Run(ctx context.Context, config app.Config) error {
|
||||
// Set terminal to raw mode to capture input properly in the REPL.
|
||||
// This allows us to handle input character by character and provide a better user experience.
|
||||
// We also ensure that the terminal state is restored when the REPL exits, even if an error occurs.
|
||||
|
||||
Reference in New Issue
Block a user