Skip to content

Commit 871eb2f

Browse files
committed
cli: Add config subcommand
And update Configuration type to use Path types instead of os.path.
1 parent 3cb7d3a commit 871eb2f

File tree

3 files changed

+95
-15
lines changed

3 files changed

+95
-15
lines changed

cli/cloe_launch/__init__.py

+22-15
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import os
77
import shutil
88
import subprocess
9+
import sys
10+
11+
from pathlib import Path
912

1013
from typing import (
1114
List,
@@ -22,10 +25,9 @@ class ConfigurationError(Exception):
2225
class Configuration:
2326
"""Configuration contains the launcher configuration as read from file."""
2427

25-
config_dir = os.path.expanduser("~/.config/cloe/launcher/")
26-
27-
config_file = os.path.expanduser("~/.config/cloe/launcher/conf.toml")
28-
runtime_dir = os.path.expanduser("~/.cache/cloe/launcher")
28+
config_dir = Path("~/.config/cloe/launcher/").expanduser()
29+
config_file = Path("~/.config/cloe/launcher/conf.toml").expanduser()
30+
runtime_dir = Path("~/.cache/cloe/launcher").expanduser()
2931

3032
conf_version = "1"
3133
_conf = {
@@ -41,15 +43,17 @@ class Configuration:
4143

4244
def __init__(self):
4345
# Make configuration and runtime directories if needed:
44-
if not os.path.exists(self.config_dir):
46+
if not self.config_dir.exists():
4547
logging.info("Create configuration directory: %s", self.config_dir)
46-
os.makedirs(self.config_dir)
47-
if not os.path.exists(self.runtime_dir):
48+
self.config_dir.mkdir(parents=True)
49+
if not self.runtime_dir.exists():
4850
logging.info("Create runtime directory: %s", self.runtime_dir)
49-
os.makedirs(self.runtime_dir)
51+
self.runtime_dir.mkdir(parents=True)
5052

5153
# Load configuration file:
52-
if os.path.exists(self.config_file):
54+
if self.config_file.exists():
55+
if not self.config_file.is_file():
56+
raise ConfigurationError("configuration file not readable")
5357
conf = toml.load(self.config_file)
5458
if "version" not in conf:
5559
raise ConfigurationError(
@@ -58,23 +62,26 @@ def __init__(self):
5862
for k in conf.keys():
5963
self._conf[k] = conf[k]
6064

61-
def profile_runtime(self, hash: str) -> str:
65+
def print(self):
66+
toml.dump(self._conf, sys.stdout)
67+
68+
def profile_runtime(self, hash: str) -> Path:
6269
"""Return the path to the runtime directory of the profile."""
63-
return os.path.join(self.runtime_dir, hash)
70+
return self.runtime_dir / hash
6471

6572
def write(self) -> None:
6673
"""Write current configuration to the disk."""
6774
logging.info(f"Write configuration to {self.config_file}:\n {self._conf}")
6875
with open(self.config_file, "w", encoding="utf-8") as file:
6976
toml.dump(self._conf, file)
7077

71-
def edit(self, create: bool = False) -> None:
78+
def edit(self, create: bool = False) -> int:
7279
"""Open the configuration in the user's editor."""
7380
editor = os.getenv("EDITOR")
7481
if editor is None:
7582
raise ConfigurationError("environment variable EDITOR is unset")
76-
if not create and not os.path.exists(self.config_file):
83+
if not create and not self.config_file.exists():
7784
raise ConfigurationError(f"configuration {self.config_file} does not exist")
78-
cmd = [editor, self.config_file]
85+
cmd = editor.split(' ') + [str(self.config_file)]
7986
logging.info("Exec: %s", " ".join(cmd))
80-
subprocess.call(cmd)
87+
return subprocess.call(cmd)

cli/cloe_launch/commands/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from .exec import cli_exec
4141
from .prepare import cli_prepare
4242
from .shell import cli_shell
43+
from .config import cli_config
4344

4445

4546
@click.group(context_settings={"allow_interspersed_args": False})
@@ -129,6 +130,7 @@ def main():
129130
cli.add_command(cli_deploy)
130131
cli.add_command(cli_prepare)
131132
cli.add_command(cli_shell)
133+
cli.add_command(cli_config)
132134

133135
try:
134136
cli()

cli/cloe_launch/commands/config.py

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Copyright 2024 Robert Bosch GmbH
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
#
15+
# SPDX-License-Identifier: Apache-2.0
16+
17+
# pylint: disable=too-many-arguments
18+
19+
"""
20+
Implementation for cloe-launch command config.
21+
22+
Usage: cloe-launch config
23+
"""
24+
25+
import click
26+
27+
from cloe_launch import Configuration
28+
from ._options import cli_command
29+
30+
31+
@cli_command("config")
32+
@click.option("-e", "--edit", is_flag=True, help="Edit configuration file.")
33+
@click.option("-w", "--write", is_flag=True, help="Write current configuration file.")
34+
@click.pass_obj
35+
def cli_config(
36+
conf: Configuration,
37+
edit: bool,
38+
write: bool,
39+
):
40+
"""Manage launcher configuration.
41+
42+
Run without any options, the current configuration will be printed to
43+
stdout. (This is the effective configuration, not the contents of the
44+
configuration file, which may be less.)
45+
46+
When using the --edit flag, the EDITOR environment variable is used
47+
to select the editor to use for the file:
48+
49+
EDITOR="code -w" cloe-launch config -e
50+
51+
The configuration file is located at:
52+
53+
~/.config/cloe/launcher/conf.toml
54+
55+
The runtime directory is located at:
56+
57+
~/.cache/cloe/launcher/
58+
59+
Usage Examples:
60+
61+
\b
62+
cloe-launch config
63+
cloe-launch config -we
64+
"""
65+
66+
if not (write or edit):
67+
conf.print()
68+
if write:
69+
conf.write()
70+
if edit:
71+
conf.edit(create = True)

0 commit comments

Comments
 (0)