@@ -21,8 +21,8 @@ if (sys.platform != "win32"):
21
21
import fcntl
22
22
import errno
23
23
import datetime
24
- VERSION = "1.7 "
25
- UPDATED = "2023-05-02 "
24
+ VERSION = "1.8 "
25
+ UPDATED = "2023-06-12 "
26
26
27
27
# ==============================================================================
28
28
# Global variables
@@ -57,7 +57,6 @@ default_out_spec = 'name{:.30}#shape#cidr-block#prohibit-public{:5.5}' \
57
57
'#token'
58
58
59
59
matches = [] # list of commands that match user input
60
- best_match = None # matches[best_match] is the closest match
61
60
all_options_for_this_cmd = '' # for best_match
62
61
remaining_options = '' # for best_match
63
62
options_out = [] # list of expanded options to be passed to oci cli
@@ -91,11 +90,13 @@ def usage(s=''):
91
90
<oci-options> Options and parameters for <oci-command>
92
91
These will be expanded into a more correct oci command
93
92
94
- . or go Run the oci command if the line ends with "." (or "go")""" )
93
+ . or go If the line ends with "." (or "go") run the oci command
94
+ ! If line ends with "!", force-run the command, even if
95
+ param substitution fails""" )
95
96
96
97
if s != "help" :
97
98
print ("""
98
- o help More "o" options. Use "--help ." to get oci help """ )
99
+ o help More "o" options and usage. """ )
99
100
else :
100
101
print ("""
101
102
More output options:
@@ -122,7 +123,15 @@ def usage(s=''):
122
123
123
124
o prune "name" Remove OCIDs for resources matching "name" from
124
125
$HOME/.oci/ocids. If "name" is a compartment, remove
125
- all OCIDS under compartment "name".""" )
126
+ all OCIDS under compartment "name".
127
+
128
+ touch $HOME/.oci/.otmp Activate "save last result" feature. With this
129
+ you can reformat output from the last command
130
+ without re-running the command with:
131
+ o -o <fmt>
132
+
133
+ o <oci-command> help Show all parameters, including globals
134
+ o <oci-command> --help . Get help from oci""" )
126
135
print ("""
127
136
o Version {} - {}
128
137
""" .format (VERSION , UPDATED ))
@@ -271,10 +280,9 @@ def setup_ocids_file():
271
280
272
281
and I'll do this for you:
273
282
274
- o iam compartment get -c <tenancy-ocid> go
275
- o iam ad list -c <tenancy-ocid> go
276
- o iam region list go
277
- o iam compartment list -c <tenancy-ocid> -ciis true -all go
283
+ o iam compartment list -c <tenancy-ocid> -ciis true -all .
284
+ o iam ad list -c <tenancy-ocid> .
285
+ o iam region-subscription list .
278
286
""" )
279
287
280
288
# To be extra helpful, get likely tenancy ocid from oci config file
@@ -293,23 +301,30 @@ and I'll do this for you:
293
301
if len (sys .argv ) == 3 :
294
302
print ("Tenancy name:" , sys .argv [2 ])
295
303
print (bold ('Setting up your ' + ocids_file_name + ' file:' ))
296
- if run_command (sys .argv [0 ] + ' -o name iam compartment get -c ' + sys .argv [1 ] + ' go' ):
304
+ print (bold ('\n Getting compartment names and ocids...' ), end = '' )
305
+ if run_command (sys .argv [0 ] + ' -o name iam compartment list -c ' + sys .argv [1 ] + ' -ciis true -all .' ):
306
+ print ('\n That didn\' t work. Your account may need additional privileges.' )
307
+ exit (0 )
308
+ print (bold ('\n Getting availability domains...' ), end = '' )
309
+ run_command (sys .argv [0 ] + ' -o name iam ad list -c ' + sys .argv [1 ] + ' .' )
310
+ print (bold ('\n Getting regions...' ), end = '' )
311
+ run_command (sys .argv [0 ] + ' -o name iam region-subscription list .' )
312
+ print (bold ('\n Getting tenancy name from root compartment...' ), end = '' )
313
+ if run_command (sys .argv [0 ] + ' -o name iam compartment get -c ' + sys .argv [1 ] + ' .' ):
297
314
# compartment get on tenancy failed; try saving a stub for tenancy
298
- print ("\n That didn't work. Creating a stub for tenancy root." )
315
+ print ("\n Could not get root compartment." )
316
+ tenancy_name = input (bold ("Enter tenancy name: " ))
317
+ if not tenancy_name :
318
+ tenancy_name = 'root'
299
319
newids = {'root' : {
300
320
'type' : 'tenancy' ,
301
- 'alias' : 'root' if len (sys .argv ) == 2
302
- else sys .argv [2 ],
321
+ 'alias' : tenancy_name ,
303
322
'id' : sys .argv [1 ],
304
- 'name' : 'root' if len ( sys . argv ) == 2 else sys . argv [ 2 ]
323
+ 'name' : tenancy_name ,
305
324
},
306
325
}
307
326
ocids_file ('write' , newids )
308
327
exit (0 )
309
- run_command (sys .argv [0 ] + ' -o name iam ad list -c ' + sys .argv [1 ] + ' go' )
310
- run_command (sys .argv [0 ] + ' -o name iam region list go' )
311
- run_command (sys .argv [0 ] + ' -o name iam compartment list -c '
312
- + sys .argv [1 ] + ' -ciis true -all go' )
313
328
print ('\n ' + bold ('All set. Have fun!\n ' ))
314
329
315
330
exit (0 )
@@ -342,7 +357,7 @@ def ocids_file(action, new_ocids={}):
342
357
# Run setup if no ocids_file exists or
343
358
# if this is an "o <tenancy>" setup command and new_ocids is empty
344
359
if (not new_ocids
345
- and (not re .search ('get .*-c ocid..tenancy' , ' ' .join (sys .argv ))
360
+ and (not ( re .search ('iam compartment list .*-c ocid..tenancy' , ' ' .join (sys .argv ) ))
346
361
and (not os .path .exists (ocids_file_name )
347
362
or re .search ('o [^c]*ocid..tenancy' , ' ' .join (sys .argv ))))):
348
363
setup_ocids_file ()
@@ -570,10 +585,8 @@ def args_match_command(c, params):
570
585
# accept plurals in place of singulars
571
586
if (len (a ) > 3 and a [- 1 ] == 's' and a not in ['address' , 'access' , 'waas' ]):
572
587
a = a [:- 1 ]
573
-
574
- # stop searching at first -opt
575
- if (len (a ) and a [0 ] == '-' ):
576
- return (True )
588
+ if a [- 2 :] == 'ie' :
589
+ a = a [:- 2 ]
577
590
578
591
# Look for full-word match first
579
592
if ' ' + a + ' ' in action :
@@ -584,7 +597,7 @@ def args_match_command(c, params):
584
597
# look for word starting with arg
585
598
if ' ' + a in action or '-' + a in action :
586
599
# Scratch out this match
587
- action = re .sub (' ' + a + r'\S+' , ' ' , action , count = 1 )
600
+ action = re .sub (' ' + r'(\S+-)*' + a + r'( \S+) ' , ' ' , action , count = 1 )
588
601
continue
589
602
590
603
# look for rlc -> really-long-command
@@ -1163,7 +1176,7 @@ def jdump_item(item, indent=2):
1163
1176
re .sub (r'(?m)^[\s{}[\],]+\n' , '' , # noqa: E128
1164
1177
# noqa: E128 change key: [ value ] -> key: value
1165
1178
re .sub (r': \[\s+("[^"]*")\s+]' , r': \1' , # noqa: E128
1166
- json .dumps (item , indent = indent ))))))) # noqa: E128
1179
+ json .dumps (item , indent = indent , ensure_ascii = False ))))))) # noqa: E128
1167
1180
1168
1181
# ==============================================================================
1169
1182
@@ -1212,7 +1225,7 @@ def show_item(item, fields, sep):
1212
1225
else :
1213
1226
out .append (value )
1214
1227
else :
1215
- if field == 'id' and sep == '|' and user_out_spec == default_out_spec :
1228
+ if field == 'id' and sep == '|' and user_out_spec == default_out_spec and 'ocid' in value :
1216
1229
out .append (' ' + value .split ('.' )[3 ])
1217
1230
out .append (column [field ]['format' ].format (re .sub (r'[{}\'[\]\n]' , '' , value )[
1218
1231
column [field ]['offset' ]:]))
@@ -1959,20 +1972,19 @@ elif 'help' == CLI_params[-1]:
1959
1972
# ==============================================================================
1960
1973
# Identify matching oci cli command(s)
1961
1974
# ==============================================================================
1962
- options_in_args = any (len (i ) > 0 and '-' in i [0 ] for i in CLI_params )
1975
+ command_words = [i for i , w in enumerate (CLI_params ) if w .startswith ('-' )]
1976
+ ncw = command_words [0 ] if command_words else len (CLI_params )
1977
+ options_in_args = len (command_words ) > 0
1963
1978
1964
- # Prioritize core services above others. The list could include: os, fs, bv
1965
- # But these already have priority due to their short service names.
1966
- # Each service name must end with ' '.
1967
- priority_services = ('compute ' , 'bv ' , 'os ' , 'network ' , 'iam ' )
1979
+ # Prioritize core services above others.
1980
+ priority_services = ('compute ' , 'bv ' , 'os ' , 'network ' , 'iam ' , 'db ' )
1968
1981
1969
1982
# Find matching commands. Sort by priority, then by command length.
1970
- matches = sorted ([c for c in command if args_match_command (c , CLI_params )],
1983
+ matches = sorted ([c for c in command if args_match_command (c , CLI_params [: ncw ] )],
1971
1984
key = lambda c : (len (c ['action' ]) if len (c ['action' ]) < 25 else int (round (len (c ['action' ]) / 10. ) * 10. ))
1972
1985
if c ['action' ].startswith (priority_services )
1973
1986
else (1.5 * len (c ['action' ]) if len (c ['action' ]) < 25 else 1.5 * int (round (len (c ['action' ]) / 10. ) * 10. )),
1974
1987
reverse = True )
1975
- best_match = len (matches ) - 1
1976
1988
1977
1989
# ==============================================================================
1978
1990
# Take action - either show help or 'go' run the command
@@ -1982,8 +1994,8 @@ if len(matches) == 0:
1982
1994
+ '\n \n Try: o .' )
1983
1995
1984
1996
# If many matches, list matches and exit
1985
- elif not (len (matches ) <= 16 or ( 'db' in CLI_params and len ( matches ) <= 50 )
1986
- or matches [best_match ]['action' ].split (' ' )[0 ] + ' ' in priority_services ):
1997
+ elif not (len (matches ) <= 16
1998
+ or ( matches [- 1 ]['action' ].split (' ' )[0 ] + ' ' in priority_services and len ( matches ) < 200 and ncw > 1 ) ):
1987
1999
print (bold ('Possible commands:' ))
1988
2000
for c in matches :
1989
2001
show_command (c , prefix = ' ' )
@@ -1994,28 +2006,30 @@ elif not (len(matches) <= 16 or ('db' in CLI_params and len(matches) <= 50)
1994
2006
elif show_help :
1995
2007
for c in matches :
1996
2008
show_command (c , full = True )
1997
- print ('use:' , bold ('o ' + c ['action' ] + ' --help go' ), 'for full oci help' )
2009
+ print ('For oci help:' , bold ('o ' + c ['action' ] + ' --help .\n ' )
2010
+ + 'https://docs.oracle.com/en-us/iaas/tools/oci-cli/latest/oci_cli_docs/cmdref/'
2011
+ + c ['action' ].replace (' ' , '/' ) + '.html' )
1998
2012
exit (0 )
1999
2013
2000
2014
# Fewer than 16 matches. Didn't ask for help.
2001
2015
elif not go and quiet < 1 :
2002
2016
if len (matches ) > 1 :
2003
2017
if options_in_args :
2004
- print (bold (str (len (matches )) + ' other matching commands not shown.'
2018
+ print (bold (str (len (matches ) - 1 ) + ' other matching commands not shown.'
2005
2019
+ ' (Remove parameters to see other commands.)' ), end = '' )
2006
2020
else :
2007
2021
print (bold ('Possible matches:' ))
2008
2022
for i in range (len (matches )):
2009
- if i != best_match :
2023
+ if i != len ( matches ) - 1 :
2010
2024
show_command (matches [i ], prefix = ' ' )
2011
2025
print (bold ('\n Best match:' ))
2012
- show_command (matches [best_match ], full = True )
2026
+ show_command (matches [- 1 ], full = True )
2013
2027
2014
2028
# ==============================================================================
2015
- # We've settled on matching_command[best_match] .
2029
+ # We've settled on matches[-1] as the best match .
2016
2030
# Continue interpreting additional parameters (even if no 'go')
2017
2031
# ==============================================================================
2018
- c = matches [best_match ]
2032
+ c = matches [- 1 ]
2019
2033
oci_command_line = 'oci ' + c ['action' ] + ' '
2020
2034
2021
2035
if c ['action' ] in ('audit event list' , 'logging-search search-logs' ) and \
0 commit comments