(cmd *cobra.Command, args []string)
| 34 | } |
| 35 | |
| 36 | func runSwitch(cmd *cobra.Command, args []string) error { |
| 37 | tc := auth.NewTokenClient() |
| 38 | token, err := tc.GetToken() |
| 39 | if err != nil { |
| 40 | return fmt.Errorf("failed to get auth token: %w", err) |
| 41 | } |
| 42 | |
| 43 | cl, err := team.NewClient(token.Value) |
| 44 | if err != nil { |
| 45 | return fmt.Errorf("failed to create API client: %w", err) |
| 46 | } |
| 47 | |
| 48 | if len(args) == 0 { |
| 49 | // Print the current team context |
| 50 | currentTeam, err := config.GetValue("team") |
| 51 | if err != nil && !errors.Is(err, os.ErrNotExist) { |
| 52 | return fmt.Errorf("failed to get current team: %w", err) |
| 53 | } |
| 54 | |
| 55 | allTeams, err := cl.ListAllTeams(cmd.Context()) |
| 56 | if err != nil { |
| 57 | return fmt.Errorf("failed to list teams: %w", err) |
| 58 | } |
| 59 | |
| 60 | if currentTeam == "" { |
| 61 | cmd.Printf("Your current team is not set.\n\n") |
| 62 | } else { |
| 63 | cmd.Printf("Your current team is set to %v.\n\n", currentTeam) |
| 64 | } |
| 65 | teamNames := team.Names(allTeams) |
| 66 | cmd.Println("Teams available to you:", strings.Join(teamNames, ", ")+"\n") |
| 67 | cmd.Println("To switch teams, run `cloudquery switch <team>`") |
| 68 | return nil |
| 69 | } |
| 70 | selectedTeam := args[0] |
| 71 | err = cl.ValidateTeam(cmd.Context(), selectedTeam) |
| 72 | if err != nil { |
| 73 | return fmt.Errorf("failed to switch teams: %w", err) |
| 74 | } |
| 75 | |
| 76 | teamInfo, err := cl.GetTeam(cmd.Context(), selectedTeam) |
| 77 | if err != nil { |
| 78 | return fmt.Errorf("failed to get team: %w", err) |
| 79 | } |
| 80 | |
| 81 | err = config.SetValue("team", selectedTeam) |
| 82 | if err != nil { |
| 83 | return fmt.Errorf("failed to set team value: %w", err) |
| 84 | } |
| 85 | |
| 86 | teamInternalStr := "false" |
| 87 | if teamInfo.Internal { |
| 88 | teamInternalStr = "true" |
| 89 | } |
| 90 | |
| 91 | err = config.SetValue("team_internal", teamInternalStr) |
| 92 | if err != nil { |
| 93 | return fmt.Errorf("failed to set team metadata: %w", err) |
nothing calls this directly
no test coverage detected