()
| 24 | ) |
| 25 | |
| 26 | func newOrganizationSet() *cobra.Command { |
| 27 | var orgName string |
| 28 | var setDefault bool |
| 29 | |
| 30 | cmd := &cobra.Command{ |
| 31 | Use: "set", |
| 32 | Short: "Set the current organization to be used by this CLI", |
| 33 | Example: ` |
| 34 | # Set the current organization to be used by this CLI |
| 35 | $ chainloop org set --name my-org |
| 36 | |
| 37 | # Optionally set the organization as the default one for all clients by storing the preference server-side |
| 38 | $ chainloop org set --name my-org --default |
| 39 | `, |
| 40 | RunE: func(cmd *cobra.Command, _ []string) error { |
| 41 | ctx := cmd.Context() |
| 42 | // To find the membership ID, we need to iterate and filter by org |
| 43 | membership, err := membershipFromOrg(ctx, orgName) |
| 44 | if err != nil { |
| 45 | return fmt.Errorf("getting membership: %w", err) |
| 46 | } else if membership == nil { |
| 47 | return fmt.Errorf("organization %s not found", orgName) |
| 48 | } |
| 49 | |
| 50 | // set the local state |
| 51 | if err := setLocalOrganization(orgName); err != nil { |
| 52 | return fmt.Errorf("writing config file: %w", err) |
| 53 | } |
| 54 | |
| 55 | // change the state server side |
| 56 | if setDefault { |
| 57 | var err error |
| 58 | membership, err = action.NewMembershipSet(ActionOpts).Run(ctx, membership.ID) |
| 59 | if err != nil { |
| 60 | return err |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | logger.Info().Msg("Organization switched!") |
| 65 | return output.EncodeOutput(flagOutputFormat, []*action.MembershipItem{membership}, orgMembershipTableOutput) |
| 66 | }, |
| 67 | } |
| 68 | |
| 69 | cmd.Flags().StringVar(&orgName, "name", "", "organization name to make the switch") |
| 70 | cmd.Flags().BoolVar(&setDefault, "default", false, "set this organization as the default one for all clients") |
| 71 | cobra.CheckErr(cmd.MarkFlagRequired("name")) |
| 72 | |
| 73 | return cmd |
| 74 | } |
no test coverage detected