|
| 1 | +package get |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + storage "github.com/ninech/apis/storage/v1alpha1" |
| 8 | + "github.com/ninech/nctl/api" |
| 9 | + "github.com/ninech/nctl/internal/format" |
| 10 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 11 | +) |
| 12 | + |
| 13 | +type bucketUserCmd struct { |
| 14 | + resourceCmd |
| 15 | + PrintCredentials bool `help:"Print the credentials of the BucketUser. Requires name to be set." xor:"cred"` |
| 16 | + PrintAccessKey bool `help:"Print the access key of the BucketUser. Requires name to be set." xor:"access"` |
| 17 | + PrintSecretKey bool `help:"Print the secret key of the BucketUser. Requires name to be set." xor:"secret"` |
| 18 | +} |
| 19 | + |
| 20 | +func (bu *bucketUserCmd) Run(ctx context.Context, client *api.Client, get *Cmd) error { |
| 21 | + return get.listPrint(ctx, client, bu, api.MatchName(bu.Name)) |
| 22 | +} |
| 23 | + |
| 24 | +func (bu *bucketUserCmd) list() client.ObjectList { |
| 25 | + return &storage.BucketUserList{} |
| 26 | +} |
| 27 | + |
| 28 | +func (bu *bucketUserCmd) print(ctx context.Context, client *api.Client, list client.ObjectList, out *output) error { |
| 29 | + bucketUserList := list.(*storage.BucketUserList) |
| 30 | + |
| 31 | + if len(bucketUserList.Items) == 0 { |
| 32 | + return out.printEmptyMessage(storage.BucketUserKind, client.Project) |
| 33 | + } |
| 34 | + |
| 35 | + user := &bucketUserList.Items[0] |
| 36 | + |
| 37 | + if bu.printFlagSet() { |
| 38 | + if bu.Name != "" { |
| 39 | + return fmt.Errorf("name needs to be set to print bucket user information") |
| 40 | + } |
| 41 | + |
| 42 | + if bu.PrintCredentials { |
| 43 | + return bu.printCredentials(ctx, client, user, out, nil) |
| 44 | + } |
| 45 | + |
| 46 | + key := "" |
| 47 | + if bu.PrintAccessKey { |
| 48 | + key = storage.BucketUserCredentialAccessKey |
| 49 | + } |
| 50 | + |
| 51 | + if bu.PrintSecretKey { |
| 52 | + key = storage.BucketUserCredentialSecretKey |
| 53 | + } |
| 54 | + return bu.printSecret(ctx, client, user, key) |
| 55 | + } |
| 56 | + |
| 57 | + switch out.Format { |
| 58 | + case full: |
| 59 | + return bu.printBucketUserInstances(bucketUserList.Items, out, true) |
| 60 | + case noHeader: |
| 61 | + return bu.printBucketUserInstances(bucketUserList.Items, out, false) |
| 62 | + case yamlOut: |
| 63 | + return format.PrettyPrintObjects(bucketUserList.GetItems(), format.PrintOpts{Out: out.writer}) |
| 64 | + case jsonOut: |
| 65 | + return format.PrettyPrintObjects( |
| 66 | + bucketUserList.GetItems(), |
| 67 | + format.PrintOpts{ |
| 68 | + Out: out.writer, |
| 69 | + Format: format.OutputFormatTypeJSON, |
| 70 | + JSONOpts: format.JSONOutputOptions{ |
| 71 | + PrintSingleItem: bu.Name != "", |
| 72 | + }, |
| 73 | + }) |
| 74 | + } |
| 75 | + |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +func (bu *bucketUserCmd) printBucketUserInstances(list []storage.BucketUser, out *output, header bool) error { |
| 80 | + if header { |
| 81 | + out.writeHeader("NAME", "LOCATION") |
| 82 | + } |
| 83 | + |
| 84 | + for _, bu := range list { |
| 85 | + out.writeTabRow(bu.Namespace, bu.Name, string(bu.Spec.ForProvider.Location)) |
| 86 | + } |
| 87 | + |
| 88 | + return out.tabWriter.Flush() |
| 89 | +} |
| 90 | + |
| 91 | +func (bu *bucketUserCmd) printFlagSet() bool { |
| 92 | + return bu.PrintCredentials || bu.PrintAccessKey || bu.PrintSecretKey |
| 93 | +} |
| 94 | + |
| 95 | +func (bu *bucketUserCmd) printSecret(ctx context.Context, client *api.Client, user *storage.BucketUser, key string) error { |
| 96 | + data, err := getConnectionSecret(ctx, client, key, user) |
| 97 | + if err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + fmt.Printf("%s\n", data) |
| 101 | + return nil |
| 102 | +} |
0 commit comments