migrate graph 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 14:42:38 +01:00
committed by Florian Schade
parent 57013e848b
commit 8d6f7a8942
5 changed files with 53 additions and 54 deletions

View File

@@ -8,19 +8,19 @@ import (
"github.com/opencloud-eu/opencloud/services/graph/pkg/config"
"github.com/opencloud-eu/opencloud/services/graph/pkg/config/parser"
"github.com/opencloud-eu/opencloud/services/graph/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 configlog.ReturnError(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

@@ -4,15 +4,14 @@ import (
"os"
"github.com/opencloud-eu/opencloud/pkg/clihelper"
"github.com/urfave/cli/v2"
"github.com/opencloud-eu/opencloud/services/graph/pkg/config"
"github.com/spf13/cobra"
)
// GetCommands provides all commands for this service
func GetCommands(cfg *config.Config) cli.Commands {
return append([]*cli.Command{
func GetCommands(cfg *config.Config) []*cobra.Command {
return append([]*cobra.Command{
// start this service
Server(cfg),
@@ -26,10 +25,12 @@ func GetCommands(cfg *config.Config) cli.Commands {
// Execute is the entry point for the opencloud graph command.
func Execute(cfg *config.Config) error {
app := clihelper.DefaultApp(&cli.App{
Name: "graph",
Usage: "Serve Graph API for OpenCloud",
Commands: GetCommands(cfg),
app := clihelper.DefaultAppCobra(&cobra.Command{
Use: "graph",
Short: "Serve Graph 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

@@ -5,11 +5,6 @@ import (
"fmt"
"os/signal"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"github.com/opencloud-eu/opencloud/pkg/config/configlog"
"github.com/opencloud-eu/opencloud/pkg/runner"
"github.com/opencloud-eu/opencloud/pkg/tracing"
@@ -20,20 +15,24 @@ import (
"github.com/opencloud-eu/opencloud/services/graph/pkg/metrics"
"github.com/opencloud-eu/opencloud/services/graph/pkg/server/debug"
"github.com/opencloud-eu/opencloud/services/graph/pkg/server/http"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
"github.com/pkg/errors"
"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

@@ -5,15 +5,15 @@ import (
"slices"
"strings"
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/renderer"
"github.com/olekukonko/tablewriter/tw"
"github.com/urfave/cli/v2"
"github.com/opencloud-eu/opencloud/pkg/config/configlog"
"github.com/opencloud-eu/opencloud/services/graph/pkg/config"
"github.com/opencloud-eu/opencloud/services/graph/pkg/config/parser"
"github.com/opencloud-eu/opencloud/services/graph/pkg/unifiedrole"
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/renderer"
"github.com/olekukonko/tablewriter/tw"
"github.com/spf13/cobra"
)
var (
@@ -34,15 +34,14 @@ var (
)
// UnifiedRoles bundles available commands for unified roles
func UnifiedRoles(cfg *config.Config) cli.Commands {
cmds := cli.Commands{
func UnifiedRoles(cfg *config.Config) []*cobra.Command {
cmds := []*cobra.Command{
listUnifiedRoles(cfg),
}
for _, cmd := range cmds {
cmd.Category = "unified-roles"
cmd.Name = strings.Join([]string{cmd.Name, "unified-roles"}, "-")
cmd.Before = func(c *cli.Context) error {
cmd.Use = strings.Join([]string{cmd.Use, "unified-roles"}, "-")
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
return configlog.ReturnError(parser.ParseConfig(cfg))
}
}
@@ -51,11 +50,11 @@ func UnifiedRoles(cfg *config.Config) cli.Commands {
}
// unifiedRolesStatus lists available unified roles, it contains an indicator to show if the role is enabled or not
func listUnifiedRoles(cfg *config.Config) *cli.Command {
return &cli.Command{
Name: "list",
Usage: "list available unified roles",
Action: func(c *cli.Context) error {
func listUnifiedRoles(cfg *config.Config) *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "list available unified roles",
RunE: func(cmd *cobra.Command, args []string) error {
r := tw.Rendition{
Settings: tw.Settings{
Separators: tw.Separators{

View File

@@ -6,20 +6,20 @@ import (
"github.com/opencloud-eu/opencloud/pkg/registry"
"github.com/opencloud-eu/opencloud/pkg/version"
"github.com/opencloud-eu/opencloud/services/graph/pkg/config"
"github.com/olekukonko/tablewriter"
"github.com/olekukonko/tablewriter/tw"
"github.com/opencloud-eu/opencloud/services/graph/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("")