Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default keys #17

Merged
merged 4 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Added git remote repositories and directories as potential sources for config files in the cli.
* `title` and `description` can be set from `pretty` and `description` keys at root.
* Added default key for os systems.


### Changed
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ Options are dynamically created with the schema part of the config file.
2. Add `pretty` and `description` keys.
3. Create lists like `key: Pretty Key`.
* `title` and `description` from within the schema overwrite `pretty` and `description` outside of the schema.
* For the package to set the default os to the running system, name the property `__os__`.

```yaml
schema:
Expand Down
31 changes: 31 additions & 0 deletions installation_instruction/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from sys import exit
from os.path import isfile, isdir
from subprocess import run
import platform

import click

Expand All @@ -29,6 +30,29 @@
License: {__license__}
Repository: {__repository__}"""

def _get_system(option_types):
"""
Returns the os from the list of possible os systems defined in the schema.

:param option_types: list of system from the schema.
:type option_types: list
:return: os system from input list or None.
:rtype: string or None
"""

system = platform.system()
system_names = {
'Linux': 'linux',
'Darwin': 'mac',
'Windows': 'win',
}

new_default = system_names.get(system,None)
for type in option_types:
if new_default in type.lower():
return type

return None

def _red_echo(text: str):
click.echo(click.style(text, fg="red"))
Expand All @@ -47,6 +71,7 @@ def __init__(self, *args, **kwargs):
options_metavar="",
)


def get_command(self, ctx, config_file: str) -> click.Command|None:

temp_dir = None
Expand Down Expand Up @@ -77,6 +102,12 @@ def get_command(self, ctx, config_file: str) -> click.Command|None:
_red_echo("Error (parsing options from schema): " + str(e))
exit(1)

#set new default value for __os__ Option
for option in options:
if '__os__' in option.name:
system_default = _get_system(option.type.choices)
if system_default:
option.default = system_default

def callback(**kwargs):
inst = instruction.validate_and_render(kwargs)
Expand Down
36 changes: 20 additions & 16 deletions tests/data/test_install/install.cfg
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
schema:
$schema: https://json-schema.org/draft/2020-12/schema
$id: https://github.com/instructions-d-installation/installation-instruction/tests/data/test_install/install.cfg
name: test-install
type: object
properties:
error_install:
type: boolean
error_template:
type: boolean
default: false
required:
- error_install
- error_template
description:
error_install: Test for flag without default value.
$schema: https://json-schema.org/draft/2020-12/schema
$id: https://github.com/instructions-d-installation/installation-instruction/tests/data/test_install/install.cfg
name: test-install
type: object
properties:
error_install:
type: boolean
error_template:
type: boolean
default: false
__os__:
description: The operating system in which the package is installed.
enum:
- Windows
- macOS
- Linux
default: macOS
required:
- error_install
- error_template
------
echo "start" &&
{%- if error_install %}
Expand Down