Skip to content

Commit

Permalink
[DRAFT] first PR for validate command and includes the Python CLI str…
Browse files Browse the repository at this point in the history
…ucture. Eventually fixes issue i-am-bee#132
  • Loading branch information
maximilien committed Jan 29, 2025
1 parent a452398 commit b5d0f8d
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 26 deletions.
3 changes: 3 additions & 0 deletions beeAI
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

python3 ./tools/cli/cli.py $@
62 changes: 62 additions & 0 deletions common/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Copyright © 2025 IBM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
from random import randint

VERBOSE=False

class Colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

class Console:
def verbose(msg):
if VERBOSE:
print(f"{Colors.OKBLUE}{msg}{Colors.ENDC}".format(msg=str(msg)))

def print(msg=''):
print(msg)

def println(no=1):
for i in range(no):
print()

def ok(msg):
print(f"{Colors.OKGREEN}{msg}{Colors.ENDC}".format(msg=str(msg)))

def error(msg):
Console.fail(msg)

def fail(msg):
print(f"{Colors.FAIL}Error: {msg}{Colors.ENDC}".format(msg=str(msg)))

def warn(msg):
print(f"{Colors.WARNING}Warning: {msg}{Colors.ENDC}".format(msg=str(msg)))

def progress(count, total, status=''):
bar_len = 60
filled_len = int(round(bar_len * count / float(total)))

percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)

sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status))
sys.stdout.flush()
6 changes: 3 additions & 3 deletions test/schema/funnier_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ spec:
- name: colleague
agent: colleague
condition:
- if: { input.find('funnier') != -1 }
then: expert
else: end
- if: (input.find('funnier') != -1)
then: expert
else: end
23 changes: 0 additions & 23 deletions test/schema/validator.py

This file was deleted.

43 changes: 43 additions & 0 deletions tools/cli/beeAI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python3

# Copyright © 2025 IBM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""BeeAI
Usage:
beeAI validate SCHEMA_FILE YAML_FILE [options]
beeAI (-h | --help)
beeAI (-v | --version)
Options:
--verbose Show all output.
-h --help Show this screen.
-v --version Show version.
"""
import os, sys, traceback

from docopt import docopt
from cli import *

if __name__ == '__main__':
args = docopt(__doc__, version='beeAI CLI v0.0.0')
command = CLI(args).command()
rc = command.execute()
if rc != 0:
Console.error("executing command: {rc}".format(rc=rc))
sys.exit(rc)
63 changes: 63 additions & 0 deletions tools/cli/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Copyright © 2025 IBM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import io, sys

from common import *

class CLI:
def __init__(self, args):
self.args = args
if self.args['--verbose']:
VERBOSE = True

def command(self):
if self.args.get('validate') and self.args['validate']:
return Validate(self.args)
else:
raise Exception("Invalid command")

class Command:
def __init__(self, args, credentials, client):
self.__init_empty_options(args)
self.args = args

def println(self, msg):
self.print(msg + "\n")

def print(self, msg):
Console.print(msg)

def warn(self, msg):
Console.warn(msg)

def verbose(self):
return self.args['--verbose']

def execute(self):
func = self.dispatch()
rc = func()
if rc == None:
return 0
else:
if isinstance(rc, int):
return rc
else:
return 1

def dispatch(self):
if self.args['validate']:
return self.validate
else:
raise Exception("Invalid subcommand")
42 changes: 42 additions & 0 deletions tools/cli/validate_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright © 2025 IBM
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import io, sys, yaml, json, jsonschema

# validate command group
class Validate(Command):
def __init__(self, args):
self.args = args
super().__init__(self.args)

def SCHEMA_FILE(self):
return self.args['SCHEMA_FILE']

def YAML_FILE(self):
return self.args['SCHEMA_FILE']

def name(self):
return "validate"

def validate(self):
Console.print("validate {yaml_file} with schema {schema_file}".format(yaml_file=self.YAML_FILE, schema_file=self.SCHEMA_FILE))
with open(self.SCHEMA_FILE, 'r') as f:
schema = json.load(f)
with open(self.YAML_FILE, 'r') as f:
yamls = yaml.safe_load_all(f)
for yaml_data in yamls:
json_data = json.dumps(yaml_data, indent=4)
jsonschema.validate(yaml_data, schema)
Console.print("YAML file is valid.")
return 0

0 comments on commit b5d0f8d

Please sign in to comment.