Skip to content

Commit d0ffb16

Browse files
committed
feat: add home directory flag and environment handling for sling CLI
1 parent 9ef85c5 commit d0ffb16

7 files changed

Lines changed: 93 additions & 56 deletions

File tree

cmd/sling/sling_cli.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ var cliRunFlags = []g.Flag{
193193
Type: "bool",
194194
Description: "Shows some examples.",
195195
},
196+
{
197+
Name: "home-dir",
198+
Type: "string",
199+
Description: "Specify the sling home directory to use.",
200+
},
196201
}
197202

198203
var cliRun = &g.CliSC{
@@ -270,11 +275,23 @@ var cliConns = &g.CliSC{
270275
Type: "bool",
271276
Description: "Set logging level to TRACE (do not use in production).",
272277
},
278+
{
279+
Name: "home-dir",
280+
Type: "string",
281+
Description: "Specify the sling home directory to use.",
282+
},
273283
},
274284
},
275285
{
276286
Name: "list",
277287
Description: "list local connections detected",
288+
Flags: []g.Flag{
289+
{
290+
Name: "home-dir",
291+
Type: "string",
292+
Description: "Specify the sling home directory to use.",
293+
},
294+
},
278295
},
279296
{
280297
Name: "test",
@@ -304,6 +321,11 @@ var cliConns = &g.CliSC{
304321
Type: "string",
305322
Description: "The endpoint(s) to test, for API connections only",
306323
},
324+
{
325+
Name: "home-dir",
326+
Type: "string",
327+
Description: "Specify the sling home directory to use.",
328+
},
307329
},
308330
},
309331
{
@@ -317,6 +339,13 @@ var cliConns = &g.CliSC{
317339
Description: "The name of the connection to remove",
318340
},
319341
},
342+
Flags: []g.Flag{
343+
{
344+
Name: "home-dir",
345+
Type: "string",
346+
Description: "Specify the sling home directory to use.",
347+
},
348+
},
320349
},
321350
{
322351
Name: "set",
@@ -335,6 +364,13 @@ var cliConns = &g.CliSC{
335364
Description: "The key=value properties to set. See https://docs.slingdata.io/sling-cli/environment#set-connections",
336365
},
337366
},
367+
Flags: []g.Flag{
368+
{
369+
Name: "home-dir",
370+
Type: "string",
371+
Description: "Specify the sling home directory to use.",
372+
},
373+
},
338374
},
339375
{
340376
Name: "exec",
@@ -365,6 +401,11 @@ var cliConns = &g.CliSC{
365401
Type: "bool",
366402
Description: "Set logging level to TRACE (do not use in production).",
367403
},
404+
{
405+
Name: "home-dir",
406+
Type: "string",
407+
Description: "Specify the sling home directory to use.",
408+
},
368409
},
369410
},
370411
},

