Skip to content

Commit

Permalink
feature: Support YAML output for newly added commands (goharbor#268)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Eschenbach <[email protected]>
  • Loading branch information
Standing-Man authored and qcserestipy committed Dec 22, 2024
1 parent 2f936b0 commit 3ddbf3d
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 13 deletions.
1 change: 1 addition & 0 deletions cmd/harbor/root/artifact/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func ListArtifactCommand() *cobra.Command {

if err != nil {
log.Errorf("failed to list artifacts: %v", err)
return
}

FormatFlag := viper.GetString("output-format")
Expand Down
1 change: 1 addition & 0 deletions cmd/harbor/root/artifact/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func ViewArtifactCommmand() *cobra.Command {

if err != nil {
log.Errorf("failed to get info of an artifact: %v", err)
return
}

FormatFlag := viper.GetString("output-format")
Expand Down
9 changes: 6 additions & 3 deletions cmd/harbor/root/labels/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ func ListLabelCommand() *cobra.Command {
}
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(label)
return
err = utils.PrintFormat(label, FormatFlag)
if err != nil {
log.Error(err)
}
} else {
list.ListLabels(label.Payload)
}
list.ListLabels(label.Payload)
},
}

Expand Down
1 change: 1 addition & 0 deletions cmd/harbor/root/project/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func ListProjectCommand() *cobra.Command {

if err != nil {
log.Fatalf("failed to get projects list: %v", err)
return
}
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
Expand Down
1 change: 1 addition & 0 deletions cmd/harbor/root/project/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func LogsProjectCommmand() *cobra.Command {

if err != nil {
log.Fatalf("failed to get project logs: %v", err)
return
}

FormatFlag := viper.GetString("output-format")
Expand Down
10 changes: 7 additions & 3 deletions cmd/harbor/root/project/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ func SearchProjectCommand() *cobra.Command {
if err != nil {
log.Fatalf("failed to get projects: %v", err)
}

FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(projects)
return
err = utils.PrintFormat(projects, FormatFlag)
if err != nil {
log.Error(err)
}
} else {
list.SearchProjects(projects.Payload.Project)
}

list.SearchProjects(projects.Payload.Project)
},
}
return cmd
Expand Down
1 change: 1 addition & 0 deletions cmd/harbor/root/registry/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func ListRegistryCommand() *cobra.Command {

if err != nil {
log.Fatalf("failed to get projects list: %v", err)
return
}
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
Expand Down
1 change: 1 addition & 0 deletions cmd/harbor/root/repository/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func ListRepositoryCommand() *cobra.Command {

if err != nil {
log.Errorf("failed to list repositories: %v", err)
return
}

FormatFlag := viper.GetString("output-format")
Expand Down
11 changes: 8 additions & 3 deletions cmd/harbor/root/repository/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@ func SearchRepoCmd() *cobra.Command {
repo, err := api.SearchRepository(args[0])
if err != nil {
log.Fatalf("failed to get repositories: %v", err)
return
}

FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(repo)
return
err = utils.PrintFormat(repo, FormatFlag)
if err != nil {
log.Error(err)
}
} else {
search.SearchRepositories(repo.Payload.Repository)
}

search.SearchRepositories(repo.Payload.Repository)
},
}
return cmd
Expand Down
13 changes: 9 additions & 4 deletions cmd/harbor/root/schedule/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,21 @@ func ListScheduleCommand() *cobra.Command {
Short: "show all schedule jobs in Harbor",
Run: func(cmd *cobra.Command, args []string) {
schedule, err := api.ListSchedule(opts)

if err != nil {
log.Fatalf("failed to get schedule list: %v", err)
return
}

FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(schedule)
return
err = utils.PrintFormat(schedule, FormatFlag)
if err != nil {
log.Error(err)
}
} else {
list.ListSchedule(schedule.Payload)
}

list.ListSchedule(schedule.Payload)
},
}

Expand Down

0 comments on commit 3ddbf3d

Please sign in to comment.