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

env: add a flag to output as environment variable bindings #6316

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
config command: factorise some list existence check
rjbou committed Dec 3, 2024
commit ff46b96ac1995868c5350f0e37d3956a3670b9f8
19 changes: 9 additions & 10 deletions src/client/opamConfigCommand.ml
Original file line number Diff line number Diff line change
@@ -62,6 +62,9 @@ let list t ns =
OpamStd.Format.align_table |>
OpamConsole.print_table stdout ~sep:" "

let name_not_in_env n env =
not (List.exists (fun (n', _, _) -> String.equal n n') env)

let possibly_unix_path_env_value k v =
if k = "PATH" then
(Lazy.force OpamSystem.get_cygpath_path_transform) ~pathlist:true v
@@ -72,8 +75,7 @@ let rec print_env output = function
| (k, v, comment) :: r ->
if OpamConsole.verbose () then
OpamStd.Option.iter (Printf.ksprintf output ": %s;\n") comment;
if not (List.exists (fun (k1, _, _) -> k = k1) r) || OpamConsole.verbose ()
then (
if name_not_in_env k r || OpamConsole.verbose () then (
let v' = possibly_unix_path_env_value k v in
Printf.ksprintf output "%s='%s'; export %s;\n"
k (OpamStd.Env.escape_single_quotes v') k);
@@ -84,8 +86,7 @@ let rec print_csh_env output = function
| (k, v, comment) :: r ->
if OpamConsole.verbose () then
OpamStd.Option.iter (Printf.ksprintf output ": %s;\n") comment;
if not (List.exists (fun (k1, _, _) -> k = k1) r) || OpamConsole.verbose ()
then (
if name_not_in_env k r || OpamConsole.verbose () then (
let v' = possibly_unix_path_env_value k v in
Printf.ksprintf output "setenv %s '%s';\n"
k (OpamStd.Env.escape_single_quotes v'));
@@ -94,8 +95,7 @@ let rec print_csh_env output = function
let rec print_pwsh_env output = function
| [] -> ()
| (k, v, _) :: r ->
if not (List.exists (fun (k1, _, _) -> k = k1) r) || OpamConsole.verbose ()
then
if name_not_in_env k r || OpamConsole.verbose () then
Printf.ksprintf output "$env:%s = '%s'\n"
k (OpamStd.Env.escape_powershell v);
print_pwsh_env output r
@@ -104,8 +104,7 @@ let print_cmd_env output env =
let rec aux = function
| [] -> ()
| (k, v, _) :: r ->
if not (List.exists (fun (k1, _, _) -> k = k1) r) || OpamConsole.verbose ()
then begin
if name_not_in_env k r || OpamConsole.verbose () then begin
let is_special = function
| '(' | ')' | '!' | '^' | '%' | '"' | '<' | '>' | '|' -> true
| _ -> false
@@ -123,7 +122,7 @@ let print_sexp_env output env =
let rec aux = function
| [] -> ()
| (k, v, _) :: r ->
if not (List.exists (fun (k1, _, _) -> k = k1) r) then
if name_not_in_env k r then
Printf.ksprintf output " (%S %S)\n" k v;
aux r
in
@@ -156,7 +155,7 @@ let rec print_fish_env output env =
match env with
| [] -> ()
| (k, v, _) :: r ->
if not (List.exists (fun (k1, _, _) -> k = k1) r) then
if name_not_in_env k r then
(match k with
| "PATH" | "CDPATH" ->
(* This function assumes that `v` does not include any variable