11#include < future>
2- #include < fstream>
32#include < iostream>
43#include < getopt.h>
54#include < list>
65#include < boost/algorithm/string.hpp>
76#include < boost/algorithm/string/find.hpp>
87#include " common/redisreply.h"
9- #include < nlohmann/json.hpp>
108#include " sonic-db-cli.h"
119
1210using namespace swss ;
1311using namespace std ;
1412
15- void initializeGlobalConfig ()
16- {
17- SonicDBConfig::initializeGlobalConfig (SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE);
18- }
19-
20- void initializeConfig (const string& container_name = " " )
21- {
22- if (container_name.empty ())
23- {
24- SonicDBConfig::initialize (SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE);
25- }
26- else
27- {
28- auto path = getContainerFilePath (container_name, SONIC_DB_CONFIG_DIR, SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE);
29- SonicDBConfig::initialize (path);
30- }
31- };
32-
3313void printUsage ()
3414{
35- cout << " usage: sonic-db-cli [-h] [-s] [-n NAMESPACE] [-c CONTAINER_NAME] db_or_op [cmd [cmd ...]]" << endl;
15+ cout << " usage: sonic-db-cli [-h] [-s] [-n NAMESPACE] db_or_op [cmd [cmd ...]]" << endl;
3616 cout << endl;
3717 cout << " SONiC DB CLI:" << endl;
3818 cout << endl;
@@ -45,8 +25,6 @@ void printUsage()
4525 cout << " -s, --unixsocket Override use of tcp_port and use unixsocket" << endl;
4626 cout << " -n NAMESPACE, --namespace NAMESPACE" << endl;
4727 cout << " Namespace string to use asic0/asic1.../asicn" << endl;
48- cout << " -c CONTAINER_NAME, --container_name CONTAINER_NAME" << endl;
49- cout << " Container name for accessing container database instead of default dpu0/dpu1.../dpu3" << endl;
5028 cout << endl;
5129 cout << " **sudo** needed for commands accesing a different namespace [-n], or using unixsocket connection [-s]" << endl;
5230 cout << endl;
@@ -138,7 +116,7 @@ int handleAllInstances(
138116 {
139117 return 1 ;
140118 }
141-
119+
142120 if (operation == " PING" )
143121 {
144122 cout << " PONG" << endl;
@@ -208,16 +186,15 @@ void parseCliArguments(
208186 Options &options)
209187{
210188 // Parse argument with getopt https://man7.org/linux/man-pages/man3/getopt.3.html
211- const char * short_options = " hsn:c: " ;
189+ const char * short_options = " hsn" ;
212190 static struct option long_options[] = {
213- {" help" , no_argument, NULL , ' h' },
214- {" unixsocket" , no_argument, NULL , ' s' },
215- {" namespace" , required_argument, NULL , ' n' },
216- {" container_name" , required_argument, NULL , ' c' },
191+ {" help" , optional_argument, NULL , ' h' },
192+ {" unixsocket" , optional_argument, NULL , ' s' },
193+ {" namespace" , optional_argument, NULL , ' n' },
217194 // The last element of the array has to be filled with zeros.
218195 {0 , 0 , 0 , 0 }
219196 };
220-
197+
221198 // prevent getopt_long print "invalid option" message.
222199 opterr = 0 ;
223200 while (optind < argc)
@@ -235,40 +212,21 @@ void parseCliArguments(
235212 break ;
236213
237214 case ' n' :
238- if (optarg )
215+ if (optind < argc )
239216 {
240- options.m_namespace = optarg;
217+ options.m_namespace = argv[optind];
218+ optind++;
241219 }
242220 else
243221 {
244222 throw invalid_argument (" namespace value is missing." );
245223 }
246224 break ;
247225
248- case ' c' :
249- if (optarg)
250- {
251- options.m_container_name = optarg;
252- }
253- else
254- {
255- throw invalid_argument (" container_name value option used but container name is missing." );
256- }
257- break ;
258-
259226 default :
260227 // argv contains unknown argument
261228 throw invalid_argument (" Unknown argument:" + string (argv[optind]));
262229 }
263-
264- if (!options.m_namespace .empty () && !options.m_container_name .empty ())
265- {
266- throw invalid_argument (" container_name and namespace flags cannot be used together." );
267- }
268- else if (options.m_unixsocket && !options.m_container_name .empty ())
269- {
270- throw invalid_argument (" container_name and unixsocket flags cannot be used together." );
271- }
272230 }
273231 else
274232 {
@@ -290,7 +248,7 @@ int sonic_db_cli(
290248 int argc,
291249 char ** argv,
292250 function<void ()> initializeGlobalConfig,
293- function<void(const string& )> initializeConfig)
251+ function<void()> initializeConfig)
294252{
295253 Options options;
296254 try
@@ -317,9 +275,6 @@ int sonic_db_cli(
317275 return 0 ;
318276 }
319277
320- // Need to reset SonicDBConfig to remove information from other database config files
321- SonicDBConfig::reset ();
322-
323278 if (!options.m_db_or_op .empty ())
324279 {
325280 auto dbOrOperation = options.m_db_or_op ;
@@ -338,7 +293,10 @@ int sonic_db_cli(
338293 {
339294 auto commands = options.m_cmd ;
340295
341- initializeConfig (options.m_container_name );
296+ if (netns.empty ())
297+ {
298+ initializeConfig ();
299+ }
342300
343301 return executeCommands (dbOrOperation, commands, netns, useUnixSocket);
344302 }
@@ -350,9 +308,11 @@ int sonic_db_cli(
350308 // sonic-db-cli catch all possible exceptions and handle it as a failure case which not return 'OK' or 'PONG'
351309 try
352310 {
353-
354- // When database_config.json does not exist, sonic-db-cli will ignore exception and return 1.
355- initializeConfig (options.m_container_name );
311+ if (netns.empty ())
312+ {
313+ // When database_config.json does not exist, sonic-db-cli will ignore exception and return 1.
314+ initializeConfig ();
315+ }
356316
357317 return handleAllInstances (netns, dbOrOperation, useUnixSocket);
358318 }
@@ -385,7 +345,7 @@ int cli_exception_wrapper(
385345 int argc,
386346 char ** argv,
387347 function<void ()> initializeGlobalConfig,
388- function<void(const string& )> initializeConfig)
348+ function<void()> initializeConfig)
389349{
390350 try
391351 {
@@ -421,31 +381,3 @@ string getCommandName(vector<string>& commands)
421381
422382 return boost::to_upper_copy<string>(commands[0 ]);
423383}
424-
425- string getContainerFilePath (const string& container_name, const string& config_directory, const string& global_config_file)
426- {
427- using json = nlohmann::json;
428- ifstream i (global_config_file);
429- json global_config = json::parse (i);
430- for (auto & element : global_config[" INCLUDES" ])
431- {
432- if (element[" container_name" ] == container_name)
433- {
434- auto relative_path = to_string (element[" include" ]);
435-
436- // remove the trailing " from the relative path (JSON string quotes)
437- if (relative_path.front () == ' "' && relative_path.back () == ' "' ) {
438- relative_path = relative_path.substr (1 , relative_path.size () - 2 );
439- }
440-
441- // remove all preceding "../" sequences from the relative path
442- while (relative_path.substr (0 , 3 ) == " ../" ) {
443- relative_path = relative_path.substr (3 );
444- }
445- std::stringstream path_stream;
446- path_stream << config_directory << " /" << relative_path;
447- return path_stream.str ();
448- }
449- }
450- throw invalid_argument (" container name " + container_name + " not found in global config file" );
451- }
0 commit comments