diff --git a/cmd/registry/cmd/root.go b/cmd/registry/cmd/root.go index 767cc425f..6a6e36d53 100644 --- a/cmd/registry/cmd/root.go +++ b/cmd/registry/cmd/root.go @@ -29,7 +29,6 @@ import ( "github.com/apigee/registry/cmd/registry/cmd/resolve" "github.com/apigee/registry/cmd/registry/cmd/rpc" "github.com/apigee/registry/cmd/registry/cmd/upload" - "github.com/apigee/registry/cmd/registry/cmd/vocabulary" pkgconf "github.com/apigee/registry/pkg/config" "github.com/spf13/cobra" ) @@ -59,7 +58,6 @@ func Command() *cobra.Command { cmd.AddCommand(get.Command()) cmd.AddCommand(label.Command()) cmd.AddCommand(upload.Command()) - cmd.AddCommand(vocabulary.Command()) cmd.AddCommand(rpc.Command()) return cmd } diff --git a/cmd/registry/cmd/vocabulary/difference.go b/cmd/registry/cmd/vocabulary/difference.go deleted file mode 100644 index 83dd689fe..000000000 --- a/cmd/registry/cmd/vocabulary/difference.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2020 Google LLC. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package vocabulary - -import ( - "fmt" - - "github.com/apigee/registry/pkg/connection" - "github.com/apigee/registry/pkg/log" - "github.com/google/gnostic/metrics/vocabulary" - "github.com/spf13/cobra" - "google.golang.org/protobuf/encoding/protojson" -) - -func differenceCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "difference", - Short: "Compute the difference of specified API vocabularies", - 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") - } - output, err := cmd.Flags().GetString("output") - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to get output from flags") - } - client, err := connection.NewRegistryClient(ctx) - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to get client") - } - _, inputs := collectInputVocabularies(ctx, client, args, filter) - vocab := vocabulary.Difference(inputs) - if output != "" { - setVocabularyToArtifact(ctx, client, vocab, output) - } else { - fmt.Println(protojson.Format((vocab))) - } - }, - } - - cmd.Flags().String("output", "", "artifact name to use when saving the vocabulary artifact") - return cmd -} diff --git a/cmd/registry/cmd/vocabulary/intersection.go b/cmd/registry/cmd/vocabulary/intersection.go deleted file mode 100644 index abb03ff28..000000000 --- a/cmd/registry/cmd/vocabulary/intersection.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2020 Google LLC. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package vocabulary - -import ( - "fmt" - - "github.com/apigee/registry/pkg/connection" - "github.com/apigee/registry/pkg/log" - "github.com/google/gnostic/metrics/vocabulary" - "github.com/spf13/cobra" - "google.golang.org/protobuf/encoding/protojson" -) - -func intersectionCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "intersection", - Short: "Compute the intersection of specified API vocabularies", - 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") - } - output, err := cmd.Flags().GetString("output") - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to get output from flags") - } - client, err := connection.NewRegistryClient(ctx) - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to get client") - } - _, inputs := collectInputVocabularies(ctx, client, args, filter) - vocab := vocabulary.Intersection(inputs) - if output != "" { - setVocabularyToArtifact(ctx, client, vocab, output) - } else { - fmt.Println(protojson.Format((vocab))) - } - }, - } - - cmd.Flags().String("output", "", "artifact name to use when saving the vocabulary artifact") - return cmd -} diff --git a/cmd/registry/cmd/vocabulary/union.go b/cmd/registry/cmd/vocabulary/union.go deleted file mode 100644 index 453789403..000000000 --- a/cmd/registry/cmd/vocabulary/union.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright 2020 Google LLC. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package vocabulary - -import ( - "fmt" - - "github.com/apigee/registry/pkg/connection" - "github.com/apigee/registry/pkg/log" - "github.com/google/gnostic/metrics/vocabulary" - "github.com/spf13/cobra" - "google.golang.org/protobuf/encoding/protojson" -) - -func unionCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "union", - Short: "Compute the union of specified API vocabularies", - 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") - } - output, err := cmd.Flags().GetString("output") - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to get output from flags") - } - client, err := connection.NewRegistryClient(ctx) - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to get client") - } - _, inputs := collectInputVocabularies(ctx, client, args, filter) - vocab := vocabulary.Union(inputs) - if output != "" { - setVocabularyToArtifact(ctx, client, vocab, output) - } else { - fmt.Println(protojson.Format((vocab))) - } - }, - } - - cmd.Flags().String("output", "", "artifact name to use when saving the vocabulary artifact") - return cmd -} diff --git a/cmd/registry/cmd/vocabulary/unique.go b/cmd/registry/cmd/vocabulary/unique.go deleted file mode 100644 index 5f091a464..000000000 --- a/cmd/registry/cmd/vocabulary/unique.go +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2020 Google LLC. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package vocabulary - -import ( - "fmt" - "path/filepath" - "strings" - - "github.com/apigee/registry/pkg/connection" - "github.com/apigee/registry/pkg/log" - "github.com/google/gnostic/metrics/vocabulary" - "github.com/spf13/cobra" - "google.golang.org/protobuf/encoding/protojson" -) - -func uniqueCommand() *cobra.Command { - var outputID string - cmd := &cobra.Command{ - Use: "unique", - Short: "Compute the unique subsets of each member of specified vocabularies", - 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") - } - - if strings.Contains(outputID, "/") { - log.Fatal(ctx, "output-id must specify an artifact id (final segment only) and not a full name.") - } - - client, err := connection.NewRegistryClient(ctx) - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to get client") - } - names, inputs := collectInputVocabularies(ctx, client, args, filter) - list := vocabulary.FilterCommon(inputs) - if outputID != "" { - for i, unique := range list.Vocabularies { - outputArtifactName := filepath.Dir(names[i]) + "/" + outputID - setVocabularyToArtifact(ctx, client, unique, outputArtifactName) - } - } else { - fmt.Println(protojson.Format((list))) - } - }, - } - - cmd.Flags().StringVar(&outputID, "output-id", "vocabulary-unique", "artifact ID to use when saving each result vocabulary") - return cmd -} diff --git a/cmd/registry/cmd/vocabulary/versions.go b/cmd/registry/cmd/vocabulary/versions.go deleted file mode 100644 index eaaf7b54e..000000000 --- a/cmd/registry/cmd/vocabulary/versions.go +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright 2020 Google LLC. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package vocabulary - -import ( - "fmt" - "strings" - - "github.com/apigee/registry/pkg/connection" - "github.com/apigee/registry/pkg/log" - "github.com/google/gnostic/metrics/vocabulary" - "github.com/spf13/cobra" - "google.golang.org/protobuf/encoding/protojson" -) - -func versionsCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "versions", - Short: "Compute the differences in API vocabularies associated with successive API versions", - 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") - } - output, err := cmd.Flags().GetString("output") - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to get output from flags") - } - client, err := connection.NewRegistryClient(ctx) - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to get client") - } - names, inputs := collectInputVocabularies(ctx, client, args, filter) - - parts := strings.Split(names[0], "/") - parts = parts[0:4] - parent := strings.Join(parts, "/") - - history := vocabulary.Version(inputs, names, parent) - if output != "" { - setVersionHistoryToArtifact(ctx, client, history, output) - } else { - fmt.Println(protojson.Format((history))) - } - }, - } - - cmd.Flags().String("output", "", "artifact name to use when saving the vocabulary artifact") - return cmd -} diff --git a/cmd/registry/cmd/vocabulary/vocabulary.go b/cmd/registry/cmd/vocabulary/vocabulary.go deleted file mode 100644 index 59cc3c2ae..000000000 --- a/cmd/registry/cmd/vocabulary/vocabulary.go +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright 2020 Google LLC. All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package vocabulary - -import ( - "context" - "strings" - - "github.com/apigee/registry/cmd/registry/compress" - "github.com/apigee/registry/pkg/connection" - "github.com/apigee/registry/pkg/log" - "github.com/apigee/registry/pkg/mime" - "github.com/apigee/registry/pkg/names" - "github.com/apigee/registry/pkg/visitor" - "github.com/apigee/registry/rpc" - "github.com/spf13/cobra" - "google.golang.org/protobuf/proto" - - metrics "github.com/google/gnostic/metrics" -) - -func Command() *cobra.Command { - cmd := &cobra.Command{ - Use: "vocabulary", - Short: "Operate on API vocabularies in the API Registry", - } - - cmd.AddCommand(differenceCommand()) - cmd.AddCommand(intersectionCommand()) - cmd.AddCommand(unionCommand()) - cmd.AddCommand(uniqueCommand()) - cmd.AddCommand(versionsCommand()) - - cmd.PersistentFlags().String("filter", "", "Filter selected resources") - return cmd -} - -func collectInputVocabularies(ctx context.Context, client connection.RegistryClient, args []string, filter string) ([]string, []*metrics.Vocabulary) { - c, err := connection.ActiveConfig() - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to get config") - } - - inputNames := make([]string, 0) - inputs := make([]*metrics.Vocabulary, 0) - for _, name := range args { - name = c.FQName(name) - artifact, err := names.ParseArtifact(name) - if err != nil { - continue - } - - err = visitor.ListArtifacts(ctx, client, artifact, filter, true, func(ctx context.Context, artifact *rpc.Artifact) error { - messageType, err := mime.MessageTypeForMimeType(artifact.GetMimeType()) - if err != nil || messageType != "gnostic.metrics.Vocabulary" { - log.Debugf(ctx, "Skipping, not a vocabulary: %s", artifact.Name) - return nil - } - - vocab := &metrics.Vocabulary{} - if err := proto.Unmarshal(artifact.GetContents(), vocab); err != nil { - log.FromContext(ctx).WithError(err).Debug("Failed to unmarshal contents") - return nil - } - - inputNames = append(inputNames, artifact.Name) - inputs = append(inputs, vocab) - return nil - }) - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to list artifacts") - } - } - - return inputNames, inputs -} - -func setVocabularyToArtifact(ctx context.Context, client connection.RegistryClient, output *metrics.Vocabulary, outputArtifactName string) { - parts := strings.Split(outputArtifactName, "/artifacts/") - subject := parts[0] - relation := parts[1] - messageData, _ := proto.Marshal(output) - var err error - messageData, err = compress.GZippedBytes(messageData) - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to compress artifact") - } - log.Debugf(ctx, "Saving vocabulary data (%d bytes)", len(messageData)) - artifact := &rpc.Artifact{ - Name: subject + "/artifacts/" + relation, - MimeType: mime.MimeTypeForMessageType("gnostic.metrics.Vocabulary+gzip"), - Contents: messageData, - } - err = visitor.SetArtifact(ctx, client, artifact) - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to save artifact") - } -} - -func setVersionHistoryToArtifact(ctx context.Context, client connection.RegistryClient, output *metrics.VersionHistory, outputArtifactName string) { - parts := strings.Split(outputArtifactName, "/artifacts/") - subject := parts[0] - relation := parts[1] - messageData, _ := proto.Marshal(output) - artifact := &rpc.Artifact{ - Name: subject + "/artifacts/" + relation, - MimeType: mime.MimeTypeForMessageType("gnostic.metrics.VersionHistory"), - Contents: messageData, - } - err := visitor.SetArtifact(ctx, client, artifact) - if err != nil { - log.FromContext(ctx).WithError(err).Fatal("Failed to save artifact") - } -}