From ad9fe824293c4deebf5bcdb2225b9e605cf1f786 Mon Sep 17 00:00:00 2001 From: Manuel Riezebosch Date: Wed, 12 Feb 2020 09:21:47 +0100 Subject: [PATCH] feat(main): specify repository path on command line --- Main.go | 17 +++++++++++++++-- routes.go | 41 ++++++++++++++++++++++++++--------------- routes_test.go | 34 +++++++++++++++++++++------------- 3 files changed, 62 insertions(+), 30 deletions(-) diff --git a/Main.go b/Main.go index 0d70eeb..9115f46 100644 --- a/Main.go +++ b/Main.go @@ -1,15 +1,26 @@ package main import ( + "flag" "fmt" "net" "net/http" + "os" "github.com/pkg/browser" ) func main() { - listener, err := net.Listen("tcp", ":0") + port := flag.Int("port", 0, "") + flag.Usage = func() { + fmt.Println(fmt.Sprintf("%s [flags] [repository]", os.Args[0])) + fmt.Println() + fmt.Println("flags:") + flag.PrintDefaults() + } + flag.Parse() + + listener, err := net.Listen("tcp", fmt.Sprintf(":%v", *port)) if err != nil { panic(err) } @@ -18,5 +29,7 @@ func main() { fmt.Print(url) browser.OpenURL(url) - panic(http.Serve(listener, Routes(".git"))) + path := flag.Arg(0) + fmt.Print(path) + panic(http.Serve(listener, Routes(path))) } diff --git a/routes.go b/routes.go index 339b95a..8656d97 100644 --- a/routes.go +++ b/routes.go @@ -5,7 +5,9 @@ import ( "fmt" "io/ioutil" "net/http" + "os" "path/filepath" + "strings" "github.com/git-lfs/gitobj" "github.com/go-chi/chi" @@ -14,7 +16,7 @@ import ( ) // Routes for graph and content -func Routes(path string) *chi.Mux { +func Routes(wd string) *chi.Mux { r := chi.NewRouter() cors := cors.New(cors.Options{ AllowedOrigins: []string{"https://riezebosch.github.io"}, @@ -33,18 +35,24 @@ func Routes(path string) *chi.Mux { }) }) + if wd == "" { + wd, _ = os.Getwd() + } + + repo := repo(wd) + r.Route("/api", func(r chi.Router) { - r.Get("/info", get(info, path)) - r.Get("/graph", get(graph, path)) + r.Get("/info", get(info, wd)) + r.Get("/graph", get(graph, repo)) r.Route("/refs", func(r chi.Router) { - r.Get("/{type}/{name}", get(ref, path)) - r.Get("/remotes/{remote}/{name}", get(remote, path)) - r.Get("/HEAD", get(head, path)) + r.Get("/{type}/{name}", get(ref, repo)) + r.Get("/remotes/{remote}/{name}", get(remote, repo)) + r.Get("/HEAD", get(head, repo)) }) r.Route("/objects", func(r chi.Router) { - r.Get("/blob/{id}", get(blob, path)) - r.Get("/tree/{id}", get(tree, path)) - r.Get("/commit/{id}", get(commit, path)) + r.Get("/blob/{id}", get(blob, repo)) + r.Get("/tree/{id}", get(tree, repo)) + r.Get("/commit/{id}", get(commit, repo)) }) }) @@ -56,12 +64,7 @@ type Info struct { Directory string `json:"directory"` } -func info(w http.ResponseWriter, r *http.Request, path string) { - wd, err := filepath.Abs(filepath.Join(path, "..")) - if err != nil { - panic(err) - } - +func info(w http.ResponseWriter, r *http.Request, wd string) { render.JSON(w, r, Info{Directory: filepath.Base(wd)}) } @@ -148,3 +151,11 @@ func get(f func(http.ResponseWriter, *http.Request, string), path string) func(h f(w, r, path) } } + +func repo(wd string) string { + if strings.HasSuffix(wd, ".git") != true { + return filepath.Join(wd, ".git") + } + + return wd +} diff --git a/routes_test.go b/routes_test.go index 65f461e..27ea310 100644 --- a/routes_test.go +++ b/routes_test.go @@ -10,50 +10,58 @@ import ( ) func TestInfo(t *testing.T) { - assert.Contains(t, request(t, "/api/info"), "\"gitviz\"") + assert.Contains(t, request(t, "test-repo.git", "/api/info"), "\"test-repo.git\"") +} + +func TestInfoCurrentDir(t *testing.T) { + assert.Contains(t, request(t, "", "/api/info"), "\"gitviz\"") } func TestGraph(t *testing.T) { - assert.Contains(t, request(t, "/api/graph"), "f07c09068550591ffd7efda7814ec1dfda4a0da8") + assert.Contains(t, request(t, "test-repo.git", "/api/graph"), "f07c09068550591ffd7efda7814ec1dfda4a0da8") +} + +func TestGraphCurrentDir(t *testing.T) { + assert.Contains(t, request(t, "", "/api/graph"), "\"from\":\"HEAD\"") } func TestObjectCommit(t *testing.T) { - assert.Contains(t, request(t, "/api/objects/commit/5b4ddc33ef3da7a248025cc228bc9ef7e860740a"), "author Manuel Riezebosch ") + assert.Contains(t, request(t, "test-repo.git", "/api/objects/commit/5b4ddc33ef3da7a248025cc228bc9ef7e860740a"), "author Manuel Riezebosch ") } func TestObjectBlob(t *testing.T) { - assert.Contains(t, request(t, "/api/objects/blob/f07c09068550591ffd7efda7814ec1dfda4a0da8"), "") + assert.Contains(t, request(t, "test-repo.git", "/api/objects/blob/f07c09068550591ffd7efda7814ec1dfda4a0da8"), "") } func TestRef(t *testing.T) { - assert.Contains(t, request(t, "/api/refs/heads/simple-merge"), "6de25b8c5cd0cd49dc40d91e96f8e1cc9c2d07d8") + assert.Contains(t, request(t, "test-repo.git", "/api/refs/heads/simple-merge"), "6de25b8c5cd0cd49dc40d91e96f8e1cc9c2d07d8") } func TestRefTag(t *testing.T) { - assert.Contains(t, request(t, "/api/refs/tags/R0.1"), "07870fcf1cae67fcee108e7e0bac81a4c69842d0") + assert.Contains(t, request(t, "test-repo.git", "/api/refs/tags/R0.1"), "07870fcf1cae67fcee108e7e0bac81a4c69842d0") } func TestRefRemoteTrackingBranch(t *testing.T) { - assert.NotEmpty(t, request(t, "/api/refs/remotes/github/master")) + assert.NotEmpty(t, request(t, "test-repo.git", "/api/refs/remotes/github/master")) } func TestRefHead(t *testing.T) { - assert.Contains(t, request(t, "/api/refs/HEAD"), "ref: refs/heads/master") + assert.Contains(t, request(t, "test-repo.git", "/api/refs/HEAD"), "ref: refs/heads/master") } func TestIndex(t *testing.T) { - assert.Contains(t, request(t, ""), "") + assert.Contains(t, request(t, "test-repo.git", ""), "") } func TestFavicon(t *testing.T) { - assert.NotContains(t, request(t, "/favicon.ico"), "404") + assert.NotContains(t, request(t, "test-repo.git", "/favicon.ico"), "404") } func TestObjectTree(t *testing.T) { - assert.Contains(t, request(t, "/api/objects/tree/4e84516b47b89c12f2f9bf41f34725ef6ddce099"), "Main.go") + assert.Contains(t, request(t, "test-repo.git", "/api/objects/tree/4e84516b47b89c12f2f9bf41f34725ef6ddce099"), "Main.go") } -func request(t *testing.T, path string) string { - ts := httptest.NewServer(Routes("test-repo.git")) +func request(t *testing.T, repo string, path string) string { + ts := httptest.NewServer(Routes(repo)) defer ts.Close() req, err := http.NewRequest("GET", ts.URL+path, nil)