Write value to file #210
theoephraim
started this conversation in
New Features
Replies: 2 comments
-
|
also realized we might want a read from file function as well. Useful for things like JSON creds that GCP gives you. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
A quick summary of what I am doing today without varlock: I use
SSL_KEY=$HOME/cert/dev.local+4-key.pem
SSL_CERT=$HOME/cert/dev.local+4.pem
LOCAL_DOMAIN=dev.local
import { readFileSync } from "node:fs";
import { dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { loadEnv } from "vite";
const __dirname = dirname(fileURLToPath(import.meta.url));
const env = loadEnv("development", __dirname, "");
const readFile = (path: string | undefined) =>
path ? readFileSync(path, "utf-8") : undefined
const sslKey = readFile(env.SSL_KEY);
const sslCert = readFile(env.SSL_CERT);
const localDomain = env.LOCAL_DOMAIN
/// <reference types="vitest" />
export default defineConfig({
server: {
https:
sslKey && sslCert
? {
key: sslKey,
cert: sslCert,
}
: undefined,
allowedHosts: localDomain ? [localDomain] : [],
},
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Often as part of our secrets / config, we encounter certificates or other things that are usually stored in files. Especially if they are large, passing things around through env vars doesn't really make sense. So we want a way to use varlock to fetch the value and write it to a file. The overall goal would be to make this work for any plugin, and to probably keep the "fetch the value" part within an item value, rather than a decorator.
In general here the idea of having data that is not injected as env vars may be a helpful building block. See #549 for some discussion.
A few options / ideas:
Option 1 - new
filedata typeAdd a new data type called
filewhich is meant to save the contents of the item to a path, and then return the file path as the env var. Conceptually it's a little weirder but is relatively simple.Option 2 -
writeFileresolver which returns the pathKeep the value more related to being a file path, but have the data written as a side effect of a function. (Note that path is not a real type yet, but one that is planned on being added anyway).
Option 3 -
writeFileresolver which returns the file contentsBreak it up into 2 items, and have a
writeFilefunction that writes the file but returns the value rather than the path.In this case we may want another decorator to mark items that should not be injected as env vars (or could maybe use a naming convention). This one allows for validating both the path and the file contents independently, and is perhaps the most clear, but it's also a bit verbose.
We should also consider that often we wouldn't really need to inject the path as an env var because it is going to a known constant location, so you may not always need both items.
Option 4 -
writeFileroot decoratorCould just make writeFile a decorator. Root decorators must go in the header, but we could consider allowing these unattached decorators anywhere in the file. This way you'd be writing to a file, but no env var is needed. Or you could attach to an item too...
root decorator
item decorator
Beta Was this translation helpful? Give feedback.
All reactions