Skip to content

Commit

Permalink
Nicer use of argparse types
Browse files Browse the repository at this point in the history
  • Loading branch information
Jc2k committed May 26, 2015
1 parent e24d90d commit a6ff905
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
6 changes: 3 additions & 3 deletions touchdown/aws/logs/tail.py
Expand Up @@ -18,7 +18,7 @@
import time

from touchdown.core import plan
from touchdown.core.datetime import parse_datetime_as_seconds
from touchdown.core.datetime import as_seconds
from touchdown.aws import common
from touchdown.aws.logs import LogGroup

Expand All @@ -34,9 +34,9 @@ def tail(self, start, end, follow):
'logGroupName': self.resource.name,
}
if start:
kwargs['startTime'] = parse_datetime_as_seconds(start)
kwargs['startTime'] = as_seconds(start)
if end:
kwargs['endTime'] = parse_datetime_as_seconds(end)
kwargs['endTime'] = as_seconds(end)

def pull(kwargs, previous_events):
seen = set()
Expand Down
5 changes: 2 additions & 3 deletions touchdown/core/datetime.py
Expand Up @@ -89,6 +89,5 @@ def parse_datetime(value):
)


def parse_datetime_as_seconds(value):
date = parse_datetime(value)
return int(date.strftime("%s")) * 1000
def as_seconds(value):
return int(value.strftime("%s")) * 1000
44 changes: 40 additions & 4 deletions touchdown/goals/tail.py
Expand Up @@ -13,9 +13,20 @@
# limitations under the License.

from touchdown.core import errors
from touchdown.core.datetime import parse_datetime
from touchdown.core.goals import Goal, register


def datetime(value):
try:
return parse_datetime(value)
except errors.Error:
import argparse
raise argparse.ArgumentTypeError(
"{} is not a valid date/time".format(value),
)


class Tail(Goal):

""" Inspect (and stream) your logs """
Expand All @@ -30,10 +41,35 @@ def get_plan_class(self, resource):

@classmethod
def setup_argparse(cls, parser):
parser.add_argument("stream", metavar="STREAM", type=str, help="The logstream to tail")
parser.add_argument("-f", "--follow", default=False, action="store_true", help="Don't exit and continue to print new events in the stream")
parser.add_argument("-s", "--start", default="5m ago", action="store", help="The earliest event to retrieve")
parser.add_argument("-e", "--end", default=None, action="store", help="The latest event to retrieve")
parser.add_argument(
"stream",
metavar="STREAM",
type=str,
help="The logstream to tail"
)
parser.add_argument(
"-f",
"--follow",
default=False,
action="store_true",
help="Don't exit and continue to print new events in the stream"
)
parser.add_argument(
"-s",
"--start",
default="5m ago",
action="store",
type=datetime,
help="The earliest event to retrieve"
)
parser.add_argument(
"-e",
"--end",
default=None,
action="store",
type=datetime,
help="The latest event to retrieve"
)

def execute(self, stream, start="5m ago", end=None, follow=False):
tailers = {}
Expand Down

0 comments on commit a6ff905

Please sign in to comment.