diff options
| author | ewy <ewy0@protonmail.com> | 2026-04-22 19:27:25 +0200 |
|---|---|---|
| committer | ewy <ewy0@protonmail.com> | 2026-04-22 19:29:16 +0200 |
| commit | 5e1e840bbcdf8348ae62145e1b7231764fcc5a97 (patch) | |
| tree | dad5d223dc95b643febde31c45beb2d68110aa9c | |
| parent | f13b2bcf9a3a5ea6afeaa60c7a2e801e8e79a2c1 (diff) | |
update script experiment
| -rw-r--r-- | .pik/site/update.sh | 1 | ||||
| -rw-r--r-- | .pik/web/update.sh | 3 | ||||
| -rw-r--r-- | .pik/web/web.sh | 4 | ||||
| -rw-r--r-- | cache/cache.go | 8 | ||||
| -rw-r--r-- | cache/cache_test.go | 52 | ||||
| -rw-r--r-- | indexers/pikdex/index.go | 4 | ||||
| -rw-r--r-- | main.go | 4 | ||||
| -rw-r--r-- | menu/banner.go | 14 | ||||
| -rw-r--r-- | runner/shell/shell.go | 6 | ||||
| -rw-r--r-- | search/search_test.go | 20 | ||||
| -rw-r--r-- | testx/create.go | 1 |
11 files changed, 103 insertions, 14 deletions
diff --git a/.pik/site/update.sh b/.pik/site/update.sh deleted file mode 100644 index b4745e1..0000000 --- a/.pik/site/update.sh +++ /dev/null @@ -1 +0,0 @@ -ssh git@ewy.one -- cd /srv/pik/pik "&&" git pull
\ No newline at end of file diff --git a/.pik/web/update.sh b/.pik/web/update.sh new file mode 100644 index 0000000..0b53154 --- /dev/null +++ b/.pik/web/update.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +# update files on website +ssh git@ewy.one -- cd /srv/pik/pik "&&" .pik/web/web.sh
\ No newline at end of file diff --git a/.pik/web/web.sh b/.pik/web/web.sh new file mode 100644 index 0000000..b2d0575 --- /dev/null +++ b/.pik/web/web.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash +# gets run on server after calling `pik web update` from anywhere +set -euo pipefail +git pull
\ No newline at end of file diff --git a/cache/cache.go b/cache/cache.go index 8a17592..f036052 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -143,11 +143,3 @@ outer: Entries: result, } } - -func Touch() error { - fd, err := os.Create(Path) - if fd != nil { - defer fd.Close() - } - return err -} diff --git a/cache/cache_test.go b/cache/cache_test.go index 499c0f5..38c100a 100644 --- a/cache/cache_test.go +++ b/cache/cache_test.go @@ -1,11 +1,37 @@ +//go:build test + package cache import ( "github.com/stretchr/testify/assert" + . "pik/testx" "strings" "testing" + "testing/fstest" ) +func TestLoadFile(t *testing.T) { + input := ` + +# comment +/asdf/hjkl # label + +# other comment +//comment + +` + f := fstest.MapFS{ + "contexts": &fstest.MapFile{ + Data: []byte(input), + }, + } + res, err := LoadFile(f, "contexts") + assert.NoError(t, err) + assert.Len(t, res.Entries, 1) + assert.Equal(t, res.Entries[0].Path, "/asdf/hjkl") + assert.Equal(t, res.Entries[0].Label, "label") +} + func TestFromReader_Blank(t *testing.T) { input := ` ` @@ -113,3 +139,29 @@ func TestMerge(t *testing.T) { assert.Len(t, result.Entries, 3) assert.Contains(t, result.Entries, a, b, c) } + +func TestCache_String(t *testing.T) { + c := Cache{Entries: []Entry{{Path: "/asdf/hjkl", Label: "hjkl"}}} + output := c.String() + for _, e := range c.Entries { + assert.Contains(t, output, e.Path) + assert.Contains(t, output, e.Label) + } +} + +func TestNew(t *testing.T) { + st := TState(TSource("/test/location", "target1", "target2"), TSource("/other/place/asdf/hjkl", "1target", "2target")) + c := New(st) + assert.NotNil(t, c) + assert.Len(t, c.Entries, 2) + assert.Equal(t, "/test/location", c.Entries[0].Path) + assert.Equal(t, "/other/place/asdf/hjkl", c.Entries[1].Path) +} + +func TestLoadFile_NotExist(t *testing.T) { + f := fstest.MapFS{} + c, err := LoadFile(f, "anything is fine") + assert.Nil(t, c.Entries) + assert.Error(t, err) + +} diff --git a/indexers/pikdex/index.go b/indexers/pikdex/index.go index 7328125..9a54502 100644 --- a/indexers/pikdex/index.go +++ b/indexers/pikdex/index.go @@ -10,6 +10,7 @@ import ( "pik/spool" "slices" "strings" + "sync" ) var Roots = []string{ @@ -56,6 +57,7 @@ func (u *pikdex) Init() error { var Indexer = &pikdex{mods: make(map[string]*SourceData)} type pikdex struct { + sync.Mutex mods map[string]*SourceData } @@ -126,7 +128,9 @@ func (u *pikdex) Index(absPath string, f fs.FS, runners []model.Runner) ([]model } return nil }) + u.Lock() u.mods[absPath] = mod + u.Unlock() return targets, err } @@ -172,7 +172,7 @@ func main() { result := search.Search(st, args...) // TODO: Move auto-all logic into Search? - if !*flags.All && result.Target == nil && len(args) > 0 { + if !*flags.All && result.Target == nil && len(result.Args) > 0 { ForceConfirm = true if err != nil { _, _ = spool.Warn("%v\n", err) @@ -198,7 +198,7 @@ func main() { if result.Overridden { _, _ = fmt.Fprintln(os.Stderr, menu.OverrideWarning(result.Target)) } - err = run.Run(result.Source, result.Target, args...) + err = run.Run(result.Source, result.Target, result.Args...) if err != nil { _, _ = spool.Warn("%v\n", err) os.Exit(1) diff --git a/menu/banner.go b/menu/banner.go index b65cbbf..33d79d4 100644 --- a/menu/banner.go +++ b/menu/banner.go @@ -43,6 +43,9 @@ var ( BannerDryStyle = style.New(func() lipgloss.Style { return lipgloss.NewStyle().Foreground(BannerDryColor).Bold(true).MarginRight(1) }) + BannerDefaultStyle = style.New(func() lipgloss.Style { + return lipgloss.NewStyle().Faint(true).MarginLeft(1) + }) ) func Banner(source *model.Source, target model.Target, args ...string) string { @@ -53,7 +56,15 @@ func Banner(source *model.Source, target model.Target, args ...string) string { parts = append(parts, BannerPromptStyle.Render("> ")) parts = append(parts, BannerSelfStyle.Render("pik")) parts = append(parts, BannerSourceLabelStyle.Render(source.Label())) + def := false if sub := target.Sub(); sub != nil { + + // remove "default" invocations + if sub[len(sub)-1] == target.ShortestId() { + sub = sub[:len(sub)-1] + def = true + } + for i, s := range sub { sub[i] = BannerSubItemStyle.Render(s) } @@ -75,6 +86,9 @@ func Banner(source *model.Source, target model.Target, args ...string) string { parts = append(parts, BannerArgsStyle.Render(argParts...)) } + if def { + parts = append(parts, BannerDefaultStyle.Render("# "+target.Label())) + } result := BannerStyle.Render(lipgloss.JoinHorizontal(lipgloss.Left, parts...)) return result } diff --git a/runner/shell/shell.go b/runner/shell/shell.go index 3c4bddb..a4aee9d 100644 --- a/runner/shell/shell.go +++ b/runner/shell/shell.go @@ -33,8 +33,8 @@ var Runner = &shell{ } type shell struct { + sync.Mutex Locations map[string]string - lock sync.Mutex } func (s *shell) Wants(f fs.FS, file string, entry fs.DirEntry) (bool, error) { @@ -69,9 +69,9 @@ func (s *shell) Find(shell string) (string, error) { } if p, err := exec.LookPath(shell); err == nil { - s.lock.Lock() + s.Lock() s.Locations[shell] = p - s.lock.Unlock() + s.Unlock() return shell, nil } else { return "", err diff --git a/search/search_test.go b/search/search_test.go index 607a91e..5eaa1b9 100644 --- a/search/search_test.go +++ b/search/search_test.go @@ -113,3 +113,23 @@ func TestSearch_Override(t *testing.T) { assert.Equal(t, "abc.override.sh", res.Target.(*testx.TestTarget).Id.Full) assert.False(t, res.NeedsConfirmation) } + +func TestSearch_SubdirDefault(t *testing.T) { + tgt := testx.TTarget("subname", "subname") + src := testx.TSource("src") + src.Targets = append(src.Targets, tgt) + st := testx.TState(src) + res := Search(st, "subname") + assert.Nil(t, res.Args) + assert.Equal(t, res.Target, tgt) + assert.False(t, res.NeedsConfirmation) +} + +func TestSearch_SourceDefault(t *testing.T) { + src := testx.TSource("sourcename", "sourcename") + st := testx.TState(src) + res := Search(st, "sourcename") + assert.Nil(t, res.Args) + assert.Equal(t, res.Target, src.Targets[0]) + assert.False(t, res.NeedsConfirmation) +} diff --git a/testx/create.go b/testx/create.go index 62960fd..24f8259 100644 --- a/testx/create.go +++ b/testx/create.go @@ -18,6 +18,7 @@ func TTarget(name string, sub ...string) model.Target { func TSource(name string, targets ...string) *model.Source { src := &model.Source{ + Path: name, Identity: identity.New(name), } for _, t := range targets { |
