From c62290d1a1024e1d2049df0608ace6dc53eaf124 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Sun, 15 Jan 2023 22:20:54 -0600 Subject: [PATCH] feat: write env for nu, sh, and ps1 Signed-off-by: jolheiser --- meta/meta.go | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/meta/meta.go b/meta/meta.go index b19b877..c74f560 100644 --- a/meta/meta.go +++ b/meta/meta.go @@ -84,10 +84,6 @@ func metaPath() (string, error) { return xdg.ConfigFile("eget/packages.json") } -func nushellPath() (string, error) { - return xdg.DataFile("eget/eget.nu") -} - func save(m Meta) error { fp, err := metaPath() if err != nil { @@ -104,32 +100,47 @@ func save(m Meta) error { return err } - fp, err = nushellPath() - if err != nil { - return fmt.Errorf("could not get nushell path: %w", err) + if err := m.writeShellEnv("nu"); err != nil { + return err } - fi, err = os.Create(fp) - if err != nil { - return fmt.Errorf("could not create nushell file: %w", err) + if err := m.writeShellEnv("sh"); err != nil { + return err } - defer fi.Close() - out, err := m.nushell() - if err != nil { - return fmt.Errorf("could not generate nushell environment: %w", err) + if err := m.writeShellEnv("ps1"); err != nil { + return err } - fi.WriteString(out) return nil } -func (m Meta) nushell() (string, error) { - tmpl := "let-env PATH = ($env.PATH | append '%s')\n" +func (m Meta) writeShellEnv(shell string) error { + var tmpl string + switch shell { + case "nu": + tmpl = "let-env PATH = ($env.PATH | append '%s')\n" + case "sh": + tmpl = "PATH=$PATH:%s\n" + case "ps1": + tmpl = `$env:Path = "$env:Path;%s"` + "\n" + default: + return errors.New("shell not recognized") + } + fp, err := xdg.DataFile("eget/eget." + shell) + if err != nil { + return err + } + fi, err := os.Create(fp) + if err != nil { + return err + } + defer fi.Close() var out strings.Builder out.WriteString("# managed by eget; DO NOT EDIT\n\n") for name := range m.Packages { out.WriteString(fmt.Sprintf(tmpl, disk.Path(name))) } + fi.WriteString(out.String()) - return out.String(), nil + return nil }