Skip to content

Commit 9e220a7

Browse files
committed
Add new 'env' library - environment library manager
1 parent 14bb59b commit 9e220a7

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

Diff for: src/env.q

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Environemnt Variable Manager
2+
// Copyright (c) 2020 - 2021 Jaskirat Rajasansir
3+
4+
.require.lib each `type`convert`os;
5+
6+
7+
/ Environment variables that should be loaded on library initialisation and optionally parsed by the specified parse function
8+
/ Specify null symbol to cache the environment variable value on library init as returned by 'getenv'
9+
/ @see .os.sharedObjectEnvVar
10+
.env.cfg.vars:(`symbol$())!`symbol$();
11+
.env.cfg.vars[`QHOME]: `.convert.stringToHsym;
12+
.env.cfg.vars[`QLIC]: `.convert.stringToHsym;
13+
.env.cfg.vars[`PATH]: `.env.i.parsePathTypeVar;
14+
.env.cfg.vars[.os.sharedObjectEnvVar]: `.env.i.parsePathTypeVar;
15+
16+
17+
/ The cached and optionally parsed environment variables
18+
.env.cache:1#.q;
19+
20+
21+
.env.init:{
22+
.env.loadAllEnvVars[];
23+
};
24+
25+
26+
/ Loads all the pre-configured environment variables from the current shell. This can be called at any point to update all environment
27+
/ variables in the cache
28+
/ @see .env.cfg.vars
29+
/ @see .env.i.loadEnvVar
30+
.env.loadAllEnvVars:{
31+
.log.if.info "Loading all configured environment variables [ Total: ",string[count .env.cfg.vars]," ]";
32+
.env.i.loadEnvVar ./: flip (key; value) @\: .env.cfg.vars;
33+
};
34+
35+
/ Queries the specified environment varaible either from the cache or directly via 'getenv' if not pre-configured
36+
/ @param envVar (Symbol) The environment variable to query
37+
/ @returns () The raw environment variable or the parsed result
38+
/ @throws EnvironmentVariableNotDefinedException If the environment variable is not set in the cache and an empty value from 'getenv'
39+
/ @see .env.cache
40+
/ @see getenv
41+
.env.get:{[envVar]
42+
if[not envVar in key .env.cache;
43+
envVal:getenv envVar;
44+
45+
if[0 = count envVal;
46+
'"EnvironmentVariableNotDefinedException";
47+
];
48+
49+
:envVal;
50+
];
51+
52+
:.env.cache envVar;
53+
};
54+
55+
56+
/ Loads and optionally parses the specified environment variable with the specified parse function reference
57+
/ @param envVar (Symbol) The environment variable to parse
58+
/ @param parseFunc (Symbol) Function reference for the parse function. If null, no parse is performed
59+
/ @see .env.cache
60+
.env.i.loadEnvVar:{[envVar; parseFunc]
61+
.log.if.debug "Loading environment variable [ Variable: ",string[envVar]," ] [ Parse Function: ",string[`none ^ parseFunc]," ]";
62+
63+
envVal:getenv envVar;
64+
65+
if[not null parseFunc;
66+
envVal:get[parseFunc] envVal;
67+
];
68+
69+
.env.cache[envVar]:envVal;
70+
};
71+
72+
73+
/ Parses a '$PATH'-type environment variable into a list of folder paths for use within kdb+
74+
/ NOTE: Only valid folders will be returned from this function
75+
/ @param rawPath (String) The environment variable output
76+
/ @returns (FolderPathList) The list of valid folders from the '$PATH'-type environment variable or empty symbol list if environment variable not set
77+
/ @see .type.isFolder
78+
.env.i.parsePathTypeVar:{[rawPath]
79+
paths:.os.envPathSeparator vs rawPath;
80+
81+
if[0 = count paths;
82+
:`symbol$();
83+
];
84+
85+
paths:`$":",/:paths;
86+
paths@:where .type.isFolder each paths;
87+
88+
:paths;
89+
};

0 commit comments

Comments
 (0)