Skip to content

Commit 4b2ef1a

Browse files
tarekbadrshafrittoli
authored andcommitted
add command files
1 parent b836f88 commit 4b2ef1a

File tree

9 files changed

+855
-399
lines changed

9 files changed

+855
-399
lines changed

cli/cdevents/cli/__main__.py

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77
import yaml
88

99
from cdevents.cli.constants import LOGGING_CONFIGURATION_FILE
10-
from cdevents.cli.build import finished, queued, started
1110
from cdevents.cli.utils import add_disclaimer_text
1211

12+
from cdevents.cli.build import finished, queued, started
13+
from cdevents.cli.artifact import packaged, published
14+
from cdevents.cli.branch import created as branch_created, deleted as branch_deleted
15+
from cdevents.cli.env import created as env_created, deleted as env_deleted, modified as env_modified
16+
from cdevents.cli.pipelinerun import started as pipe_started, finished as pipe_finished, queued as pipe_queued
17+
from cdevents.cli.service import deployed as service_deployed, upgraded as service_upgraded, removed as service_removed, rolledback as service_rolledback
18+
from cdevents.cli.taskrun import started as taskrun_started, finished as taskrun_finished
19+
20+
1321
def configure_logging():
1422
"""Configures logging from file."""
1523
config_file = Path(LOGGING_CONFIGURATION_FILE)
@@ -26,25 +34,61 @@ def build():
2634
build.add_command(started)
2735

2836

29-
# @click.group(help=add_disclaimer_text("""Commands with no interaction with hostlog."""))
30-
# def offline():
31-
# """Function for command group offline."""
37+
@click.group(help=add_disclaimer_text("""Commands Artifact related CloudEvent."""))
38+
def artifact():
39+
"""Click group for command 'artifact'."""
40+
41+
artifact.add_command(packaged)
42+
artifact.add_command(published)
43+
44+
45+
@click.group(help=add_disclaimer_text("""Commands Branch related CloudEvent."""))
46+
def branch():
47+
"""Click group for command 'branch'."""
48+
49+
branch.add_command(branch_created)
50+
branch.add_command(branch_deleted)
51+
52+
53+
@click.group(help=add_disclaimer_text("""Commands Environment related CloudEvent."""))
54+
def env():
55+
"""Click group for command 'environment'."""
56+
57+
env.add_command(env_created)
58+
env.add_command(env_deleted)
59+
env.add_command(env_modified)
60+
61+
62+
63+
64+
@click.group(help=add_disclaimer_text("""Commands PipelineRun related CloudEvent."""))
65+
def pipelinerun():
66+
"""Click group for command 'environment'."""
67+
68+
pipelinerun.add_command(pipe_started)
69+
pipelinerun.add_command(pipe_finished)
70+
pipelinerun.add_command(pipe_queued)
71+
72+
73+
74+
@click.group(help=add_disclaimer_text("""Commands Service related CloudEvent."""))
75+
def service():
76+
"""Click group for command 'service'."""
3277

78+
service.add_command(service_deployed)
79+
service.add_command(service_upgraded)
80+
service.add_command(service_removed)
81+
service.add_command(service_rolledback)
3382

34-
# offline.add_command(pcap_to_hdf5)
35-
# TODO: activate line when command is implemented
36-
# offline.add_command(visualize)
37-
# offline.add_command(create_dissector)
38-
# offline.add_command(convert_to_xviz)
39-
# offline.add_command(topic_configuration)
4083

84+
@click.group(help=add_disclaimer_text("""Commands TaskRun related CloudEvent."""))
85+
def taskrun():
86+
"""Click group for command 'taskrun'."""
4187

42-
# @click.group(help=add_disclaimer_text("Commands for local web UI."))
43-
# def web():
44-
# """Click group for command 'web'."""
88+
taskrun.add_command(taskrun_started)
89+
taskrun.add_command(taskrun_finished)
4590

4691

47-
# web.add_command(web_start)
4892

4993

