Author | Jakob Wakeling <[email protected]> |
Date | 2023-09-07 09:47:49 |
Commit | edde49188189df2433f5b5562c2bec5c96bbe736 |
Parent | c026530dc796eb0979ecc4e576ad718fe978402e |
Add README and LICENCE links to the edit page
Diffstat
A | Makefile | | | 14 | ++++++++++++++ |
M | res/res.go | | | 2 | ++ |
M | src/repo/edit.go | | | 28 | ++++++++++++++++++++++++++++ |
3 files changed, 44 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0f0fa25 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +.PHONY: all build test help +all: help + +PROGRAM = "goit" +VERSION = "0.0.0" + +build: ## Build the project + @go build -ldflags "-X res.Version=$(VERSION)" -o ./bin/$(PROGRAM) . + +test: ## Run unit tests + @go test ./... + +help: ## Display help information + @grep -E '^[a-zA-Z_-]+:.*?##.*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## *"}; {printf "\033[36m%-6s\033[0m %s\n", $$1, $$2}' diff --git a/res/res.go b/res/res.go index fbf0647..e5dfd89 100644 --- a/res/res.go +++ b/res/res.go @@ -2,6 +2,8 @@ package res import _ "embed" +var Version string = "dev" + //go:embed error.html var Error string diff --git a/src/repo/edit.go b/src/repo/edit.go index 3ced0b8..7f49c15 100644 --- a/src/repo/edit.go +++ b/src/repo/edit.go @@ -1,13 +1,17 @@ package repo import ( + "errors" "fmt" "log" "net/http" + "path" "slices" goit "github.com/Jamozed/Goit/src" "github.com/Jamozed/Goit/src/util" + "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing" "github.com/gorilla/mux" ) @@ -58,6 +62,30 @@ func HandleEdit(w http.ResponseWriter, r *http.Request) { data.Form.Description = repo.Description data.Form.IsPrivate = repo.IsPrivate + gr, err := git.PlainOpen(goit.RepoPath(repo.Name)) + if err != nil { + log.Println("[/repo/file]", err.Error()) + goit.HttpError(w, http.StatusInternalServerError) + return + } + + ref, err := gr.Head() + if errors.Is(err, plumbing.ErrReferenceNotFound) { + goit.HttpError(w, http.StatusNotFound) + return + } else if err != nil { + log.Println("[/repo/file]", err.Error()) + goit.HttpError(w, http.StatusInternalServerError) + return + } + + if readme, _ := findReadme(gr, ref); readme != "" { + data.Readme = path.Join("/", repo.Name, "file", readme) + } + if licence, _ := findLicence(gr, ref); licence != "" { + data.Licence = path.Join("/", repo.Name, "file", licence) + } + if r.Method == http.MethodPost { data.Form.Name = r.FormValue("reponame") data.Form.Description = r.FormValue("description")