Skip to content

Commit

Permalink
vmsdk (python): rework example for get_quote
Browse files Browse the repository at this point in the history
Signed-off-by: zhongjie <[email protected]>
  • Loading branch information
intelzhongjie committed Dec 21, 2023
1 parent 3da9deb commit 5b3beba
Showing 1 changed file with 39 additions and 26 deletions.
65 changes: 39 additions & 26 deletions vmsdk/python/cc_quote_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,42 @@
OUT_FORMAT_RAW = "raw"
OUT_FORMAT_HUMAN = "human"

parser = argparse.ArgumentParser()
parser.add_argument(
'--out-format',
action='store',
help='Output format: raw/human. Default raw.',
dest='out_format'
)
args = parser.parse_args()

dump_raw = False
f = args.out_format
if f is None:
# When the format is not set. Dump raw as default.
dump_raw = True
elif f == OUT_FORMAT_HUMAN:
dump_raw = False
elif f == OUT_FORMAT_RAW:
dump_raw = True
else:
parser.print_help()
parser.exit(2, "Specified output format is not supported!")

logging.basicConfig(level=logging.NOTSET, format='%(name)s %(levelname)-8s %(message)s')
quote = CCTrustedVmSdk.inst().get_quote(None, None, None)
if quote is not None:
quote.dump(dump_raw)
def out_format_validator(out_format):
"""Validator (callback for ArgumentParser) of output format
Args:
out_format: User specified output format.
Returns:
Validated value of the argument.
Raises:
ValueError: An invalid value is given by user.
"""
if out_format != OUT_FORMAT_HUMAN and out_format != OUT_FORMAT_RAW:
raise ValueError
return out_format

def main():
"""Example to call get_quote and dump the result to stdout"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--out-format",
action="store",
default=OUT_FORMAT_RAW,
dest="out_format",
help="Output format: raw/human. Default raw.",
type=out_format_validator
)
args = parser.parse_args()

logging.basicConfig(
level=logging.NOTSET,
format="%(name)s %(levelname)-8s %(message)s"
)
quote = CCTrustedVmSdk.inst().get_quote(None, None, None)
if quote is not None:
quote.dump(args.out_format == OUT_FORMAT_RAW)

if __name__ == "__main__":
main()

0 comments on commit 5b3beba

Please sign in to comment.