Skip to content

Commit cde4459

Browse files
committed
Improve a bit of documentation, add comments, and update code structure
1 parent ddd63e6 commit cde4459

File tree

2 files changed

+37
-29
lines changed

2 files changed

+37
-29
lines changed

contentctl/actions/initialize.py

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import shutil
33
import os
44
import pathlib
5-
6-
from pydantic import RootModel
75
from contentctl.objects.config import test
86
from contentctl.output.yml_writer import YmlWriter
97

@@ -17,42 +15,47 @@ def execute(self, config: test) -> None:
1715

1816
YmlWriter.writeYmlFile(str(config.path/'contentctl.yml'), config.model_dump())
1917

20-
if config.bare:
21-
#Create the following empty directories:
22-
for emptyDir in ['lookups', 'baselines', 'docs', 'reporting', 'investigations', 'deployments',
23-
'detections/application', 'detections/cloud', 'detections/endpoint',
24-
'detections/network', 'detections/web', 'data_sources', 'macros', 'stories']:
25-
#Throw an error if this directory already exists
26-
(config.path/emptyDir).mkdir(exist_ok=False, parents=True)
27-
28-
# Copy the contents of the app_template directory
29-
source_directory = pathlib.Path(os.path.dirname(__file__))/'../templates/app_template/'
30-
target_directory = config.path/'app_template'
31-
shutil.copytree(source_directory, target_directory, dirs_exist_ok=False)
3218

33-
else:
34-
#Create the following empty directories:
35-
for emptyDir in ['lookups', 'baselines', 'docs', 'reporting', 'investigations']:
36-
#Throw an error if this directory already exists
37-
(config.path/emptyDir).mkdir(exist_ok=False)
38-
19+
#Create the following empty directories:
20+
for emptyDir in ['lookups', 'baselines', 'data_sources', 'docs', 'reporting', 'investigations',
21+
'detections/application', 'detections/cloud', 'detections/endpoint',
22+
'detections/network', 'detections/web', 'macros', 'stories']:
23+
#Throw an error if this directory already exists
24+
(config.path/emptyDir).mkdir(exist_ok=False, parents=True)
25+
26+
# If this is not a bare config, then populate
27+
# a small amount of content into the directories
28+
if not config.bare:
3929
#copy the contents of all template directories
4030
for templateDir, targetDir in [
41-
('../templates/app_template/', 'app_template'),
42-
('../templates/deployments/', 'deployments'),
4331
('../templates/detections/', 'detections'),
4432
('../templates/data_sources/', 'data_sources'),
45-
('../templates/macros/','macros'),
33+
('../templates/macros/', 'macros'),
4634
('../templates/stories/', 'stories'),
4735
]:
4836
source_directory = pathlib.Path(os.path.dirname(__file__))/templateDir
4937
target_directory = config.path/targetDir
50-
#Throw an exception if the target exists
51-
shutil.copytree(source_directory, target_directory, dirs_exist_ok=False)
38+
39+
# Do not throw an exception if the directory exists. In fact, it was
40+
# created above when the structure of the app was created.
41+
shutil.copytree(source_directory, target_directory, dirs_exist_ok=True)
5242

53-
# Create a README.md file. Note that this is the README.md for the repository, not the
54-
# one which will actually be packaged into the app. That is located in the app_template folder.
55-
shutil.copyfile(pathlib.Path(os.path.dirname(__file__))/'../templates/README.md','README.md')
43+
# The contents of app_template must ALWAYS be copied because it contains
44+
# several special files.
45+
# For now, we also copy the deployments because the ability to create custom
46+
# deployment files is limited with built-in functionality.
47+
for templateDir, targetDir in [
48+
('../templates/app_template/', 'app_template'),
49+
('../templates/deployments/', 'deployments')
50+
]:
51+
source_directory = pathlib.Path(os.path.dirname(__file__))/templateDir
52+
target_directory = config.path/targetDir
53+
#Throw an exception if the target exists
54+
shutil.copytree(source_directory, target_directory, dirs_exist_ok=False)
55+
56+
# Create a README.md file. Note that this is the README.md for the repository, not the
57+
# one which will actually be packaged into the app. That is located in the app_template folder.
58+
shutil.copyfile(pathlib.Path(os.path.dirname(__file__))/'../templates/README.md','README.md')
5659

5760

5861
print(f"The app '{config.app.title}' has been initialized. "

contentctl/objects/config.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,12 @@ def serialize_path(path: DirectoryPath)->str:
172172

173173
class init(Config_Base):
174174
model_config = ConfigDict(use_enum_values=True,validate_default=True, arbitrary_types_allowed=True)
175-
bare: bool = Field(default=False, description="Initialize with empty directory structure")
175+
bare: bool = Field(default=False, description="contentctl normally provides some some example content "
176+
"(macros, stories, data_sources, and/or analytic stories). This option disables "
177+
"initialization with that additional contnet. Note that even if --bare is used, it "
178+
"init will still create the directory structure of the app, "
179+
"include the app_template directory with default content, and content in "
180+
"the deployment/ directory (since it is not yet easily customizable).")
176181

177182

178183
# TODO (#266): disable the use_enum_values configuration

0 commit comments

Comments
 (0)