Skip to content

Commit

Permalink
small restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
k-anshul committed Feb 11, 2025
1 parent 70f897a commit f3d6eee
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions cli/cmd/sudo/project/dump_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ func DumpResources(ch *cmdutil.Helper) *cobra.Command {
Short: "Dump resources for projects by pattern",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

client, err := ch.Client()
if err != nil {
return err
}

pattern := "%"
var pattern string
// If args is not empty, use the first element as the pattern
if len(args) > 0 {
pattern = args[0]
} else {
pattern = "%"
}

res, err := client.SearchProjectNames(ctx, &adminv1.SearchProjectNamesRequest{
Expand All @@ -58,21 +59,21 @@ func DumpResources(ch *cmdutil.Helper) *cobra.Command {
return nil
}

failedProjects := make([]string, len(res.Names))
errors := make([]error, len(res.Names))

var m sync.Mutex
failedProjects := map[string]error{}
resources := map[string]map[string]json.RawMessage{}

grp, ctx := errgroup.WithContext(ctx)
for idx, name := range res.Names {
for _, name := range res.Names {
org := strings.Split(name, "/")[0]
project := strings.Split(name, "/")[1]

grp.Go(func() error {
row, err := dumpResourcesForProject(ctx, client, org, project, typ)
if err != nil {
failedProjects[idx] = name
errors[idx] = err
m.Lock()
failedProjects[name] = err
m.Unlock()
return nil
}
m.Lock()
Expand All @@ -92,16 +93,15 @@ func DumpResources(ch *cmdutil.Helper) *cobra.Command {
if err != nil {
return fmt.Errorf("failed to marshal resources: %w", err)
}
fmt.Println(string(jsonData))
ch.Println(string(jsonData))

for idx, failed := range failedProjects {
if failed != "" {
ch.PrintfWarn("Failed to dump resources for project %v: %s\n", failed, errors[idx])
}
for name, err := range failedProjects {
ch.Println()
ch.PrintfWarn("Failed to dump resources for project %v: %s\n", name, err)
}
if res.NextPageToken != "" {
cmd.Println()
cmd.Printf("Next page token: %s\n", res.NextPageToken)
ch.Println()
ch.Printf("Next page token: %s\n", res.NextPageToken)
}

return nil
Expand Down

0 comments on commit f3d6eee

Please sign in to comment.