Skip to content

Commit

Permalink
✨ Added reload command to update shell envs
Browse files Browse the repository at this point in the history
  • Loading branch information
abdheshnayak committed Dec 26, 2024
1 parent 4df9848 commit 367aa4d
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 5 deletions.
25 changes: 22 additions & 3 deletions cmd/packages/shell/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func resetEnvs(envMap map[string]string, env []string) map[string]string {
}

type ShellArgs struct {
OnlyPrint bool
ShellData *fileclient.ShellData
Shell string
EnvVars []string
Expand All @@ -54,10 +55,13 @@ func NixShell(cmd *cobra.Command, args ShellArgs) error {
envMap := fn.EnvSliceToMap(append(os.Environ(), args.EnvVars...))
envMap = resetEnvs(envMap, []string{"PATH", "LD_LIBRARY_PATH", "CPATH"})

newenvs := make(map[string]string)

if args.ShellData != nil {
m := args.ShellData.Envs
for k, v := range m {
envMap[k] = v
newenvs[k] = v
}
}

Expand All @@ -66,14 +70,13 @@ func NixShell(cmd *cobra.Command, args ShellArgs) error {
if err != nil {
return fn.NewE(err)
}

for k, v := range m {
envMap[k] = v
newenvs[k] = v
}

fc := clients.File
if err != nil {
return fn.NewE(err)
}

wc, err := fc.GetWsContext()
if err != nil {
Expand Down Expand Up @@ -102,6 +105,22 @@ func NixShell(cmd *cobra.Command, args ShellArgs) error {
envMap[k] = v
}

if args.OnlyPrint {

output := ""

for k, v := range newenvs {
// fmt.Println("export", k, v)
output += fmt.Sprintf("export %s=%q\n", k, v)
}

output += fmt.Sprintf("export PATH=%s:%s\n", newenvs["KL_NIX_PATH"], envMap["KL_OLD_PATH"])

fmt.Println(output)

return nil
}

c := exec.Command(shell, extraArgs...)
if flags.IsVerbose {
fn.Log(c.String())
Expand Down
7 changes: 7 additions & 0 deletions cmd/packages/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,22 @@ func Shell(cmd *cobra.Command, args []string) error {
envs = append(envs, fmt.Sprintf("%s=%s", k, v))
}

onlyPrint := fn.ParseBoolFlag(cmd, "onlyprint")

if err := NixShell(cmd, ShellArgs{
Shell: os.Getenv("SHELL"),
EnvVars: append(envs, "KL_SHELL=true", fmt.Sprintf("kl_mounts=%s", mountpath)),
Packages: pkgs,
Libraries: libs,
ShellData: ck.ShellData,
OnlyPrint: onlyPrint,
}); err != nil {
return fn.NewE(err)
}

return nil
}

func init() {
Cmd.Flags().BoolP("onlyprint", "p", false, "print vars export command")
}
2 changes: 2 additions & 0 deletions cmd/packages/shell/shellrc.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ cd "{{ .ProjectDir }}" || exit
cd "$working_dir" || exit

export PATH="$KL_NIX_PATH:$PATH"

alias reload='eval "$(kl shell --onlyprint)"'
3 changes: 3 additions & 0 deletions cmd/packages/shell/shellrc_fish.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ cd "{{ .ProjectDir }}" || exit
cd "$workingDir" || exit

set -gx PATH $KL_NIX_PATH $PATH


alias reload 'eval "$(kl shell --onlyprint)"'
1 change: 1 addition & 0 deletions cmd/runner/add/add-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ func selectAndAddConfig(cmd *cobra.Command, args []string) error {

fn.Log(fmt.Sprintf("added config %s/%s to your kl-file", selectedConfigGroup.Metadata.Name, selectedConfigKey.Key))

fn.WarnReload()
return nil
}

Expand Down
1 change: 1 addition & 0 deletions cmd/runner/add/add-mres.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func AddMres(apic apiclient.ApiClient, cmd *cobra.Command, args []string) error

fn.Log(fmt.Sprintf("added mres %s/%s to your kl-file", mres.SecretRefName.Name, *mresKey))

fn.WarnReload()
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions cmd/runner/add/add-secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ func selectAndAddSecret(cmd *cobra.Command, args []string) error {

fn.Log(fmt.Sprintf("added secret %s/%s to your kl-file", selectedSecretGroup.Metadata.Name, selectedSecretKey.Key))

fn.WarnReload()

return nil
}

Expand Down
1 change: 1 addition & 0 deletions cmd/runner/add/envvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func addEnvvar(cmd *cobra.Command, args []string) error {

fn.Log(text.Green(fmt.Sprintf("added envvar %s=%s to your kl-file", key, value)))

fn.WarnReload()
return nil
}

Expand Down
1 change: 1 addition & 0 deletions cmd/runner/add/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ func selectConfigMount(apic apiclient.ApiClient, klFile fileclient.KLFileType, c

fn.Log("added mount to your kl-file")

fn.WarnReload()
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/functions/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"os/exec"
"strconv"
"strings"

"github.com/kloudlite/kl/pkg/ui/text"
)

func ExecCmd(cmdString string, env map[string]string, verbose bool) error {
Expand Down Expand Up @@ -174,3 +176,9 @@ func EnvSliceToMap(env []string) map[string]string {
}
return result
}

func WarnReload() {
if _, ok := os.LookupEnv("KL_SHELL"); ok {
Warn(text.Yellow("environment variables are updated, please run `reload` to reflect changes to your current shell"))
}
}
5 changes: 3 additions & 2 deletions pkg/nixpkghandler/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (p *pkgHandler) SyncLockfile() error {
lf.Libraries = newLock
lf.Checksum = lfcheck

fn.WarnReload()
return lf.Save()
}

Expand Down Expand Up @@ -241,8 +242,8 @@ func (p *pkgHandler) EvaluateShell(ctx context.Context, packages []string, libra
libPaths = createSet(libPaths)
includes = createSet(includes)

resp["LD_LIBRARY_PATH"] = fmt.Sprintf("%s:%s", strings.Join(libPaths, ":"), os.Getenv("LD_LIBRARY_PATH"))
resp["CPATH"] = fmt.Sprintf("%s:%s", strings.Join(includes, ":"), os.Getenv("CPATH"))
resp["LD_LIBRARY_PATH"] = strings.ReplaceAll(fmt.Sprintf("%s:%s", strings.Join(libPaths, ":"), os.Getenv("LD_LIBRARY_PATH")), "::", ":")
resp["CPATH"] = strings.ReplaceAll(fmt.Sprintf("%s:%s", strings.Join(includes, ":"), os.Getenv("CPATH")), "::", ":")

return resp, nil
}
Expand Down

0 comments on commit 367aa4d

Please sign in to comment.