Skip to content

Commit d7c910d

Browse files
fix: Create scriptor config when not existing (#199)
Fix for #198 --------- Co-authored-by: Jan Max Meyer <jmm@phorward.de>
1 parent 477ff3b commit d7c910d

3 files changed

Lines changed: 53 additions & 35 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This file documents any relevant changes.
44

5+
## [2.3.1] 2025-10-22
6+
7+
- fix: Create scriptor config when not existing (#199)
8+
59
## [2.3.0] 2025-10-20
610

711
- cicd: Updated deployment to trusted publisher workflow

src/viur_cli/conf.py

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,82 +2,96 @@
22
import click
33
import requests
44
import difflib
5+
import os
56
from .utils import *
67
from .version import __version__ as cli_version
78

89

910
class Config(dict):
10-
CONFIG_FILE = None
11-
PROJECT_CONFIG_VERSION = None
12-
LAST_VERSION = None
11+
"""
12+
Abstraction layer for managing config files.
13+
"""
1314

14-
def __init__(self, *args, **kwargs):
15-
self.load()
15+
FILENAME = None
16+
"""
17+
The filename used for this configuration file.
18+
"""
1619

17-
def load(self):
18-
"""
19-
Load project.json and write to the global projectConfig.
20+
VERSION = None
21+
"""
22+
The current version for this configuration file, which is tied to the viur-cli package.
23+
"""
2024

21-
This function is responsible for loading the project.json configuration file and populating the global
22-
projectConfig variable.
23-
It handles error checks, such as missing or invalid JSON configuration files, and updates the project
24-
configuration.
25+
def __init__(self, *, path=None):
26+
self.path = path
2527

26-
:param path: str, optional
27-
The path to the project.json file. If not provided, the default projectConfigFilePath is used.
28+
if self.VERSION:
29+
self["format"] = self.VERSION
2830

29-
:return: dict
30-
The project configuration loaded from the project.json file.
31+
self.load()
32+
33+
def load(self):
34+
"""
35+
Load configuration from a file into this config object.
3136
"""
3237
# Search in any parent folder for a project.json,
3338
# change working directory because subsequent commands
3439
# require for project root folder.
3540

3641
changed = False
37-
while not os.path.exists(self.CONFIG_FILE):
42+
while not os.path.exists(self.FILENAME):
3843
os.chdir("..")
3944
changed = True
4045

4146
if os.getcwd() == "/":
42-
echo_fatal(f"{self.CONFIG_FILE} not found - please check if you are in the right folder.")
47+
48+
if self.path:
49+
self.save()
50+
self.load()
51+
return
52+
else:
53+
echo_fatal(f"{self.FILENAME} not found - please check if you are in the right folder.")
4354

4455
if changed:
4556
echo_info(f"Project root is {os.getcwd()}")
4657

4758
try:
48-
f = open(self.CONFIG_FILE, "r")
59+
f = open(self.FILENAME, "r")
60+
self.path = os.getcwd()
4961
self.update(json.loads(f.read()))
5062

5163

5264
except FileNotFoundError:
53-
echo_fatal(f"Can't open {self.CONFIG_FILE} for reading")
65+
echo_fatal(f"Can't open {self.FILENAME} for reading")
5466

5567
except json.decoder.JSONDecodeError as e:
5668
echo_fatal(
57-
f"The configuration in {self.CONFIG_FILE} contains invalid JSON: {str(e)}. Please verify right syntax.")
69+
f"The configuration in {self.FILENAME} contains invalid JSON: {str(e)}. Please verify right syntax.")
70+
5871
self.migrate()
5972

6073
def migrate(self):
74+
"""
75+
A hook for migrating a read config.
76+
"""
6177
pass
6278

6379
def save(self):
6480
"""
65-
Write the current projectConfig dictionary to project.json.
81+
Write the current configuration back to the file.
6682
"""
67-
with open(self.CONFIG_FILE, "w") as f:
83+
os.chdir(self.path)
84+
with open(self.FILENAME, "w") as f:
6885
json.dump(self, f, indent=4, sort_keys=True)
6986
f.write('\n')
7087

7188

7289
class ProjectConfig(Config):
73-
CONFIG_FILE = "project.json"
74-
PROJECT_CONFIG_VERSION = "2.0.0"
75-
LAST_VERSION = ""
90+
FILENAME = "project.json"
91+
VERSION = "2.0.0"
7692

7793
def __init__(self):
78-
7994
self["default"] = {}
80-
self["format"] = self.PROJECT_CONFIG_VERSION
8195
super().__init__()
8296

8397
def get_profile(self, profile):
@@ -129,7 +143,7 @@ def migrate(self):
129143
self["format"] = old_format
130144
del self["default"]["format"]
131145

132-
assert self["format"] in ["1.0.0", "1.0.1", "1.1.0", "1.1.1", "1.2.0", self.PROJECT_CONFIG_VERSION], \
146+
assert self["format"] in ["1.0.0", "1.0.1", "1.1.0", "1.1.1", "1.2.0", self.VERSION], \
133147
"Invalid formatversion, you have to fix it manually"
134148

135149
# Version 1.0.1
@@ -229,18 +243,18 @@ class ScriptorConfig(Config):
229243
Manage scriptor configuration.
230244
TODO miragte with other config
231245
"""
232-
CONFIG_FILE = "viur_scriptor_config.json"
246+
FILENAME = "viur_scriptor_config.json"
233247
DEFAULT_BASE_URL = "http://localhost:8080"
234248
DEFAULT_WORKING_DIR = "scripts/"
235-
_instance = None
236249

237-
def __init__(self, *args, **kwargs):
250+
def __init__(self, **kwargs):
238251
self.update({
239252
"base_url": self.DEFAULT_BASE_URL,
240253
"working_dir": self.DEFAULT_WORKING_DIR,
241254
})
242-
super().__init__(*args, **kwargs)
255+
super().__init__(**kwargs)
243256

244257

258+
# Create specific configs
245259
config = ProjectConfig()
246-
scriptor_config = ScriptorConfig()
260+
scriptor_config = ScriptorConfig(path=config.path)

src/viur_cli/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = "2.3.0"
1+
__version__ = "2.3.1"
22
MINIMAL_PIPENV = "2023.11.15"

0 commit comments

Comments
 (0)