Skip to content

Commit

Permalink
Add support for dry_run flag in resolve command (#309)
Browse files Browse the repository at this point in the history
* Add support for dry_run flag in resolve command

* change flags from underscore to hyphen separators
  • Loading branch information
shrutiparabgoogle authored Sep 24, 2021
1 parent 5478442 commit 2f1c779
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 32 deletions.
16 changes: 14 additions & 2 deletions cmd/registry/cmd/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func fetchManifest(
}

func Command(ctx context.Context) *cobra.Command {
var dryRun bool
cmd := &cobra.Command{
Use: "resolve MANIFEST_RESOURCE",
Short: "resolve the dependencies and update the registry state (experimental)",
Expand Down Expand Up @@ -87,10 +88,19 @@ func Command(ctx context.Context) *cobra.Command {
if len(actions) == 0 {
log.Debug("Generated 0 actions. The registry is already in a resolved state.")
return
} else {
log.Debugf("Generated %d actions. Starting Execution...", len(actions))
}

log.Debugf("Generated %d actions.", len(actions))

// If dry_run is set to true, print the generated actions and exit
if dryRun {
for _, a := range actions {
log.Debugf("Action: %q", a.Command)
}
return
}

log.Debug("Starting execution...")
taskQueue, wait := core.WorkerPool(ctx, 64)
defer wait()
// Submit tasks to taskQueue
Expand All @@ -102,5 +112,7 @@ func Command(ctx context.Context) *cobra.Command {
}
},
}

cmd.Flags().BoolVar(&dryRun, "dry-run", false, "if set, actions will only be printed and not executed")
return cmd
}
17 changes: 16 additions & 1 deletion cmd/registry/cmd/resolve/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ func TestResolve(t *testing.T) {
tests := []struct {
desc string
manifestPath string
dryRun bool
listPattern string
want []string
}{
{
desc: "normal case",
manifestPath: filepath.Join("testdata", "manifest.yaml"),
dryRun: false,
listPattern: "projects/controller-demo/locations/global/apis/petstore/versions/-/specs/-/artifacts/-",
want: []string{
"projects/controller-demo/locations/global/apis/petstore/versions/1.0.0/specs/openapi.yaml/artifacts/complexity",
Expand All @@ -68,13 +70,21 @@ func TestResolve(t *testing.T) {
{
desc: "receipt artifact",
manifestPath: filepath.Join("testdata", "manifest_receipt.yaml"),
dryRun: false,
listPattern: "projects/controller-demo/locations/global/apis/petstore/versions/-/specs/-/artifacts/-",
want: []string{
"projects/controller-demo/locations/global/apis/petstore/versions/1.0.0/specs/openapi.yaml/artifacts/test-receipt-artifact",
"projects/controller-demo/locations/global/apis/petstore/versions/1.0.1/specs/openapi.yaml/artifacts/test-receipt-artifact",
"projects/controller-demo/locations/global/apis/petstore/versions/1.1.0/specs/openapi.yaml/artifacts/test-receipt-artifact",
},
},
{
desc: "dry run",
manifestPath: filepath.Join("testdata", "manifest.yaml"),
dryRun: true,
listPattern: "projects/controller-demo/locations/global/apis/petstore/versions/-/specs/-/artifacts/-",
want: []string{},
},
}

for _, test := range tests {
Expand Down Expand Up @@ -216,16 +226,21 @@ func TestResolve(t *testing.T) {
}

// Upload the manifest to registry
args := []string{"manifest", test.manifestPath, "--project_id=" + testProject}
args := []string{"manifest", test.manifestPath, "--project-id=" + testProject}
uploadCmd := upload.Command(ctx)
uploadCmd.SetArgs(args)
if err = uploadCmd.Execute(); err != nil {
t.Fatalf("Failed to upload the manifest: %s", err)
}

resolveCmd := Command(ctx)

args = []string{"projects/" + testProject + "/locations/global/artifacts/test-manifest"}
if test.dryRun {
args = append(args, "--dry-run")
}
resolveCmd.SetArgs(args)

if err = resolveCmd.Execute(); err != nil {
t.Fatalf("Execute() with args %v returned error: %s", args, err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/registry/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ func Command(ctx context.Context) *cobra.Command {
cmd.AddCommand(upload.Command(ctx))
cmd.AddCommand(vocabulary.Command(ctx))

cmd.PersistentFlags().StringVar(&logID, "log_id", "", "Assign an ID which gets attached to the log produced")
cmd.PersistentFlags().StringVar(&logID, "log-id", "", "Assign an ID which gets attached to the log produced")
return cmd
}
4 changes: 2 additions & 2 deletions cmd/registry/cmd/upload/bulk/bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Command(ctx context.Context) *cobra.Command {
cmd.AddCommand(openAPICommand(ctx))
cmd.AddCommand(protosCommand(ctx))

cmd.PersistentFlags().String("project_id", "", "Project ID to use for each upload")
_ = cmd.MarkFlagRequired("project_id")
cmd.PersistentFlags().String("project-id", "", "Project ID to use for each upload")
_ = cmd.MarkFlagRequired("project-id")
return cmd
}
4 changes: 2 additions & 2 deletions cmd/registry/cmd/upload/bulk/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func discoveryCommand(ctx context.Context) *cobra.Command {
Use: "discovery",
Short: "Bulk-upload API Discovery documents from the Google API Discovery service",
Run: func(cmd *cobra.Command, args []string) {
projectID, err := cmd.Flags().GetString("project_id")
projectID, err := cmd.Flags().GetString("project-id")
if err != nil {
log.WithError(err).Fatal("Failed to get project_id from flags")
log.WithError(err).Fatal("Failed to get project-id from flags")
}

ctx := context.Background()
Expand Down
6 changes: 3 additions & 3 deletions cmd/registry/cmd/upload/bulk/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func openAPICommand(ctx context.Context) *cobra.Command {
Short: "Bulk-upload OpenAPI descriptions from a directory of specs",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
projectID, err := cmd.Flags().GetString("project_id")
projectID, err := cmd.Flags().GetString("project-id")
if err != nil {
log.WithError(err).Fatal("Failed to get project_id from flags")
log.WithError(err).Fatal("Failed to get project-id from flags")
}

ctx := context.Background()
Expand All @@ -55,7 +55,7 @@ func openAPICommand(ctx context.Context) *cobra.Command {
},
}

cmd.Flags().StringVar(&baseURI, "base_uri", "", "Prefix to use for the source_uri field of each spec upload")
cmd.Flags().StringVar(&baseURI, "base-uri", "", "Prefix to use for the source_uri field of each spec upload")
return cmd
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/registry/cmd/upload/bulk/protos.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func protosCommand(ctx context.Context) *cobra.Command {
Short: "Bulk-upload Protocol Buffer descriptions from a directory of specs",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
projectID, err := cmd.Flags().GetString("project_id")
projectID, err := cmd.Flags().GetString("project-id")
if err != nil {
log.WithError(err).Fatal("Failed to get project_id from flags")
log.WithError(err).Fatal("Failed to get project-id from flags")
}

ctx := context.Background()
Expand All @@ -55,7 +55,7 @@ func protosCommand(ctx context.Context) *cobra.Command {
},
}

cmd.Flags().StringVar(&baseURI, "base_uri", "", "Prefix to use for the source_uri field of each proto upload")
cmd.Flags().StringVar(&baseURI, "base-uri", "", "Prefix to use for the source_uri field of each proto upload")
return cmd
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/registry/cmd/upload/csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func csvCommand(ctx context.Context) *cobra.Command {
)

cmd := &cobra.Command{
Use: "csv file --project_id=value [--delimiter=value]",
Use: "csv file --project-id=value [--delimiter=value]",
Short: "Upload API specs from a CSV file",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -85,8 +85,8 @@ func csvCommand(ctx context.Context) *cobra.Command {
},
}

cmd.Flags().StringVar(&projectID, "project_id", "", "Project ID to use for each upload")
_ = cmd.MarkFlagRequired("project_id")
cmd.Flags().StringVar(&projectID, "project-id", "", "Project ID to use for each upload")
_ = cmd.MarkFlagRequired("project-id")
cmd.Flags().StringVar(&delimiter, "delimiter", ",", "Field delimiter for the CSV file")
return cmd
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/registry/cmd/upload/csv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestUploadCSV(t *testing.T) {
desc: "multiple spec upload",
args: []string{
filepath.Join("testdata", "multiple-specs.csv"),
"--project_id", testProject,
"--project-id", testProject,
},
want: []*rpc.ApiSpec{
{
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestUploadCSV(t *testing.T) {
desc: "out of order columns",
args: []string{
filepath.Join("testdata", "out-of-order-columns.csv"),
"--project_id", testProject,
"--project-id", testProject,
},
want: []*rpc.ApiSpec{
{
Expand All @@ -109,7 +109,7 @@ func TestUploadCSV(t *testing.T) {
desc: "empty sheet",
args: []string{
filepath.Join("testdata", "empty-sheet.csv"),
"--project_id", testProject,
"--project-id", testProject,
},
want: []*rpc.ApiSpec{},
},
Expand Down
8 changes: 4 additions & 4 deletions cmd/registry/cmd/upload/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ func readManifestProto(filename string) (*rpc.Manifest, error) {
func manifestCommand(ctx context.Context) *cobra.Command {
var projectID string
cmd := &cobra.Command{
Use: "manifest FILE_PATH --project_id=value",
Use: "manifest FILE_PATH --project-id=value",
Short: "Upload a dependency manifest",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
manifestPath := args[0]
if manifestPath == "" {
log.Fatal("Please provide manifest_path")
log.Fatal("Please provide manifest-path")
}

manifest, err := readManifestProto(manifestPath)
Expand Down Expand Up @@ -97,7 +97,7 @@ func manifestCommand(ctx context.Context) *cobra.Command {
},
}

cmd.Flags().StringVar(&projectID, "project_id", "", "Project ID to use when saving the result manifest artifact")
_ = cmd.MarkFlagRequired("project_id")
cmd.Flags().StringVar(&projectID, "project-id", "", "Project ID to use when saving the result manifest artifact")
_ = cmd.MarkFlagRequired("project-id")
return cmd
}
2 changes: 1 addition & 1 deletion cmd/registry/cmd/upload/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestManifestUpload(t *testing.T) {
}

cmd := Command(ctx)
args := []string{"manifest", test.filePath, "--project_id", test.project}
args := []string{"manifest", test.filePath, "--project-id", test.project}
cmd.SetArgs(args)
if err = cmd.Execute(); err != nil {
t.Fatalf("Execute() with args %v returned error: %s", args, err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/registry/cmd/upload/styleguide.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func readStyleGuideProto(filename string) (*rpc.StyleGuide, error) {
func styleGuideCommand(ctx context.Context) *cobra.Command {
var projectID string
cmd := &cobra.Command{
Use: "styleguide FILE_PATH --project_id=value",
Use: "styleguide FILE_PATH --project-id=value",
Short: "Upload an API style guide",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
Expand Down Expand Up @@ -95,7 +95,7 @@ func styleGuideCommand(ctx context.Context) *cobra.Command {
},
}

cmd.Flags().StringVar(&projectID, "project_id", "", "Project ID to use when storing the styleguide artifact")
_ = cmd.MarkFlagRequired("project_id")
cmd.Flags().StringVar(&projectID, "project-id", "", "Project ID to use when storing the styleguide artifact")
_ = cmd.MarkFlagRequired("project-id")
return cmd
}
2 changes: 1 addition & 1 deletion cmd/registry/cmd/upload/styleguide_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestStyleGuideUpload(t *testing.T) {
}

cmd := Command(ctx)
args := []string{"styleguide", test.filePath, "--project_id", test.project}
args := []string{"styleguide", test.filePath, "--project-id", test.project}
cmd.SetArgs(args)
if err = cmd.Execute(); err != nil {
t.Fatalf("Execute() with args %v returned error: %s", args, err)
Expand Down
4 changes: 2 additions & 2 deletions cmd/registry/cmd/vocabulary/unique.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func uniqueCommand(ctx context.Context) *cobra.Command {
}

if strings.Contains(outputID, "/") {
log.Fatal("output_id must specify an artifact id (final segment only) and not a full name.")
log.Fatal("output-id must specify an artifact id (final segment only) and not a full name.")
}

ctx := context.Background()
Expand All @@ -60,6 +60,6 @@ func uniqueCommand(ctx context.Context) *cobra.Command {
},
}

cmd.Flags().StringVar(&outputID, "output_id", "vocabulary-unique", "Artifact ID to use when saving each result vocabulary")
cmd.Flags().StringVar(&outputID, "output-id", "vocabulary-unique", "Artifact ID to use when saving each result vocabulary")
return cmd
}
2 changes: 1 addition & 1 deletion cmd/registry/controller/local-tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (task *ExecCommandTask) Run(ctx context.Context) error {

// first party registry commands
if strings.HasPrefix(task.Action.Command, "registry") {
fullCmd := append(strings.Fields(task.Action.Command), fmt.Sprintf("--log_id=%s", task.TaskID))
fullCmd := append(strings.Fields(task.Action.Command), fmt.Sprintf("--log-id=%s", task.TaskID))

cmd := exec.Command(fullCmd[0], fullCmd[1:]...)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stderr
Expand Down

0 comments on commit 2f1c779

Please sign in to comment.