use registry for interfaces also
add list interfaces command
This commit is contained in:
@@ -5,10 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/LazyBachelor/LazyPM/cmd/survey/tasks"
|
"github.com/LazyBachelor/LazyPM/cmd/survey/tasks"
|
||||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/repl"
|
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/tui"
|
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/web"
|
|
||||||
|
|
||||||
_ "github.com/LazyBachelor/LazyPM/cmd/survey/tasks"
|
_ "github.com/LazyBachelor/LazyPM/cmd/survey/tasks"
|
||||||
)
|
)
|
||||||
@@ -18,18 +15,21 @@ func initializeServices(ctx context.Context) (*service.App, func(), error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initInterfaces() map[string]task.Interface {
|
func initInterfaces() map[string]task.Interface {
|
||||||
return map[string]task.Interface{
|
interfaces := make(map[string]task.Interface)
|
||||||
"repl": repl.NewRepl(),
|
for _, name := range task.ListInterfaces() {
|
||||||
"tui": tui.NewTui(),
|
i, err := task.GetInterface(name)
|
||||||
"web": web.NewWeb(),
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
interfaces[name] = i
|
||||||
}
|
}
|
||||||
|
return interfaces
|
||||||
}
|
}
|
||||||
|
|
||||||
func initTasks(app *service.App) []task.Tasker {
|
func initTasks(app *service.App) []task.Tasker {
|
||||||
var taskList []task.Tasker
|
var taskList []task.Tasker
|
||||||
|
for _, name := range task.ListTasks() {
|
||||||
for _, name := range task.List() {
|
t, err := task.GetTasks(name, app)
|
||||||
t, err := task.Get(name, app)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,9 +4,12 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/LazyBachelor/LazyPM/cmd/survey/tasks"
|
"github.com/LazyBachelor/LazyPM/cmd/survey/tasks"
|
||||||
"github.com/LazyBachelor/LazyPM/internal/commands/survey"
|
surveyCmd "github.com/LazyBachelor/LazyPM/internal/commands/survey"
|
||||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||||
|
"github.com/LazyBachelor/LazyPM/pkg/repl"
|
||||||
"github.com/LazyBachelor/LazyPM/pkg/task"
|
"github.com/LazyBachelor/LazyPM/pkg/task"
|
||||||
|
"github.com/LazyBachelor/LazyPM/pkg/tui"
|
||||||
|
"github.com/LazyBachelor/LazyPM/pkg/web"
|
||||||
"github.com/charmbracelet/fang"
|
"github.com/charmbracelet/fang"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,16 +23,22 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
task.RegisterInterface("tui", tui.NewTui())
|
||||||
|
task.RegisterInterface("web", web.NewWeb())
|
||||||
|
task.RegisterInterface("repl", repl.NewRepl())
|
||||||
|
|
||||||
surveyCmd.StartCmd.RunE = runStartCmd
|
surveyCmd.StartCmd.RunE = runStartCmd
|
||||||
surveyCmd.RootCmd.AddCommand(surveyCmd.StartCmd)
|
surveyCmd.RootCmd.AddCommand(surveyCmd.StartCmd)
|
||||||
surveyCmd.RootCmd.AddCommand(surveyCmd.SubmitCmd)
|
surveyCmd.RootCmd.AddCommand(surveyCmd.SubmitCmd)
|
||||||
surveyCmd.RootCmd.AddCommand(surveyCmd.StatusCmd)
|
surveyCmd.RootCmd.AddCommand(surveyCmd.StatusCmd)
|
||||||
surveyCmd.RootCmd.AddCommand(surveyCmd.ListCmd)
|
surveyCmd.RootCmd.AddCommand(surveyCmd.ListTasksCmd)
|
||||||
|
surveyCmd.RootCmd.AddCommand(surveyCmd.ListInterfacesCmd)
|
||||||
|
|
||||||
task.Register("create_issue", func(app *service.App) task.Tasker {
|
task.RegisterTask("create_issue", func(app *service.App) task.Tasker {
|
||||||
return tasks.NewCreateIssueTask(app)
|
return tasks.NewCreateIssueTask(app)
|
||||||
})
|
})
|
||||||
task.Register("coding_task", func(app *service.App) task.Tasker {
|
task.RegisterTask("coding_task", func(app *service.App) task.Tasker {
|
||||||
return tasks.NewCodingTask(app)
|
return tasks.NewCodingTask(app)
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ func runStartCmd(cmd *cobra.Command, args []string) error {
|
|||||||
|
|
||||||
if cmd.Flags().Changed("interface") {
|
if cmd.Flags().Changed("interface") {
|
||||||
if _, ok := interfaces[surveyCmd.InterfaceType]; !ok {
|
if _, ok := interfaces[surveyCmd.InterfaceType]; !ok {
|
||||||
return fmt.Errorf("invalid interface, valid are (tui, repl, web)")
|
return fmt.Errorf("invalid interface, valid are %v", task.ListInterfaces())
|
||||||
}
|
}
|
||||||
interfaces = map[string]task.Interface{
|
interfaces = map[string]task.Interface{
|
||||||
surveyCmd.InterfaceType: interfaces[surveyCmd.InterfaceType],
|
surveyCmd.InterfaceType: interfaces[surveyCmd.InterfaceType],
|
||||||
|
|||||||
@@ -5,11 +5,24 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ListCmd = &cobra.Command{
|
var ListTasksCmd = &cobra.Command{
|
||||||
Use: "list",
|
Use: "list-tasks",
|
||||||
Short: "List available tasks",
|
Aliases: []string{"ls-t"},
|
||||||
|
Short: "List available tasks",
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
for i, name := range task.List() {
|
for i, name := range task.ListTasks() {
|
||||||
|
cmd.Printf("%d. %s\n", i+1, name)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var ListInterfacesCmd = &cobra.Command{
|
||||||
|
Use: "list-interfaces",
|
||||||
|
Aliases: []string{"ls-i"},
|
||||||
|
Short: "List available interfaces",
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
for i, name := range task.ListInterfaces() {
|
||||||
cmd.Printf("%d. %s\n", i+1, name)
|
cmd.Printf("%d. %s\n", i+1, name)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -6,26 +6,51 @@ import (
|
|||||||
"github.com/LazyBachelor/LazyPM/internal/service"
|
"github.com/LazyBachelor/LazyPM/internal/service"
|
||||||
)
|
)
|
||||||
|
|
||||||
var registry = make(map[string]func(*service.App) Tasker)
|
var interfaceRegistry = make(map[string]Interface)
|
||||||
|
|
||||||
func Register(name string, constructor func(*service.App) Tasker) {
|
func RegisterInterface(name string, iface Interface) {
|
||||||
if _, exists := registry[name]; exists {
|
if _, exists := interfaceRegistry[name]; exists {
|
||||||
panic(fmt.Sprintf("task %q already registered", name))
|
panic(fmt.Sprintf("interface %q already registered", name))
|
||||||
}
|
}
|
||||||
registry[name] = constructor
|
interfaceRegistry[name] = iface
|
||||||
}
|
}
|
||||||
|
|
||||||
func Get(name string, app *service.App) (Tasker, error) {
|
func GetInterface(name string) (Interface, error) {
|
||||||
constructor, ok := registry[name]
|
iface, ok := interfaceRegistry[name]
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("interface %q not found", name)
|
||||||
|
}
|
||||||
|
return iface, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ListInterfaces() []string {
|
||||||
|
names := make([]string, 0, len(interfaceRegistry))
|
||||||
|
for name := range interfaceRegistry {
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
|
var taskRegistry = make(map[string]func(*service.App) Tasker)
|
||||||
|
|
||||||
|
func RegisterTask(name string, constructor func(*service.App) Tasker) {
|
||||||
|
if _, exists := taskRegistry[name]; exists {
|
||||||
|
panic(fmt.Sprintf("task %q already registered", name))
|
||||||
|
}
|
||||||
|
taskRegistry[name] = constructor
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetTasks(name string, app *service.App) (Tasker, error) {
|
||||||
|
constructor, ok := taskRegistry[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, fmt.Errorf("task %q not found", name)
|
return nil, fmt.Errorf("task %q not found", name)
|
||||||
}
|
}
|
||||||
return constructor(app), nil
|
return constructor(app), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func List() []string {
|
func ListTasks() []string {
|
||||||
names := make([]string, 0, len(registry))
|
names := make([]string, 0, len(taskRegistry))
|
||||||
for name := range registry {
|
for name := range taskRegistry {
|
||||||
names = append(names, name)
|
names = append(names, name)
|
||||||
}
|
}
|
||||||
return names
|
return names
|
||||||
|
|||||||
Reference in New Issue
Block a user