Skip to content

Commit

Permalink
Implement clock CLI
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Fastiuk <yfastiuk@nvidia.com>
  • Loading branch information
fastiuk committed Apr 14, 2023
1 parent d17d124 commit 586c9ea
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
63 changes: 63 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/sbin/env python

import click
import datetime
import ipaddress
import json
import jsonpatch
Expand Down Expand Up @@ -7031,5 +7032,67 @@ def del_subinterface(ctx, subinterface_name):
except JsonPatchConflict as e:
ctx.fail("{} is invalid vlan subinterface. Error: {}".format(subinterface_name, e))


#
# 'clock' group ('config clock ...')
#
@config.group()
def clock():
"""Configuring system clock"""
pass


def get_tzs(ctx, args, incomplete):
ret = clicommon.run_command('timedatectl list-timezones',
display_cmd=False, ignore_error=False,
return_cmd=True)
if len(ret) == 0:
return []

lst = ret[0].split('\n')
lst.append('Etc/UTC')
return [k for k in lst if incomplete in k]


@clock.command()
@click.argument('timezone', metavar='<timezone_name>', required=True,
autocompletion=get_tzs)
def timezone(timezone):
"""Set system timezone"""

if timezone not in get_tzs(None, None, ''):
click.echo(f'Timezone {timezone} does not conform format')
sys.exit(1)

config_db = ConfigDBConnector()
config_db.connect()
config_db.mod_entry(swsscommon.CFG_DEVICE_METADATA_TABLE_NAME, 'localhost',
{'timezone': timezone})


@clock.command()
@click.argument('date', metavar='<YYYY-MM-DD>', required=True)
@click.argument('time', metavar='<HH:MM:SS>', required=True)
def date(date, time):
"""Set system date and time"""
valid = True
try:
datetime.datetime.strptime(date, '%Y-%m-%d')
except ValueError:
click.echo(f'Date {date} does not conform format YYYY-MM-DD')
valid = False

try:
datetime.datetime.strptime(time, '%H:%M:%S')
except ValueError:
click.echo(f'Time {time} does not conform format HH:MM:SS')
valid = False

if not valid:
sys.exit(1)

clicommon.run_command(f'timedatectl set-time "{date} {time}"')


if __name__ == '__main__':
config()
28 changes: 23 additions & 5 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1765,13 +1765,31 @@ def uptime(verbose):
cmd = "uptime -p"
run_command(cmd, display_cmd=verbose)

@cli.command()
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def clock(verbose):

#
# 'clock' command group ("show clock ...")
#
@cli.group('clock', invoke_without_command=True)
@click.pass_context
def clock(ctx):
"""Show date and time"""
cmd ="date"
run_command(cmd, display_cmd=verbose)
# If invoking subcomand, no need to do anything
if ctx.invoked_subcommand is not None:
return

run_command('date')


@clock.command()
def timezones():
"""List of available timezones"""
run_command('timedatectl list-timezones')
click.echo('Etc/UTC')


#
# 'system-memory' command ("show system-memory")
#
@cli.command('system-memory')
@click.option('--verbose', is_flag=True, help="Enable verbose output")
def system_memory(verbose):
Expand Down

0 comments on commit 586c9ea

Please sign in to comment.