diff options
author | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-07-01 23:32:54 +0200 |
---|---|---|
committer | Gabriel A. Giovanini <mail@gabrielgio.me> | 2024-07-01 23:32:54 +0200 |
commit | 1b1460c8d4fa358433c51fd5293fd1c79f32aeff (patch) | |
tree | b87528374798941a89e07ead5b92c2842deb40b6 /pkg | |
parent | 8f9853c8e26ffbad74e6414cec31104281a3860b (diff) | |
download | cerrado-1b1460c8d4fa358433c51fd5293fd1c79f32aeff.tar.gz cerrado-1b1460c8d4fa358433c51fd5293fd1c79f32aeff.tar.bz2 cerrado-1b1460c8d4fa358433c51fd5293fd1c79f32aeff.zip |
feat: Add pathing to the tree tabv0.0.9
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/handler/git/handler.go | 20 | ||||
-rw-r--r-- | pkg/u/file.go | 21 | ||||
-rw-r--r-- | pkg/u/list.go | 10 | ||||
-rw-r--r-- | pkg/u/list_test.go | 30 |
4 files changed, 42 insertions, 39 deletions
diff --git a/pkg/handler/git/handler.go b/pkg/handler/git/handler.go index fd62e44..5e50122 100644 --- a/pkg/handler/git/handler.go +++ b/pkg/handler/git/handler.go @@ -233,6 +233,12 @@ func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) error { name := r.PathValue("name") ref := r.PathValue("ref") rest := r.PathValue("rest") + paths := []string{} + + // this is avoid Split from generating a len 1 array with empty string + if rest != "" { + paths = strings.Split(rest, "/") + } tree, err := g.gitService.GetTree(name, ref, rest) if err != nil { @@ -243,8 +249,8 @@ func (g *GitHandler) Tree(w http.ResponseWriter, r *http.Request) error { Name: name, Ref: ref, GitItemBase: &templates.GitItemTreePage{ - CurrentPath: rest, - Tree: tree, + Path: paths, + Tree: tree, }, } templates.WritePageTemplate(w, gitList) @@ -256,6 +262,12 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) error { name := r.PathValue("name") ref := r.PathValue("ref") rest := r.PathValue("rest") + paths := []string{} + + // this is avoid Split from generating a len 1 array with empty string + if rest != "" { + paths = strings.Split(rest, "/") + } isBin, err := g.gitService.IsBinary(name, ref, rest) if err != nil { @@ -268,7 +280,7 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) error { Name: name, Ref: ref, GitItemBase: &templates.GitItemBlobPage{ - File: rest, + Path: paths, Content: []byte("Binary file"), }, } @@ -303,7 +315,7 @@ func (g *GitHandler) Blob(w http.ResponseWriter, r *http.Request) error { Name: name, Ref: ref, GitItemBase: &templates.GitItemBlobPage{ - File: rest, + Path: paths, Content: code.Bytes(), }, } diff --git a/pkg/u/file.go b/pkg/u/file.go index cf86c75..fafe0fb 100644 --- a/pkg/u/file.go +++ b/pkg/u/file.go @@ -4,6 +4,7 @@ import ( "errors" "log/slog" "os" + "path/filepath" ) func FileExist(filename string) bool { @@ -19,3 +20,23 @@ func FileExist(filename string) bool { return false } } + +// This is just a slin wraper to make easier to compose path in the template +type Pathing string + +func Root() Pathing { + return "/" +} + +func (s Pathing) AddPath(p string) Pathing { + return Pathing(filepath.Join(string(s), p)) +} + +func (s Pathing) AddPaths(p []string) Pathing { + f := filepath.Join(p...) + return Pathing(filepath.Join(string(s), f)) +} + +func (s Pathing) Done() string { + return string(s) +} diff --git a/pkg/u/list.go b/pkg/u/list.go index 7271ef3..39d7b11 100644 --- a/pkg/u/list.go +++ b/pkg/u/list.go @@ -16,12 +16,12 @@ func FirstOrZero[T any](v []T) T { return v[0] } -func Map[T any, V any](ts []T, fun func(T) V) []V { - rs := make([]V, len(ts)) - for i := range ts { - rs[i] = fun(ts[i]) +func LastOrZero[T any](v []T) T { + if len(v) == 0 { + var zero T + return zero } - return rs + return v[len(v)-1] } func ChunkBy[T any](items []T, chunkSize int) [][]T { diff --git a/pkg/u/list_test.go b/pkg/u/list_test.go index 3a856b9..805a209 100644 --- a/pkg/u/list_test.go +++ b/pkg/u/list_test.go @@ -3,7 +3,6 @@ package u import ( - "strconv" "testing" "github.com/google/go-cmp/cmp" @@ -130,32 +129,3 @@ func TestFirstOrZero(t *testing.T) { }) } } - -func TestMap(t *testing.T) { - testCases := []struct { - name string - in []int - out []string - }{ - { - name: "empty", - in: []int{}, - out: []string{}, - }, - { - name: "not empty", - in: []int{1, 2, 3}, - out: []string{"1", "2", "3"}, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - out := Map(tc.in, func(v int) string { return strconv.Itoa(v) }) - - if diff := cmp.Diff(tc.out, out); diff != "" { - t.Errorf("Map error:\n%s", diff) - } - }) - } -} |