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 operator to query an environment variable #1232

Merged
merged 5 commits into from
Jul 5, 2023
Merged
Changes from all commits
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
51 changes: 51 additions & 0 deletions Bonsai.System/IO/GetEnvironmentVariable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.ComponentModel;
using System.Reactive.Linq;

namespace Bonsai.IO
{
/// <summary>
/// Represents an operator that gets the value of an environment variable for the current process.
/// </summary>
[Description("Returns the value of an environment variable for the current process.")]
public class GetEnvironmentVariable : Source<string>
{
/// <summary>
/// Gets or sets the name of the environment variable to query the value of.
/// </summary>
[Description("The name of the environment variable to query the value of.")]
bruno-f-cruz marked this conversation as resolved.
Show resolved Hide resolved
public string Name { get; set; }

/// <summary>
/// Gets the value of the specified environment variable for the current process
/// and returns it through an observable sequence.
/// </summary>
/// <returns>
/// A sequence containing the value of the specified environment variable. The
/// value will be <see langword="null"/> if the environment variable is not found.
/// </returns>
public override IObservable<string> Generate()
{
return Observable.Return(Environment.GetEnvironmentVariable(Name));
}

/// <summary>
/// Gets the value of the specified environment variable for the current process
/// whenever an observable sequence emits a notification.
/// </summary>
/// <typeparam name="TSource">
/// The type of the elements in the <paramref name="source"/> sequence.
/// </typeparam>
/// <param name="source">
/// The sequence of notifications used to get the value of the environment variable.
/// </param>
/// <returns>
/// A sequence containing the current values of the specified environment variable.
/// The value may be <see langword="null"/> if the environment variable is not found.
/// </returns>
public IObservable<string> Generate<TSource>(IObservable<TSource> source)
{
return source.Select(_ => Environment.GetEnvironmentVariable(Name));
}
}
}