Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update registry-experimental command constructors to match the registry tool's #142

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions cmd/registry-experimental/cmd/compute/compute.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@
package compute

import (
"context"

"github.com/apigee/registry-experimental/cmd/registry-experimental/cmd/compute/summary"
"github.com/spf13/cobra"
)

func Command(ctx context.Context) *cobra.Command {
func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "compute",
Short: "Compute properties of resources in the API Registry",
}

cmd.AddCommand(descriptorCommand(ctx))
cmd.AddCommand(searchIndexCommand(ctx))
cmd.AddCommand(descriptorCommand())
cmd.AddCommand(searchIndexCommand())
cmd.AddCommand(summary.Command())

cmd.PersistentFlags().String("filter", "", "Filter selected resources")
Expand Down
4 changes: 3 additions & 1 deletion cmd/registry-experimental/cmd/compute/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ import (
"google.golang.org/protobuf/types/descriptorpb"
)

func descriptorCommand(ctx context.Context) *cobra.Command {
func descriptorCommand() *cobra.Command {
return &cobra.Command{
Use: "descriptor",
Short: "Compute descriptors of API specs",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()

filter, err := cmd.Flags().GetString("filter")
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get filter from flags")
Expand Down
4 changes: 2 additions & 2 deletions cmd/registry-experimental/cmd/compute/search-index.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ import (

var bleveMutex sync.Mutex

func searchIndexCommand(ctx context.Context) *cobra.Command {
func searchIndexCommand() *cobra.Command {
return &cobra.Command{
Use: "search-index",
Short: "Compute a local search index of specs (experimental)",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
filter, err := cmd.Flags().GetString("filter")
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get filter from flags")
}

ctx := context.Background()
client, err := connection.NewRegistryClient(ctx)
if err != nil {
log.FromContext(ctx).WithError(err).Fatal("Failed to get client")
Expand Down
6 changes: 2 additions & 4 deletions cmd/registry-experimental/cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
package generate

import (
"context"

"github.com/spf13/cobra"
)

func Command(ctx context.Context) *cobra.Command {
func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "generate",
Short: "Generate resources from the API Registry",
}

cmd.AddCommand(openapiCommand(ctx))
cmd.AddCommand(openapiCommand())

cmd.PersistentFlags().String("filter", "", "Filter selected resources")
return cmd
Expand Down
3 changes: 2 additions & 1 deletion cmd/registry-experimental/cmd/generate/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ import (
"github.com/spf13/cobra"
)

func openapiCommand(ctx context.Context) *cobra.Command {
func openapiCommand() *cobra.Command {
var specID string
cmd := &cobra.Command{
Use: "openapi",
Short: "Generate an OpenAPI spec from another specification format",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

filter, err := cmd.Flags().GetString("filter")
if err != nil {
Expand Down
36 changes: 14 additions & 22 deletions cmd/registry-experimental/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,35 @@
package cmd

import (
"context"

"fmt"

"github.com/apigee/registry-experimental/cmd/registry-experimental/cmd/compute"
"github.com/apigee/registry-experimental/cmd/registry-experimental/cmd/count"
"github.com/apigee/registry-experimental/cmd/registry-experimental/cmd/export"
"github.com/apigee/registry-experimental/cmd/registry-experimental/cmd/extract"
"github.com/apigee/registry-experimental/cmd/registry-experimental/cmd/generate"
"github.com/apigee/registry-experimental/cmd/registry-experimental/cmd/search"
"github.com/apigee/registry-experimental/cmd/registry-experimental/cmd/wipeout"
"github.com/apigee/registry/pkg/log"
"github.com/google/uuid"
pkgconf "github.com/apigee/registry/pkg/config"
"github.com/spf13/cobra"
)

func Command(ctx context.Context) *cobra.Command {
var logID string
// Version value will be replaced by the release tag when the binaries are
// generated by GoReleaser.
var Version = "dev"

func Command() *cobra.Command {
var cmd = &cobra.Command{
Use: "registry-experimental",
Short: "Experimental utilities for working with the API Registry",
Use: "registry-experimental",
Version: Version,
Short: "Experimental utilities for working with the API Registry",
}
cmd.PersistentFlags().AddFlagSet(pkgconf.Flags)

// Bind a logger instance to the local context with metadata for outbound requests.
logger := log.NewLogger(log.DebugLevel)
ctx = log.NewOutboundContext(log.NewContext(ctx, logger), log.Metadata{
UID: fmt.Sprintf("%.8s", uuid.New()),
})

cmd.AddCommand(compute.Command(ctx))
cmd.AddCommand(compute.Command())
cmd.AddCommand(count.Command())
cmd.AddCommand(export.Command())
cmd.AddCommand(extract.Command())
cmd.AddCommand(generate.Command(ctx))
cmd.AddCommand(search.Command(ctx))
cmd.AddCommand(wipeout.Command(ctx))

cmd.PersistentFlags().StringVar(&logID, "log-id", "", "Assign an ID which gets attached to the log produced")
Copy link
Contributor Author

@timburks timburks Feb 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seaneganx do we need this --log-id flag? I don't see it in the main repo.

cmd.AddCommand(generate.Command())
cmd.AddCommand(search.Command())
cmd.AddCommand(wipeout.Command())
return cmd
}
4 changes: 2 additions & 2 deletions cmd/registry-experimental/cmd/search/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package search

import (
"context"
"fmt"

"github.com/apigee/registry/pkg/log"
Expand All @@ -24,12 +23,13 @@ import (
"github.com/spf13/cobra"
)

func Command(ctx context.Context) *cobra.Command {
func Command() *cobra.Command {
return &cobra.Command{
Use: "search",
Short: "Search a local index of specs in the API Registry (experimental)",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := cmd.Context()
// open an existing index
index, err := bleve.Open("registry.bleve")
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/registry-experimental/cmd/wipeout/wipeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import (
"github.com/spf13/cobra"
)

func Command(ctx context.Context) *cobra.Command {
func Command() *cobra.Command {
cmd := &cobra.Command{
Use: "wipeout PROJECT-ID",
Short: "Delete everything in a project",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
ctx := cmd.Context()
registryClient, err := connection.NewRegistryClient(ctx)
if err != nil {
log.Fatalf(ctx, "Failed to create client: %+v", err)
Expand Down
14 changes: 10 additions & 4 deletions cmd/registry-experimental/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ import (
"os"

"github.com/apigee/registry-experimental/cmd/registry-experimental/cmd"
"github.com/apigee/registry/pkg/log"
"github.com/google/uuid"
)

func main() {
ctx := context.Background()
cmd := cmd.Command(ctx)
if err := cmd.Execute(); err != nil {
fmt.Println(err)
// Bind a logger instance to the local context with metadata for outbound requests.
logger := log.NewLogger(log.DebugLevel)
ctx := log.NewOutboundContext(log.NewContext(context.Background(), logger), log.Metadata{
UID: fmt.Sprintf("%.8s", uuid.New()),
})

cmd := cmd.Command()
if err := cmd.ExecuteContext(ctx); err != nil {
os.Exit(1)
}
}