2023-01-13 05:07:57 +00:00
|
|
|
package meta
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-01-14 06:02:25 +00:00
|
|
|
"io/fs"
|
2023-01-13 05:07:57 +00:00
|
|
|
"os"
|
2023-01-18 18:27:34 +00:00
|
|
|
"path/filepath"
|
2023-01-27 20:57:02 +00:00
|
|
|
"runtime"
|
2023-01-13 05:07:57 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-01-15 03:31:58 +00:00
|
|
|
"go.jolheiser.com/eget/disk"
|
2023-01-13 05:07:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Meta struct {
|
2023-01-15 03:31:58 +00:00
|
|
|
Packages map[string]Package `json:"packages"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Package struct {
|
|
|
|
Repo string `json:"repo"`
|
|
|
|
Version string `json:"version"`
|
2023-01-13 05:07:57 +00:00
|
|
|
}
|
|
|
|
|
2023-01-15 03:31:58 +00:00
|
|
|
func Upsert(name, repo, version string) error {
|
2023-01-13 05:07:57 +00:00
|
|
|
m, err := Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-14 06:02:25 +00:00
|
|
|
for n := range m.Packages {
|
|
|
|
if strings.EqualFold(n, name) {
|
2023-01-15 03:31:58 +00:00
|
|
|
m.Packages[n] = Package{
|
|
|
|
Repo: repo,
|
|
|
|
Version: version,
|
|
|
|
}
|
2023-01-14 06:02:25 +00:00
|
|
|
return save(m)
|
2023-01-13 05:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-15 03:31:58 +00:00
|
|
|
m.Packages[name] = Package{
|
|
|
|
Repo: repo,
|
|
|
|
Version: version,
|
|
|
|
}
|
2023-01-13 05:07:57 +00:00
|
|
|
return save(m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Read() (Meta, error) {
|
2023-01-14 06:02:25 +00:00
|
|
|
m := Meta{
|
2023-01-15 03:31:58 +00:00
|
|
|
Packages: make(map[string]Package),
|
2023-01-14 06:02:25 +00:00
|
|
|
}
|
2023-01-18 18:27:34 +00:00
|
|
|
fp := metaPath()
|
2023-01-13 05:07:57 +00:00
|
|
|
fi, err := os.Open(fp)
|
|
|
|
if err != nil {
|
2023-01-14 06:02:25 +00:00
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
return m, json.Unmarshal([]byte(`{}`), &m)
|
|
|
|
}
|
2023-01-13 05:07:57 +00:00
|
|
|
return m, fmt.Errorf("could not open meta file: %w", err)
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
|
|
|
|
|
|
|
if err := json.NewDecoder(fi).Decode(&m); err != nil {
|
|
|
|
return m, fmt.Errorf("could not decode meta: %w", err)
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Remove(name string) error {
|
|
|
|
m, err := Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-14 06:02:25 +00:00
|
|
|
for n := range m.Packages {
|
|
|
|
if strings.EqualFold(n, name) {
|
|
|
|
delete(m.Packages, name)
|
2023-01-13 05:07:57 +00:00
|
|
|
return save(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fmt.Errorf("could not find package to remove for %q", name)
|
|
|
|
}
|
|
|
|
|
2023-01-18 18:27:34 +00:00
|
|
|
func metaPath() string {
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
home = "."
|
|
|
|
}
|
|
|
|
return filepath.Join(home, ".eget", "packages.json")
|
2023-01-15 03:31:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-13 05:07:57 +00:00
|
|
|
func save(m Meta) error {
|
2023-01-18 18:27:34 +00:00
|
|
|
fp := metaPath()
|
2023-01-13 05:07:57 +00:00
|
|
|
fi, err := os.Create(fp)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create meta file: %w", err)
|
|
|
|
}
|
2023-01-15 03:31:58 +00:00
|
|
|
if err := json.NewEncoder(fi).Encode(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := fi.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-16 04:20:54 +00:00
|
|
|
if err := m.writeShellEnv("nu"); err != nil {
|
|
|
|
return err
|
2023-01-15 03:31:58 +00:00
|
|
|
}
|
2023-01-16 04:20:54 +00:00
|
|
|
if err := m.writeShellEnv("sh"); err != nil {
|
|
|
|
return err
|
2023-01-15 03:31:58 +00:00
|
|
|
}
|
2023-01-16 04:20:54 +00:00
|
|
|
if err := m.writeShellEnv("ps1"); err != nil {
|
|
|
|
return err
|
2023-01-15 03:31:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-16 04:20:54 +00:00
|
|
|
func (m Meta) writeShellEnv(shell string) error {
|
|
|
|
var tmpl string
|
|
|
|
switch shell {
|
|
|
|
case "nu":
|
2023-01-27 20:57:02 +00:00
|
|
|
path := "PATH"
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
path = "Path"
|
|
|
|
}
|
|
|
|
tmpl = fmt.Sprintf("let-env %s = ($env.%s | append %%q)\n", path)
|
2023-01-16 04:20:54 +00:00
|
|
|
case "sh":
|
|
|
|
tmpl = "PATH=$PATH:%s\n"
|
|
|
|
case "ps1":
|
|
|
|
tmpl = `$env:Path = "$env:Path;%s"` + "\n"
|
|
|
|
default:
|
|
|
|
return errors.New("shell not recognized")
|
|
|
|
}
|
2023-01-18 18:27:34 +00:00
|
|
|
home, err := os.UserHomeDir()
|
2023-01-16 04:20:54 +00:00
|
|
|
if err != nil {
|
2023-01-18 18:27:34 +00:00
|
|
|
home = "."
|
2023-01-16 04:20:54 +00:00
|
|
|
}
|
2023-01-18 18:27:34 +00:00
|
|
|
fp := filepath.Join(home, ".eget", "eget."+shell)
|
2023-01-16 04:20:54 +00:00
|
|
|
fi, err := os.Create(fp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
2023-01-15 03:31:58 +00:00
|
|
|
|
|
|
|
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)))
|
|
|
|
}
|
2023-01-16 04:20:54 +00:00
|
|
|
fi.WriteString(out.String())
|
2023-01-15 03:31:58 +00:00
|
|
|
|
2023-01-16 04:20:54 +00:00
|
|
|
return nil
|
2023-01-13 05:07:57 +00:00
|
|
|
}
|