DEV Community

Wesley de Groot
Wesley de Groot

Posted on • Originally published at wesleydegroot.nl on

Snippet: @EnvironmentVariable

If you are creating commandline apps you sometimes need to acces the operating system environment variables, while you can use getenv(name) it can come in handy to use a property wrapper if you need to access the environment variables often, or if you are updating them.

Full code:

/// A property wrapper to set and get system's environment variables values. /// ///

 ``` /// @EnvironmentVariable(name: "PATH") /// var path: String? /// ```

 @propertyWrapper public struct EnvironmentVariable { var name: String public var wrappedValue: String? { get { guard let pointer = getenv(name) else { return nil } return String(cString: pointer) } set { guard let value = newValue else { unsetenv(name) return } setenv(name, value, 1) } } }

in the future i'll post more about @propertyWrappers.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay