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")
}
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
}