cmd/sling/sling_conns.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,16 @@ var (
2525
func processConns(c *g.CliSC) (ok bool, err error) {
2626
ok = true
2727

28+
if homeDir := cast.ToString(c.Vals["home-dir"]); homeDir != "" {
29+
os.Setenv("SLING_HOME_DIR", homeDir)
30+
env.LoadHomeDir()
31+
}
32+
2833
ef := env.LoadSlingEnvFile()
2934
ec := connection.EnvFileConns{EnvFile: &ef}
3035
asJSON := os.Getenv("SLING_OUTPUT") == "json"
3136

32-
entries := connection.GetLocalConns()
37+
entries := connection.GetLocalConns(true)
3338
defer connection.CloseAll()
3439

3540
env.SetTelVal("task_start_time", time.Now())

cmd/sling/sling_run.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ func processRun(c *g.CliSC) (ok bool, err error) {
210210
selectStreams = strings.Split(cast.ToString(v), ",")
211211
case "examples":
212212
showExamples = cast.ToBool(v)
213+
case "home-dir":
214+
if homeDir := cast.ToString(v); homeDir != "" {
215+
os.Setenv("SLING_HOME_DIR", homeDir)
216+
env.LoadHomeDir()
217+
connection.GetLocalConns(true) // force reload connections
218+
}
213219
case "cdc-options":
214220
payload := cast.ToString(v)
215221
options, err := parsePayload(payload, true)

core/dbio/connection/connection_local.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,21 @@ func GetLocalConns(options ...any) ConnEntries {
130130
}
131131
}
132132

133-
for name, homeDir := range env.HomeDirs {
134-
envFilePath := env.GetEnvFilePath(homeDir)
135-
if g.PathExists(envFilePath) {
136-
m := g.M()
137-
g.JSONConvert(env.LoadEnvFile(envFilePath), &m)
138-
profileConns, err := ReadConnections(m)
139-
if !g.LogError(err) {
140-
for _, conn := range profileConns {
141-
c := ConnEntry{
142-
Name: strings.ToUpper(conn.Info().Name),
143-
Description: conn.GetType().NameLong(),
144-
Source: name + " env yaml",
145-
Connection: conn,
146-
}
147-
connsMap[c.Name] = c
133+
if envFilePath := env.GetEnvFilePath(env.HomeDir); g.PathExists(envFilePath) {
134+
m := g.M()
135+
g.JSONConvert(env.LoadEnvFile(envFilePath), &m)
136+
profileConns, err := ReadConnections(m)
137+
if !g.LogError(err) {
138+
for _, conn := range profileConns {
139+
c := ConnEntry{
140+
Name: strings.ToUpper(conn.Info().Name),
141+
Description: conn.GetType().NameLong(),
142+
Source: "sling env yaml",
143+
Connection: conn,
148144
}
145+
connsMap[c.Name] = c
149146
}
150147
}
151-
152148
}
153149

154150
// env.yaml as an Environment variable

core/dbio/database/database_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,6 @@ func TestBigQuery(t *testing.T) {
368368
}
369369

370370
func connect(db *testDB) (conn Connection, err error) {
371-
env.SetHomeDir("sling")
372371
connsMap, _ = env.GetHomeDirConnsMap()
373372

374373
if ce, ok := connsMap[db.name]; ok {

core/env/env.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ var (
3535
LogSink func(*g.LogLine)
3636
TelMap = g.M("begin_time", time.Now().UnixMicro())
3737
TelMux = sync.Mutex{}
38-
HomeDirs = map[string]string{}
3938
envMux = sync.Mutex{}
4039
NoDebugKey = " /* nD */"
4140
Executable = ""
@@ -99,25 +98,13 @@ const (
9998
var envFolder embed.FS
10099

101100
func init() {
102-
103-
HomeDir = SetHomeDir("sling")
104-
HomeDirEnvFile = GetEnvFilePath(HomeDir)
105101
Executable, _ = osext.Executable()
106102

107-
// create env file if not exists
108-
os.MkdirAll(HomeDir, 0755)
109-
if HomeDir != "" && !g.PathExists(HomeDirEnvFile) {
110-
defaultEnvBytes, _ := envFolder.ReadFile("default.env.yaml")
111-
os.WriteFile(HomeDirEnvFile, defaultEnvBytes, 0644)
112-
}
113-
114103
if content := os.Getenv("SLING_ENV_YAML"); content != "" {
115104
os.Setenv("ENV_YAML", content)
116105
}
117106

118-
// other sources of creds
119-
SetHomeDir("dbnet") // https://github.com/dbnet-io/dbnet
120-
SetHomeDir("dbrest") // https://github.com/dbrest-io/dbrest
107+
LoadHomeDir()
121108

122109
if SentryDsn == "" {
123110
SentryDsn = os.Getenv("SENTRY_DSN")
@@ -139,6 +126,24 @@ func init() {
139126
}
140127
}
141128

129+
func LoadHomeDir() {
130+
envKey := "SLING_HOME_DIR"
131+
HomeDir = CleanWindowsPath(os.Getenv(envKey))
132+
if HomeDir == "" {
133+
HomeDir = CleanWindowsPath(path.Join(g.UserHomeDir(), ".sling"))
134+
os.Setenv(envKey, HomeDir)
135+
}
136+
137+
HomeDirEnvFile = GetEnvFilePath(HomeDir)
138+
139+
// create env file if not exists
140+
os.MkdirAll(HomeDir, 0755)
141+
if HomeDir != "" && !g.PathExists(HomeDirEnvFile) {
142+
defaultEnvBytes, _ := envFolder.ReadFile("default.env.yaml")
143+
os.WriteFile(HomeDirEnvFile, defaultEnvBytes, 0644)
144+
}
145+
}
146+
142147
func HomeBinDir() string {
143148
return path.Join(HomeDir, "bin")
144149
}
@@ -619,15 +624,13 @@ func GetHomeDirConnsMap() (connsMap map[string]map[string]any, err error) {
619624
defer envMux.Unlock()
620625
envMux.Lock()
621626
connsMap = map[string]map[string]any{}
622-
for _, homeDir := range HomeDirs {
623-
envFilePath := GetEnvFilePath(homeDir)
624-
if g.PathExists(envFilePath) {
625-
m := g.M()
626-
g.JSONConvert(LoadEnvFile(envFilePath), &m)
627-
cm, _ := readConnectionsMap(m)
628-
for k, v := range cm {
629-
connsMap[k] = v
630-
}
627+
envFilePath := GetEnvFilePath(HomeDir)
628+
if g.PathExists(envFilePath) {
629+
m := g.M()
630+
g.JSONConvert(LoadEnvFile(envFilePath), &m)
631+
cm, _ := readConnectionsMap(m)
632+
for k, v := range cm {
633+
connsMap[k] = v
631634
}
632635
}
633636
return connsMap, nil

core/env/envfile.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@ type EnvFile struct {
2323
Body string `json:"-" yaml:"-"`
2424
}
2525

26-
func SetHomeDir(name string) string {
27-
envKey := strings.ToUpper(name) + "_HOME_DIR"
28-
dir := os.Getenv(envKey)
29-
if dir == "" {
30-
dir = path.Join(g.UserHomeDir(), "."+name)
31-
os.Setenv(envKey, dir)
32-
}
33-
envMux.Lock()
34-
HomeDirs[name] = dir
35-
envMux.Unlock()
36-
return dir
37-
}
38-
3926
func (ef *EnvFile) WriteEnvFile() (err error) {
4027
connsMap := yaml.MapSlice{}
4128

@@ -217,7 +204,7 @@ func LoadEnvFile(path string) (ef EnvFile) {
217204
ef.Path = path
218205

219206
// expand variables
220-
envMap := map[string]any{}
207+
envMap := map[string]any{"SLING_HOME_DIR": HomeDir}
221208
for _, tuple := range os.Environ() {
222209
key := strings.Split(tuple, "=")[0]
223210
val := strings.TrimPrefix(tuple, key+"=")
@@ -252,5 +239,5 @@ func LoadEnvFile(path string) (ef EnvFile) {
252239
}
253240

254241
func GetEnvFilePath(dir string) string {
255-
return path.Join(dir, "env.yaml")
242+
return CleanWindowsPath(path.Join(dir, "env.yaml"))
256243
}

0 commit comments

Comments
 (0)