Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek committed Aug 18, 2018
1 parent 5ec23d9 commit 1d4516e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 49 deletions.
4 changes: 3 additions & 1 deletion datadog_checks_dev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Options:
Commands:
clean Remove a project's build artifacts
config Manage the config file
create Create scaffolding for a new integration
dep Manage dependencies
manifest Manage manifest files
release Manage the release of checks
Expand Down Expand Up @@ -228,7 +229,8 @@ Usage: ddev create [OPTIONS] NAME

Options:
-t, --type [check] The type of integration to create
-l, --location TEXT Where to create the integration
-l, --location TEXT The directory where files will be written
-i, --interactive Prompt for some fields
-q, --quiet Show less output
-n, --dry-run Only show what would be created
-h, --help Show this message and exit.
Expand Down
60 changes: 12 additions & 48 deletions datadog_checks_dev/datadog_checks/dev/tooling/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os
import uuid
from collections import defaultdict
from datetime import datetime

import click

from .utils import CONTEXT_SETTINGS, abort, echo_info, echo_success
from ..constants import get_root
from ..create import create_template_files, get_valid_templates
from ..create import construct_template_fields, create_template_files, get_valid_templates
from ..utils import normalize_package_name
from ...utils import resolve_path

Expand Down Expand Up @@ -95,12 +93,14 @@ def display_path_tree(path_tree):
default='check',
help='The type of integration to create'
)
@click.option('--location', '-l', help='Where to create the integration')
@click.option('--location', '-l', help='The directory where files will be written')
@click.option('--interactive', '-i', is_flag=True, help='Prompt for some fields')
@click.option('--quiet', '-q', is_flag=True, help='Show less output')
@click.option('--dry-run', '-n', is_flag=True, help='Only show what would be created')
@click.pass_context
def create(ctx, name, integration_type, location, quiet, dry_run):
def create(ctx, name, integration_type, location, interactive, quiet, dry_run):
"""Create scaffolding for a new integration."""
repo_choice = ctx.obj['repo_choice']
integration_name = normalize_package_name(name)
root = resolve_path(location) if location else get_root()
path_sep = os.path.sep
Expand All @@ -109,49 +109,13 @@ def create(ctx, name, integration_type, location, quiet, dry_run):
if os.path.exists(integration_dir):
abort('Path `{}` already exists!'.format(integration_dir))

check_name_cap = integration_name if integration_name.count('_') else integration_name.capitalize()
repo_choice = ctx.obj['repo_choice']
if repo_choice == 'core':
author = 'Datadog'
email = 'help@datadoghq.com'
email_packages = 'packages@datadoghq.com'
install_info = (
'The {check_name_cap} check is included in the [Datadog Agent][2] package, so you do not\n'
'need to install anything else on your server.'.format(check_name_cap=check_name_cap)
)
license_header = (
'# (C) Datadog, Inc. {year}\n'
'# All rights reserved\n'
'# Licensed under a 3-clause BSD style license (see LICENSE)\n'
.format(year=str(datetime.now().year))
)
support_type = 'core'
tox_base_dep = '../datadog_checks_base[deps]'
else:
author = 'U.N. Owen'
email = email_packages = 'friend@datadog.community'
install_info = (
'The {} check is not included in the [Datadog Agent][2] package, so you will\n'
'need to install it yourself.'.format(check_name_cap)
)
license_header = ''
support_type = 'contrib'
tox_base_dep = 'datadog-checks-base[deps]'

config = {
'author': author,
'check_class': '{}Check'.format(''.join(part.capitalize() for part in integration_name.split('_'))),
'check_name': integration_name,
'check_name_cap': check_name_cap,
'email': email,
'email_packages': email_packages,
'guid': uuid.uuid4(),
'license_header': license_header,
'install_info': install_info,
'repo_choice': repo_choice,
'support_type': support_type,
'tox_base_dep': tox_base_dep,
}
template_fields = {}
if repo_choice != 'core' and interactive:
template_fields['author'] = click.prompt('Your name')
template_fields['email'] = click.prompt('Your email')
template_fields['email_packages'] = template_fields['email']

config = construct_template_fields(integration_name, repo_choice, **template_fields)

files = create_template_files(integration_type, root, config, read=not dry_run)
file_paths = [file.file_path.replace('{}{}'.format(root, path_sep), '', 1) for file in files]
Expand Down
51 changes: 51 additions & 0 deletions datadog_checks_dev/datadog_checks/dev/tooling/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
# All rights reserved
# Licensed under a 3-clause BSD style license (see LICENSE)
import os
from datetime import datetime
from uuid import uuid4

from ..utils import (
create_file,
Expand All @@ -22,6 +24,55 @@ def get_valid_templates():
return sorted(os.listdir(TEMPLATES_DIR))


def construct_template_fields(integration_name, repo_choice, **kwargs):
check_name_cap = integration_name if integration_name.count('_') else integration_name.capitalize()

if repo_choice == 'core':
author = 'Datadog'
email = 'help@datadoghq.com'
email_packages = 'packages@datadoghq.com'
install_info = (
'The {check_name_cap} check is included in the [Datadog Agent][2] package, so you do not\n'
'need to install anything else on your server.'.format(check_name_cap=check_name_cap)
)
license_header = (
'# (C) Datadog, Inc. {year}\n'
'# All rights reserved\n'
'# Licensed under a 3-clause BSD style license (see LICENSE)\n'
.format(year=str(datetime.now().year))
)
support_type = 'core'
tox_base_dep = '../datadog_checks_base[deps]'
else:
author = 'U.N. Owen'
email = email_packages = 'friend@datadog.community'
install_info = (
'The {} check is not included in the [Datadog Agent][2] package, so you will\n'
'need to install it yourself.'.format(check_name_cap)
)
license_header = ''
support_type = 'contrib'
tox_base_dep = 'datadog-checks-base[deps]'

config = {
'author': author,
'check_class': '{}Check'.format(''.join(part.capitalize() for part in integration_name.split('_'))),
'check_name': integration_name,
'check_name_cap': check_name_cap,
'email': email,
'email_packages': email_packages,
'guid': uuid4(),
'license_header': license_header,
'install_info': install_info,
'repo_choice': repo_choice,
'support_type': support_type,
'tox_base_dep': tox_base_dep,
}
config.update(kwargs)

return config


def create_template_files(template_name, new_root, config, read=False):
files = []

Expand Down

0 comments on commit 1d4516e

Please sign in to comment.