Skip to content

Commit

Permalink
RSDK-9758 - refactor ensureLoggedIn call sites (#4784)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuqdog authored and abe-winter committed Feb 26, 2025
1 parent af8d349 commit f668123
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 246 deletions.
22 changes: 0 additions & 22 deletions cli/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,6 @@ func PrintAccessTokenAction(cCtx *cli.Context, args emptyArgs) error {
}

func (c *viamClient) printAccessTokenAction(cCtx *cli.Context) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

if token, ok := c.conf.Auth.(*token); ok {
printf(cCtx.App.Writer, token.AccessToken)
} else {
Expand Down Expand Up @@ -316,9 +312,6 @@ func OrganizationsAPIKeyCreateAction(cCtx *cli.Context, args organizationsAPIKey
}

func (c *viamClient) organizationsAPIKeyCreateAction(cCtx *cli.Context, args organizationsAPIKeyCreateArgs) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}
orgID := args.OrgID
keyName := args.Name
if keyName == "" {
Expand All @@ -337,10 +330,6 @@ func (c *viamClient) organizationsAPIKeyCreateAction(cCtx *cli.Context, args org
}

func (c *viamClient) createOrganizationAPIKey(orgID, keyName string) (*apppb.CreateKeyResponse, error) {
if err := c.ensureLoggedIn(); err != nil {
return nil, err
}

req := &apppb.CreateKeyRequest{
Authorizations: []*apppb.Authorization{
{
Expand Down Expand Up @@ -376,10 +365,6 @@ func LocationAPIKeyCreateAction(cCtx *cli.Context, args locationAPIKeyCreateArgs
}

func (c *viamClient) locationAPIKeyCreateAction(cCtx *cli.Context, args locationAPIKeyCreateArgs) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

locationID := args.LocationID
orgID := args.OrgID
keyName := args.Name
Expand Down Expand Up @@ -441,10 +426,6 @@ func RobotAPIKeyCreateAction(cCtx *cli.Context, args robotAPIKeyCreateArgs) erro
}

func (c *viamClient) robotAPIKeyCreateAction(cCtx *cli.Context, args robotAPIKeyCreateArgs) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

robotID := args.MachineID
keyName := args.Name
orgID := args.OrgID
Expand Down Expand Up @@ -600,9 +581,6 @@ func (c *viamClient) prepareDial(
orgStr, locStr, robotStr, partStr string,
debug bool,
) (context.Context, string, []rpc.DialOption, error) {
if err := c.ensureLoggedIn(); err != nil {
return nil, "", nil, err
}
if err := c.selectOrganization(orgStr); err != nil {
return nil, "", nil, err
}
Expand Down
107 changes: 8 additions & 99 deletions cli/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,6 @@ func OrganizationsSupportEmailSetAction(cCtx *cli.Context, args organizationsSup
}

func (c *viamClient) organizationsSupportEmailSetAction(cCtx *cli.Context, orgID, supportEmail string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

_, err := c.client.OrganizationSetSupportEmail(c.c.Context, &apppb.OrganizationSetSupportEmailRequest{
OrgId: orgID,
Email: supportEmail,
Expand Down Expand Up @@ -181,10 +177,6 @@ func OrganizationsSupportEmailGetAction(cCtx *cli.Context, args organizationsSup
}

func (c *viamClient) organizationsSupportEmailGetAction(cCtx *cli.Context, orgID string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

resp, err := c.client.OrganizationGetSupportEmail(c.c.Context, &apppb.OrganizationGetSupportEmailRequest{
OrgId: orgID,
})
Expand Down Expand Up @@ -242,10 +234,6 @@ func (c *viamClient) disableAuthServiceAction(cCtx *cli.Context, orgID string) e
return errors.New("cannot disable auth service without an organization ID")
}

if err := c.ensureLoggedIn(); err != nil {
return err
}

if _, err := c.client.DisableAuthService(cCtx.Context, &apppb.DisableAuthServiceRequest{OrgId: orgID}); err != nil {
return err
}
Expand Down Expand Up @@ -274,10 +262,6 @@ func EnableAuthServiceAction(cCtx *cli.Context, args enableAuthServiceArgs) erro
}

func (c *viamClient) enableAuthServiceAction(cCtx *cli.Context, orgID string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

_, err := c.client.EnableAuthService(cCtx.Context, &apppb.EnableAuthServiceRequest{OrgId: orgID})
if err != nil {
return err
Expand Down Expand Up @@ -312,9 +296,6 @@ func UpdateBillingServiceAction(cCtx *cli.Context, args updateBillingServiceArgs
}

func (c *viamClient) updateBillingServiceAction(cCtx *cli.Context, orgID, addressAsString string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}
address, err := parseBillingAddress(addressAsString)
if err != nil {
return err
Expand Down Expand Up @@ -355,10 +336,6 @@ func GetBillingConfigAction(cCtx *cli.Context, args getBillingConfigArgs) error
}

func (c *viamClient) getBillingConfig(cCtx *cli.Context, orgID string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

resp, err := c.client.GetBillingServiceConfig(cCtx.Context, &apppb.GetBillingServiceConfigRequest{
OrgId: orgID,
})
Expand Down Expand Up @@ -411,10 +388,6 @@ func OrganizationEnableBillingServiceAction(cCtx *cli.Context, args organization
}

func (c *viamClient) organizationEnableBillingServiceAction(cCtx *cli.Context, orgID, addressAsString string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

address, err := parseBillingAddress(addressAsString)
if err != nil {
return err
Expand Down Expand Up @@ -449,10 +422,6 @@ func OrganizationDisableBillingServiceAction(cCtx *cli.Context, args organizatio
}

func (c *viamClient) organizationDisableBillingServiceAction(cCtx *cli.Context, orgID string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

if _, err := c.client.DisableBillingService(cCtx.Context, &apppb.DisableBillingServiceRequest{
OrgId: orgID,
}); err != nil {
Expand Down Expand Up @@ -488,10 +457,6 @@ func OrganizationLogoSetAction(cCtx *cli.Context, args organizationsLogoSetArgs)
}

func (c *viamClient) organizationLogoSetAction(cCtx *cli.Context, orgID, logoFilePath string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

logoFile, err := os.Open(filepath.Clean(logoFilePath))
if err != nil {
return errors.WithMessagef(err, "could not open logo file: %s", logoFilePath)
Expand Down Expand Up @@ -544,10 +509,6 @@ func OrganizationsLogoGetAction(cCtx *cli.Context, args organizationsLogoGetArgs
}

func (c *viamClient) organizationsLogoGetAction(cCtx *cli.Context, orgID string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

resp, err := c.client.OrganizationGetLogo(cCtx.Context, &apppb.OrganizationGetLogoRequest{
OrgId: orgID,
})
Expand Down Expand Up @@ -584,10 +545,6 @@ func ListOAuthAppsAction(cCtx *cli.Context, args listOAuthAppsArgs) error {
}

func (c *viamClient) listOAuthAppsAction(cCtx *cli.Context, orgID string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

resp, err := c.client.ListOAuthApps(cCtx.Context, &apppb.ListOAuthAppsRequest{
OrgId: orgID,
})
Expand Down Expand Up @@ -1448,10 +1405,6 @@ func tunnelTraffic(ctx *cli.Context, robotClient *client.RobotClient, local, des
}

func (c *viamClient) robotPartTunnel(cCtx *cli.Context, args robotsPartTunnelArgs) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

orgStr := args.Organization
locStr := args.Location
robotStr := args.Machine
Expand Down Expand Up @@ -1761,7 +1714,14 @@ func newViamClientInner(c *cli.Context, disableBrowserOpen bool) (*viamClient, e
// Creates a new viam client, defaulting to _not_ passing the `disableBrowerOpen` arg (which
// users don't even have an option of setting for any CLI method currently except `Login`).
func newViamClient(c *cli.Context) (*viamClient, error) {
return newViamClientInner(c, false)
client, err := newViamClientInner(c, false)
if err != nil {
return nil, err
}
if err := client.ensureLoggedIn(); err != nil {
return nil, err
}
return client, nil
}

func (c *viamClient) loadOrganizations() error {
Expand All @@ -1774,9 +1734,6 @@ func (c *viamClient) loadOrganizations() error {
}

func (c *viamClient) selectOrganization(orgStr string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}
if orgStr != "" && (c.selectedOrg.Id == orgStr || c.selectedOrg.Name == orgStr) {
return nil
}
Expand Down Expand Up @@ -1822,9 +1779,6 @@ func (c *viamClient) selectOrganization(orgStr string) error {
// org UUID, then this matchs on organization ID, otherwise this will match
// on organization name.
func (c *viamClient) getOrg(orgStr string) (*apppb.Organization, error) {
if err := c.ensureLoggedIn(); err != nil {
return nil, err
}
resp, err := c.client.ListOrganizations(c.c.Context, &apppb.ListOrganizationsRequest{})
if err != nil {
return nil, err
Expand All @@ -1849,10 +1803,6 @@ func (c *viamClient) getOrg(orgStr string) (*apppb.Organization, error) {
// getUserOrgByPublicNamespace searches the logged in users orgs to see
// if any have a matching public namespace.
func (c *viamClient) getUserOrgByPublicNamespace(publicNamespace string) (*apppb.Organization, error) {
if err := c.ensureLoggedIn(); err != nil {
return nil, err
}

if err := c.loadOrganizations(); err != nil {
return nil, err
}
Expand All @@ -1865,9 +1815,6 @@ func (c *viamClient) getUserOrgByPublicNamespace(publicNamespace string) (*apppb
}

func (c *viamClient) listOrganizations() ([]*apppb.Organization, error) {
if err := c.ensureLoggedIn(); err != nil {
return nil, err
}
if err := c.loadOrganizations(); err != nil {
return nil, err
}
Expand Down Expand Up @@ -1921,9 +1868,6 @@ func (c *viamClient) selectLocation(locStr string) error {
}

func (c *viamClient) listLocations(orgID string) ([]*apppb.Location, error) {
if err := c.ensureLoggedIn(); err != nil {
return nil, err
}
if err := c.selectOrganization(orgID); err != nil {
return nil, err
}
Expand All @@ -1934,9 +1878,6 @@ func (c *viamClient) listLocations(orgID string) ([]*apppb.Location, error) {
}

func (c *viamClient) listRobots(orgStr, locStr string) ([]*apppb.Robot, error) {
if err := c.ensureLoggedIn(); err != nil {
return nil, err
}
if err := c.selectOrganization(orgStr); err != nil {
return nil, err
}
Expand All @@ -1953,10 +1894,6 @@ func (c *viamClient) listRobots(orgStr, locStr string) ([]*apppb.Robot, error) {
}

func (c *viamClient) robot(orgStr, locStr, robotStr string) (*apppb.Robot, error) {
if err := c.ensureLoggedIn(); err != nil {
return nil, err
}

robots, err := c.listRobots(orgStr, locStr)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1995,9 +1932,6 @@ func (c *viamClient) robotPart(orgStr, locStr, robotStr, partStr string) (*apppb
}

func (c *viamClient) robotPartInner(orgStr, locStr, robotStr, partStr string) (*apppb.RobotPart, error) {
if err := c.ensureLoggedIn(); err != nil {
return nil, err
}
parts, err := c.robotParts(orgStr, locStr, robotStr)
if err != nil {
return nil, err
Expand Down Expand Up @@ -2033,16 +1967,10 @@ func (c *viamClient) robotPartInner(orgStr, locStr, robotStr, partStr string) (*
// note: overlaps with viamClient.robotPart, which wraps GetRobotParts.
// Use this variant if you don't know the robot ID.
func (c *viamClient) getRobotPart(partID string) (*apppb.GetRobotPartResponse, error) {
if err := c.ensureLoggedIn(); err != nil {
return nil, err
}
return c.client.GetRobotPart(c.c.Context, &apppb.GetRobotPartRequest{Id: partID})
}

func (c *viamClient) updateRobotPart(part *apppb.RobotPart, confMap map[string]any) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}
confStruct, err := structpb.NewStruct(confMap)
if err != nil {
return errors.Wrap(err, "in NewStruct")
Expand Down Expand Up @@ -2100,9 +2028,6 @@ func (c *viamClient) robotPartLogs(orgStr, locStr, robotStr, partStr string, err
}

func (c *viamClient) robotParts(orgStr, locStr, robotStr string) ([]*apppb.RobotPart, error) {
if err := c.ensureLoggedIn(); err != nil {
return nil, err
}
robot, err := c.robot(orgStr, locStr, robotStr)
if err != nil {
return nil, err
Expand Down Expand Up @@ -2630,10 +2555,6 @@ func ReadOAuthAppAction(c *cli.Context, args readOAuthAppArgs) error {
}

func (c *viamClient) readOAuthAppAction(cCtx *cli.Context, orgID, clientID string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

req := &apppb.ReadOAuthAppRequest{OrgId: orgID, ClientId: clientID}
resp, err := c.client.ReadOAuthApp(c.c.Context, req)
if err != nil {
Expand Down Expand Up @@ -2709,10 +2630,6 @@ func DeleteOAuthAppAction(c *cli.Context, args deleteOAuthAppArgs) error {
}

func (c *viamClient) deleteOAuthAppAction(cCtx *cli.Context, orgID, clientID string) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

req := &apppb.DeleteOAuthAppRequest{
OrgId: orgID,
ClientId: clientID,
Expand Down Expand Up @@ -2857,10 +2774,6 @@ func CreateOAuthAppAction(c *cli.Context, args createOAuthAppArgs) error {
}

func (c *viamClient) createOAuthAppAction(cCtx *cli.Context, args createOAuthAppArgs) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

config, err := generateOAuthConfig(args.ClientAuthentication, args.Pkce, args.UrlValidation,
args.LogoutURI, args.OriginURIs, args.RedirectURIs, args.EnabledGrants)
if err != nil {
Expand Down Expand Up @@ -2907,10 +2820,6 @@ func UpdateOAuthAppAction(c *cli.Context, args updateOAuthAppArgs) error {
}

func (c *viamClient) updateOAuthAppAction(cCtx *cli.Context, args updateOAuthAppArgs) error {
if err := c.ensureLoggedIn(); err != nil {
return err
}

req, err := createUpdateOAuthAppRequest(args)
if err != nil {
return err
Expand Down
Loading

0 comments on commit f668123

Please sign in to comment.