Skip to content

Commit

Permalink
Remove click command
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoelho93 committed Jun 5, 2024
1 parent 4e5f633 commit f84ac5b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@ A tool to help you write JSON and YAML files from JSON schemas

## Installation


```shell
pip install steer
```

## Usage

Let's say you want to build an OpenAPI specification for an API.

1. Download the OpenAPI json schema from [here](https://github.com/OAI/OpenAPI-Specification/blob/main/schemas/v2.0/schema.json)
1. Run `steer`:
```shell
steer values examples/openapi.schema.json --output-type json --output-file restapi.json

```

## Roadmap

- [ ] Implement the `array` of `objects` property type
- [ ] Implement the `$ref` property type
- [ ] Validate properties values with `pattern` and `format`
- [ ] Implement prompt for `additionalProperties`
- [ ] Validate `required` fields
4 changes: 2 additions & 2 deletions steer/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from steer.cli import cli
from steer.cli import steer


if __name__ == '__main__':
cli()
steer()
9 changes: 2 additions & 7 deletions steer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@
from steer.models import OutputType


@click.group()
def cli():
pass


output_types = [t.value for t in OutputType]


@cli.command(help='Interactively build a values.yml file')
@click.command()
@click.argument('json_schema', type=click.File('r'))
@click.option('--output-type', '-t', help="The type of output", type=click.Choice(output_types), default='stdout')
@click.option('--output-file', '-o', help="The file to output data")
def values(json_schema: TextIOWrapper, output_type: str, output_file: str = None):
def steer(json_schema: TextIOWrapper, output_type: str, output_file: str = None):
content = json.loads(json_schema.read())
schema = Schema.from_dict(content)
schema.prompt(OutputType(output_type), output_file)
15 changes: 8 additions & 7 deletions steer/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Property(BaseModel):
description: Optional[str] = None
default: Optional[Any] = None
allOf: Optional[List[BaseModel]] = None
path: Optional[Any] = None
path: Optional[str] = None
value: Optional[Any] = None

@classmethod
Expand All @@ -30,7 +30,8 @@ def from_dict(cls, key, obj):

def save(self, data: Any):
if self.value is not None:
self.path.update_or_create(data, self.value)
json_path = parse(self.path)
json_path.update_or_create(data, self.value)

def _get_prompt_args(self):
args = {
Expand Down Expand Up @@ -66,7 +67,7 @@ def from_dict(cls, key, obj, parent):
default=obj.get('default'),
enum=obj.get('enum'),
pattern=obj.get('pattern'),
path=parse(parent + key)
path=parent + key
)

def prompt(self):
Expand All @@ -87,7 +88,7 @@ def from_dict(cls, key, obj, parent):
format=obj.get('format'),
default=obj.get('default'),
enum=obj.get('enum'),
path=parse(parent + key)
path=parent + key
)

def _get_prompt_args(self):
Expand All @@ -114,7 +115,7 @@ def from_dict(cls, key, obj, parent):
name=key,
type=obj.get('type'),
default=obj.get('default'),
path=parse(parent + key)
path=parent + key
)

def prompt(self):
Expand All @@ -138,7 +139,7 @@ def from_dict(cls, key, obj, parent):
name=key,
type=obj.get('type'),
items=obj.get('items'),
path=parse(parent + key)
path=parent + key
)

def prompt(self):
Expand Down Expand Up @@ -166,7 +167,7 @@ def from_dict(cls, key, obj, parent):
name=key,
type=obj.get('type', 'object'),
additionalProperties=bool(obj.get('additionalProperties', False)),
path=parse(parent + key)
path=parent + key
)
if obj.get('properties') is not None:
return property.with_properties(obj['properties'], parent + key)
Expand Down

0 comments on commit f84ac5b

Please sign in to comment.