feat: write env for nu, sh, and ps1
ci/woodpecker/tag/goreleaser Pipeline was successful
Details
ci/woodpecker/tag/goreleaser Pipeline was successful
Details
Signed-off-by: jolheiser <john.olheiser@gmail.com>main v0.0.1
parent
1ab01e3d8c
commit
c62290d1a1
47
meta/meta.go
47
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
|
||||
}
|
||||
|
|
Reference in New Issue