11"""
2- SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2+ SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES.
3+ All rights reserved.
34
45Licensed under the Apache License, Version 2.0 (the "License");
56you may not use this file except in compliance with the License.
@@ -38,7 +39,9 @@ def setup_parser(parser: argparse._SubParsersAction):
3839 login_parser = parser .add_parser ('login' ,
3940 help = 'Log in with device flow or client credentials flow.' )
4041 login_parser .set_defaults (func = _login )
41- login_parser .add_argument ('url' , help = 'The url of the osmo server to connect to' )
42+ login_parser .add_argument ('url' , nargs = '?' , default = None ,
43+ help = 'The url of the osmo server to connect to. '
44+ 'If not provided, uses the last used url.' )
4245 login_parser .add_argument ('--device-endpoint' ,
4346 help = 'The url to use to completed device flow authentication. ' +
4447 'If not provided, it will be fetched from the service.' )
@@ -71,13 +74,23 @@ def setup_parser(parser: argparse._SubParsersAction):
7174
7275
7376def _login (service_client : client .ServiceClient , args : argparse .Namespace ):
77+ # Get the url from args or fall back to last used url
78+ try :
79+ url = args .url or service_client .login_manager .login_storage .url
80+ except osmo_errors .OSMOUserError as error :
81+ raise osmo_errors .OSMOUserError (
82+ 'No url provided and no previous login found. '
83+ 'Please provide a url: osmo login <url>' ) from error
84+
7485 # Validate the url
7586 class UrlValidator (pydantic .BaseModel ):
7687 url : pydantic .AnyHttpUrl
7788 try :
78- _ = UrlValidator (url = args . url )
89+ _ = UrlValidator (url = url )
7990 except pydantic .error_wrappers .ValidationError as error :
80- raise osmo_errors .OSMOUserError (f'Bad url { args .url } : { error } ' )
91+ raise osmo_errors .OSMOUserError (f'Bad url { url } : { error } ' )
92+
93+ print (f'Logging in to { url } ' )
8194
8295 # Parse out the password and username
8396 username = args .username
@@ -90,37 +103,37 @@ class UrlValidator(pydantic.BaseModel):
90103 device_endpoint = args .device_endpoint
91104 client_id : str | None = None
92105 if not device_endpoint :
93- login_info = login .fetch_login_info (args . url )
106+ login_info = login .fetch_login_info (url )
94107 device_endpoint = login_info ['device_endpoint' ]
95108 client_id = login_info ['device_client_id' ]
96109
97110 # Login through device code flow
98111 if args .method == 'code' :
99- service_client .login_manager .device_code_login (args . url , device_endpoint , client_id )
112+ service_client .login_manager .device_code_login (url , device_endpoint , client_id )
100113
101114 # Login through resource owner password flow
102115 elif args .method == 'password' :
103116 if username is None :
104117 raise osmo_errors .OSMOUserError ('Must provide username' )
105118 if password is None :
106119 raise osmo_errors .OSMOUserError ('Must provide password' )
107- service_client .login_manager .owner_password_login (args . url , username , password )
120+ service_client .login_manager .owner_password_login (url , username , password )
108121
109122 # Login by directly reading the idtoken from a file
110123 elif args .method == 'token' :
111124 if args .token_file :
112125 refresh_url = args .token_file .read ().strip ()
113126 elif args .token :
114- refresh_url = login .construct_token_refresh_url (args . url , args .token )
127+ refresh_url = login .construct_token_refresh_url (url , args .token )
115128 else :
116129 raise osmo_errors .OSMOUserError ('Must provide token file with --token_file or --token' )
117- service_client .login_manager .token_login (args . url , refresh_url )
130+ service_client .login_manager .token_login (url , refresh_url )
118131
119132 # For developers, simply send username as a header
120133 else :
121134 if username is None :
122135 raise osmo_errors .OSMOUserError ('Must provide username' )
123- service_client .login_manager .dev_login (args . url , username )
136+ service_client .login_manager .dev_login (url , username )
124137
125138
126139def _logout (service_client : client .ServiceClient , args : argparse .Namespace ):
0 commit comments