5094
@click.group(
@@ -60,8 +104,12 @@ def cli():
60104

61105

62106
cli.add_command(build)
63-
# cli.add_command(offline)
64-
# cli.add_command(web)
107+
cli.add_command(artifact)
108+
cli.add_command(branch)
109+
cli.add_command(env)
110+
cli.add_command(pipelinerun)
111+
cli.add_command(service)
112+
cli.add_command(taskrun)
65113

66114
if __name__ == "__main__":
67115
cli()

cli/cdevents/cli/artifact.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
"""Module for cli artifact commands."""
2+
from __future__ import annotations
3+
import os
4+
5+
from typing import List
6+
7+
import click
8+
import requests
9+
10+
from cloudevents.http import CloudEvent, to_structured
11+
12+
from cdevents.cli.utils import (
13+
add_disclaimer_text,
14+
print_function_args,
15+
)
16+
17+
18+
# pylint: disable=unused-argument
19+
def common_artifact_options(function):
20+
"""Decorator for common cli options for artifact."""
21+
function = click.option(
22+
"--cde_sink",
23+
"-c",
24+
required=False,
25+
type=str,
26+
default=lambda: os.environ.get("CDE_SINK", "http://localhost:8080"),
27+
help="CDE_SINK",
28+
)(function)
29+
function = click.option(
30+
"--id",
31+
"-i",
32+
required=False,
33+
type=str,
34+
help="Artifact Id.",
35+
)(function)
36+
function = click.option(
37+
"--name",
38+
"-n",
39+
required=False,
40+
type=str,
41+
help="Artifact Name.",
42+
)(function)
43+
function = click.option(
44+
"--version",
45+
"-v",
46+
required=False,
47+
type=str,
48+
help="Artifact Version.",
49+
)(function)
50+
function = click.option(
51+
"--data",
52+
"-d",
53+
required=False,
54+
type=(str,str),
55+
multiple=True,
56+
help="Artifact Data.",
57+
)(function)
58+
59+
return function
60+
61+
62+
@click.command(help=add_disclaimer_text("Artifact Packaged CloudEvent."))
63+
@common_artifact_options
64+
def packaged(
65+
cde_sink: str,
66+
id: str,
67+
name: str = None,
68+
version: str = None,
69+
data :List[str] = None,
70+
):
71+
print_function_args()
72+
attributes = {
73+
"type": "cd.artifact.packaged.v1",
74+
"source": "cde-cli",
75+
"extensions": {
76+
"artifactid": id,
77+
"artifactname": name,
78+
"artifactversion": version,
79+
},
80+
}
81+
event = CloudEvent(attributes, dict(data))
82+
headers, body = to_structured(event)
83+
84+
# send and print event
85+
requests.post(cde_sink, headers=headers, data=body)
86+
87+
@click.command(help=add_disclaimer_text("Artifact Published CloudEvent."))
88+
@common_artifact_options
89+
def published(
90+
cde_sink: str,
91+
id: str,
92+
name: str = None,
93+
version: str = None,
94+
data :List[str] = None,
95+
):
96+
print_function_args()
97+
attributes = {
98+
"type": "cd.artifact.published.v1",
99+
"source": "cde-cli",
100+
"extensions": {
101+
"artifactid": id,
102+
"artifactname": name,
103+
"artifactversion": version,
104+
},
105+
}
106+
event = CloudEvent(attributes, dict(data))
107+
headers, body = to_structured(event)
108+
109+
# send and print event
110+
requests.post(cde_sink, headers=headers, data=body)

cli/cdevents/cli/branch.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""Module for cli branch commands."""
2+
from __future__ import annotations
3+
import os
4+
5+
from typing import List
6+
7+
import click
8+
import requests
9+
10+
from cloudevents.http import CloudEvent, to_structured
11+
12+
from cdevents.cli.utils import (
13+
add_disclaimer_text,
14+
print_function_args,
15+
)
16+
17+
18+
# pylint: disable=unused-argument
19+
def common_branch_options(function):
20+
"""Decorator for common cli options for branch."""
21+
function = click.option(
22+
"--cde_sink",
23+
"-c",
24+
required=False,
25+
type=str,
26+
default=lambda: os.environ.get("CDE_SINK", "http://localhost:8080"),
27+
help="CDE_SINK",
28+
)(function)
29+
function = click.option(
30+
"--id",
31+
"-i",
32+
required=False,
33+
type=str,
34+
help="Branch Id.",
35+
)(function)
36+
function = click.option(
37+
"--name",
38+
"-n",
39+
required=False,
40+
type=str,
41+
help="Branch Name.",
42+
)(function)
43+
function = click.option(
44+
"--repoid",
45+
"-v",
46+
required=False,
47+
type=str,
48+
help="Branch Repository Id.",
49+
)(function)
50+
function = click.option(
51+
"--data",
52+
"-d",
53+
required=False,
54+
type=(str,str),
55+
multiple=True,
56+
help="Branch Data.",
57+
)(function)
58+
59+
return function
60+
61+
62+
@click.command(help=add_disclaimer_text("Branch Created CloudEvent."))
63+
@common_branch_options
64+
def created(
65+
cde_sink: str,
66+
id: str,
67+
name: str = None,
68+
repoid: str = None,
69+
data :List[str] = None,
70+
):
71+
print_function_args()
72+
attributes = {
73+
"type": "cd.repository.branch.created.v1",
74+
"source": "cde-cli",
75+
"extensions": {
76+
"branchid": id,
77+
"branchname": name,
78+
"branchrepositoryid": repoid,
79+
},
80+
}
81+
event = CloudEvent(attributes, dict(data))
82+
headers, body = to_structured(event)
83+
84+
# send and print event
85+
requests.post(cde_sink, headers=headers, data=body)
86+
87+
88+
@click.command(help=add_disclaimer_text("Branch Deleted CloudEvent."))
89+
@common_branch_options
90+
def deleted(
91+
cde_sink: str,
92+
id: str,
93+
name: str = None,
94+
repoid: str = None,
95+
data :List[str] = None,
96+
):
97+
print_function_args()
98+
attributes = {
99+
"type": "cd.repository.branch.deleted.v1",
100+
"source": "cde-cli",
101+
"extensions": {
102+
"branchid": id,
103+
"branchname": name,
104+
"branchrepositoryid": repoid,
105+
},
106+
}
107+
event = CloudEvent(attributes, dict(data))
108+
headers, body = to_structured(event)
109+
110+
# send and print event
111+
requests.post(cde_sink, headers=headers, data=body)
112+

0 commit comments

Comments
 (0)