Skip to content

Instantly share code, notes, and snippets.

@AngelMunoz
Last active December 12, 2020 20:34
Show Gist options
  • Save AngelMunoz/a32c4b06d3cdd7752f649a2366693632 to your computer and use it in GitHub Desktop.
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
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