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

Add oci-env edit-env command #73

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions client/oci_env/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
import os
import pathlib

from oci_env.utils import exit_if_failed, exit_with_error
from oci_env.utils import (
exit_if_failed,
exit_with_error,
get_oci_env_path,
get_env_file,
get_config
)
from oci_env.templates import profile_templates


Expand Down Expand Up @@ -93,15 +99,17 @@ def pulpcore_manager(args, client):
client.exec(["pulpcore-manager"] + args.command, interactive=True)


def profile(args, client):
src_dir = client.config["SRC_DIR"]
def profile(args):
path = get_oci_env_path()
config = get_config(get_env_file(path, args.env_file))
src_dir = config["SRC_DIR"]

if args.action == "init":
if args.plugin:
profiles_dir = os.path.join(src_dir, args.plugin, "profiles")
profile_name = f"{args.plugin}/{args.profile_name}"
else:
profiles_dir = os.path.join(client.path, "profiles")
profiles_dir = os.path.join(path, "profiles")
profile_name = args.profile_name

new_profile_dir = os.path.join(profiles_dir, args.profile_name)
Expand Down Expand Up @@ -164,3 +172,9 @@ def poll(args, client):

def pulp(args, client):
client.exec(["pulp"] + args.command, interactive=True)



def edit_env(args):
editor = os.getenv("EDITOR", "vim")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we default it to a "more" common editor, like vi?
Not all distributions come with vim installed by default.

subprocess.call([editor, get_env_file(get_oci_env_path(), args.env_file)])
28 changes: 25 additions & 3 deletions client/oci_env/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
pulpcore_manager,
profile,
poll,
pulp
edit_env,
pulp,
)

from oci_env.utils import (
Expand All @@ -23,6 +24,7 @@ def get_parser():
parser = argparse.ArgumentParser(description='Pulp OCI image developer environment.')
parser.add_argument('-v', action='store_true', dest='is_verbose', help="Print extra debug information.")
parser.add_argument('-e', type=str, default="", dest='env_file', help="Specify an environment file to use other than the default.")
parser.set_defaults(no_init_client=False)

subparsers = parser.add_subparsers()

Expand All @@ -36,6 +38,7 @@ def get_parser():
parse_profile_command(subparsers)
parse_poll_command(subparsers)
parse_pulp_cli_command(subparsers)
parse_edit_env(subparsers)

return parser

Expand Down Expand Up @@ -107,6 +110,8 @@ def parse_profile_command(subparsers):
docs.add_argument('profile', nargs="?", help='Profile to view.')
docs.set_defaults(func=profile, action="docs")

parser.set_defaults(no_init_client=True)


def parse_poll_command(subparsers):
parser = subparsers.add_parser('poll', help='Poll the status API until it comes up.')
Expand All @@ -121,6 +126,17 @@ def parse_pulp_cli_command(subparsers):
parser.set_defaults(func=pulp)


def parse_edit_env(subparsers):
parser = subparsers.add_parser(
'edit-env',
help=(
'Edit your current environment file in vim. To change the default editor, set the '
'EDITOR environment variable to the path for the binary of your choosing.'
Comment on lines +133 to +134
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'Edit your current environment file in vim. To change the default editor, set the '
'EDITOR environment variable to the path for the binary of your choosing.'
'Open the current environment file using your default EDITOR (or vi/vim if none is found).'
'To change the default editor, set the EDITOR environment variable to the path for '
'binary of your choosing.'

)
)
parser.set_defaults(func=edit_env, no_init_client=True)


def main():
parser = get_parser()
args = parser.parse_args()
Expand All @@ -129,9 +145,15 @@ def main():
parser.print_help()
exit()

client = Compose(args.is_verbose, args.env_file)
try:
args.func(args, client)
# Initializing the client generates the .compiled/ directory and performs a lot of
# validation that's not necessary for some sub and may cause these commands to fail
# when the user has an invalid environment file set up.
if args.no_init_client:
args.func(args)
else:
client = Compose(args.is_verbose, args.env_file)
args.func(args, client)
except KeyboardInterrupt:
print()
exit(1)