migrate search from urfave/cli to spf13/cobra

Signed-off-by: Christian Richter <c.richter@opencloud.eu>
This commit is contained in:
Christian Richter
2025-12-02 16:58:00 +01:00
committed by Florian Schade
parent 882dede6d3
commit e7a5788634
5 changed files with 65 additions and 64 deletions

View File

@@ -7,19 +7,19 @@ import (
"github.com/opencloud-eu/opencloud/services/search/pkg/config"
"github.com/opencloud-eu/opencloud/services/search/pkg/config/parser"
"github.com/opencloud-eu/opencloud/services/search/pkg/logging"
"github.com/urfave/cli/v2"
"github.com/spf13/cobra"
)
// Health is the entrypoint for the health command.
func Health(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "health",
Usage: "check health status",
Category: "info",
Before: func(c *cli.Context) error {
func Health(cfg *config.Config) *cobra.Command {
return &cobra.Command{
Use: "health",
Short: "check health status",
PreRunE: func(cmd *cobra.Command, args []string) error {
return parser.ParseConfig(cfg)
},
Action: func(c *cli.Context) error {
RunE: func(cmd *cobra.Command, args []string) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
resp, err := http.Get(

View File

@@ -6,44 +6,32 @@ import (
"fmt"
"time"
"github.com/urfave/cli/v2"
"go-micro.dev/v4/client"
"github.com/opencloud-eu/opencloud/pkg/config/configlog"
"github.com/opencloud-eu/opencloud/pkg/service/grpc"
"github.com/opencloud-eu/opencloud/pkg/tracing"
searchsvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/search/v0"
"github.com/opencloud-eu/opencloud/services/search/pkg/config"
"github.com/opencloud-eu/opencloud/services/search/pkg/config/parser"
"github.com/spf13/cobra"
"go-micro.dev/v4/client"
)
// Index is the entrypoint for the server command.
func Index(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "index",
Usage: "index the files for one one more users",
Category: "index management",
Aliases: []string{"i"},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "space",
Aliases: []string{"s"},
Usage: "the id of the space to travers and index the files of. This or --all-spaces is required.",
},
&cli.BoolFlag{
Name: "all-spaces",
Usage: "index all spaces instead. This or --space is required.",
},
},
Before: func(_ *cli.Context) error {
func Index(cfg *config.Config) *cobra.Command {
indexCmd := &cobra.Command{
Use: "index",
Short: "index the files for one one more users",
Aliases: []string{"i"},
PreRunE: func(cmd *cobra.Command, args []string) error {
return configlog.ReturnFatal(parser.ParseConfig(cfg))
},
Action: func(ctx *cli.Context) error {
if ctx.String("space") == "" && !ctx.Bool("all-spaces") {
RunE: func(cmd *cobra.Command, args []string) error {
if cmd.Flag("space").Value.String() == "" && !cmd.Flag("all-spaces").Changed {
return errors.New("either --space or --all-spaces is required")
}
traceProvider, err := tracing.GetTraceProvider(ctx.Context, cfg.Commons.TracesExporter, cfg.Service.Name)
traceProvider, err := tracing.GetTraceProvider(cmd.Context(), cfg.Commons.TracesExporter, cfg.Service.Name)
if err != nil {
return err
}
@@ -58,7 +46,7 @@ func Index(cfg *config.Config) *cli.Command {
c := searchsvc.NewSearchProviderService("eu.opencloud.api.search", grpcClient)
_, err = c.IndexSpace(context.Background(), &searchsvc.IndexSpaceRequest{
SpaceId: ctx.String("space"),
SpaceId: cmd.Flag("space").Value.String(),
}, func(opts *client.CallOptions) { opts.RequestTimeout = 10 * time.Minute })
if err != nil {
fmt.Println("failed to index space: " + err.Error())
@@ -67,4 +55,17 @@ func Index(cfg *config.Config) *cli.Command {
return nil
},
}
indexCmd.Flags().StringP(
"space",
"s",
"",
"the id of the space to travers and index the files of. This or --all-spaces is required.")
indexCmd.Flags().Bool(
"all-spaces",
false,
"index all spaces instead. This or --space is required.",
)
return indexCmd
}

View File

@@ -4,14 +4,14 @@ import (
"os"
"github.com/opencloud-eu/opencloud/pkg/clihelper"
"github.com/opencloud-eu/opencloud/services/search/pkg/config"
"github.com/urfave/cli/v2"
"github.com/spf13/cobra"
)
// GetCommands provides all commands for this service
func GetCommands(cfg *config.Config) cli.Commands {
return []*cli.Command{
func GetCommands(cfg *config.Config) []*cobra.Command {
return []*cobra.Command{
// start this service
Server(cfg),
@@ -26,10 +26,12 @@ func GetCommands(cfg *config.Config) cli.Commands {
// Execute is the entry point for the opencloud-search command.
func Execute(cfg *config.Config) error {
app := clihelper.DefaultApp(&cli.App{
Name: "search",
Usage: "Serve search API for OpenCloud",
Commands: GetCommands(cfg),
app := clihelper.DefaultAppCobra(&cobra.Command{
Use: "search",
Short: "Serve search API for OpenCloud",
})
return app.RunContext(cfg.Context, os.Args)
app.AddCommand(GetCommands(cfg)...)
app.SetArgs(os.Args[1:])
return app.ExecuteContext(cfg.Context)
}

View File

@@ -8,12 +8,6 @@ import (
"os"
"os/signal"
"github.com/opencloud-eu/reva/v2/pkg/events/raw"
"github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool"
opensearchgo "github.com/opensearch-project/opensearch-go/v4"
opensearchgoAPI "github.com/opensearch-project/opensearch-go/v4/opensearchapi"
"github.com/urfave/cli/v2"
"github.com/opencloud-eu/opencloud/pkg/config/configlog"
"github.com/opencloud-eu/opencloud/pkg/generators"
"github.com/opencloud-eu/opencloud/pkg/registry"
@@ -33,20 +27,25 @@ import (
"github.com/opencloud-eu/opencloud/services/search/pkg/server/debug"
"github.com/opencloud-eu/opencloud/services/search/pkg/server/grpc"
svcEvent "github.com/opencloud-eu/opencloud/services/search/pkg/service/event"
"github.com/opencloud-eu/reva/v2/pkg/events/raw"
"github.com/opencloud-eu/reva/v2/pkg/rgrpc/todo/pool"
opensearchgo "github.com/opensearch-project/opensearch-go/v4"
opensearchgoAPI "github.com/opensearch-project/opensearch-go/v4/opensearchapi"
"github.com/spf13/cobra"
)
// Server is the entrypoint for the server command.
func Server(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "server",
Usage: fmt.Sprintf("start the %s service without runtime (unsupervised mode)", cfg.Service.Name),
Category: "server",
Before: func(c *cli.Context) error {
func Server(cfg *config.Config) *cobra.Command {
return &cobra.Command{
Use: "server",
Short: fmt.Sprintf("start the %s service without runtime (unsupervised mode)", cfg.Service.Name),
PreRunE: func(cmd *cobra.Command, args []string) error {
return configlog.ReturnFatal(parser.ParseConfig(cfg))
},
Action: func(c *cli.Context) error {
RunE: func(cmd *cobra.Command, args []string) error {
logger := logging.Configure(cfg.Service.Name, cfg.Log)
traceProvider, err := tracing.GetTraceProvider(c.Context, cfg.Commons.TracesExporter, cfg.Service.Name)
traceProvider, err := tracing.GetTraceProvider(cmd.Context(), cfg.Commons.TracesExporter, cfg.Service.Name)
if err != nil {
return err
}

View File

@@ -6,20 +6,19 @@ import (
"github.com/opencloud-eu/opencloud/pkg/registry"
"github.com/opencloud-eu/opencloud/pkg/version"
"github.com/opencloud-eu/opencloud/services/search/pkg/config"
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/tw"
"github.com/opencloud-eu/opencloud/services/search/pkg/config"
"github.com/urfave/cli/v2"
"github.com/spf13/cobra"
)
// Version prints the service versions of all running instances.
func Version(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "version",
Usage: "print the version of this binary and the running service instances",
Category: "info",
Action: func(c *cli.Context) error {
func Version(cfg *config.Config) *cobra.Command {
return &cobra.Command{
Use: "version",
Short: "print the version of this binary and the running service instances",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println("Version: " + version.GetString())
fmt.Printf("Compiled: %s\n", version.Compiled())
fmt.Println("")