feat: write env for nu, sh, and ps1
ci/woodpecker/tag/goreleaser Pipeline was successful Details

Signed-off-by: jolheiser <john.olheiser@gmail.com>
main v0.0.1
jolheiser 2023-01-15 22:20:54 -06:00
parent 1ab01e3d8c
commit c62290d1a1
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
1 changed files with 29 additions and 18 deletions

View File

@ -84,10 +84,6 @@ func metaPath() (string, error) {
return xdg.ConfigFile("eget/packages.json") return xdg.ConfigFile("eget/packages.json")
} }
func nushellPath() (string, error) {
return xdg.DataFile("eget/eget.nu")
}
func save(m Meta) error { func save(m Meta) error {
fp, err := metaPath() fp, err := metaPath()
if err != nil { if err != nil {
@ -104,32 +100,47 @@ func save(m Meta) error {
return err return err
} }
fp, err = nushellPath() if err := m.writeShellEnv("nu"); err != nil {
if err != nil { return err
return fmt.Errorf("could not get nushell path: %w", err)
} }
fi, err = os.Create(fp) if err := m.writeShellEnv("sh"); err != nil {
if err != nil { return err
return fmt.Errorf("could not create nushell file: %w", err)
} }
defer fi.Close() if err := m.writeShellEnv("ps1"); err != nil {
out, err := m.nushell() return err
if err != nil {
return fmt.Errorf("could not generate nushell environment: %w", err)
} }
fi.WriteString(out)
return nil return nil
} }
func (m Meta) nushell() (string, error) { func (m Meta) writeShellEnv(shell string) error {
tmpl := "let-env PATH = ($env.PATH | append '%s')\n" 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 var out strings.Builder
out.WriteString("# managed by eget; DO NOT EDIT\n\n") out.WriteString("# managed by eget; DO NOT EDIT\n\n")
for name := range m.Packages { for name := range m.Packages {
out.WriteString(fmt.Sprintf(tmpl, disk.Path(name))) out.WriteString(fmt.Sprintf(tmpl, disk.Path(name)))
} }
fi.WriteString(out.String())
return out.String(), nil return nil
} }