Last active
December 12, 2020 20:34
-
-
Save AngelMunoz/a32c4b06d3cdd7752f649a2366693632 to your computer and use it in GitHub Desktop.
An attempt to append an Environment Variable for the current user on windows
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System | |
open Spectre.Console | |
open System.Collections.Generic | |
open System.Linq | |
let sampleVar = "SAMPLE_VAR" | |
let path = | |
let mapped = | |
Environment | |
.GetEnvironmentVariables(EnvironmentVariableTarget.User) | |
.Cast<DictionaryEntry>() | |
|> Seq.map (fun de -> string de.Key,string de.Value) | |
|> Map.ofSeq | |
let values = | |
(mapped.Item "Path").Split(';') | |
values | |
|> Array.map | |
(fun pathValue -> | |
if not (mapped |> Map.exists(fun _ value -> value = pathValue)) then | |
pathValue | |
else | |
let value = | |
mapped | |
|> Seq.tryFind (fun entry -> entry.Value = pathValue) | |
match value with | |
| Some kvp -> $"%%{kvp.Key}%%" | |
| None -> pathValue | |
) | |
|> Array.reduce (fun curr next -> $"{curr};{next}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment