Goit

Simple and lightweight Git web server
git clone https://git.omkov.net/Goit
git clone [email protected]:Goit
Log | Tree | Refs | README | Download

AuthorJakob Wakeling <[email protected]>
Date2025-01-04 00:19:47
Commit5d42ac7588aaad46c0909f8c6585f752cefede52
Parent3c358ed2a85a9ee8a6c0e8b97710ca6f866fe8e1

Copy clone URLs to clipboard on click

Diffstat

M res/repo/header.html | 12 +++++++++++-
M src/goit/config.go | 16 ++++++++++++++--
M src/repo/repo.go | 15 +++++++++------

3 files changed, 34 insertions, 9 deletions

diff --git a/res/repo/header.html b/res/repo/header.html
index ca1a4cb..38de347 100644
--- a/res/repo/header.html
+++ b/res/repo/header.html
@@ -12,8 +12,11 @@
 	{{end}}
 	<tr>
 		{{if or (.Description) (.Mirror)}}<td></td>{{end}}
-		<td>git clone <a href="{{.Url}}">{{.Url}}</a></td>
+		<td>git clone <a style="cursor: copy;" onclick="copyToClipboard('{{.URL}}')">{{.URL}}</a></td>
 	</tr>
+	{{if .EnableSSH}}
+	<tr><td></td><td>git clone <a style="cursor: copy;" onclick="copyToClipboard('{{.SSH}}')">{{.SSH}}</a></td></tr>
+	{{end}}
 	<tr>
 		<td></td>
 		<td>
@@ -33,3 +36,10 @@
 		</td>
 	</tr>
 </table>
+<script>
+	function copyToClipboard(s) {
+		navigator.clipboard.writeText(s).then(() => {}, () => {
+			console.error("Failed to copy to clipboard");
+		});
+	}
+</script>
diff --git a/src/goit/config.go b/src/goit/config.go
index 9e29c64..fc1a461 100644
--- a/src/goit/config.go
+++ b/src/goit/config.go
@@ -7,7 +7,10 @@ import (
 	"encoding/json"
 	"errors"
 	"os"
+	"os/user"
 	"path/filepath"
+
+	"github.com/Jamozed/Goit/src/util"
 )
 
 type config struct {
@@ -21,6 +24,7 @@ type config struct {
 	UsesHTTPS   bool   `json:"uses_https"`
 	IPForwarded bool   `json:"ip_forwarded"`
 	EnableSSH   bool   `json:"enable_ssh"`
+	SSHUsername string `json:"ssh_username"`
 	CSRFSecret  string `json:"csrf_secret"`
 }
 
@@ -36,10 +40,18 @@ func LoadConfig() (config, error) {
 		UsesHTTPS:   false,
 		IPForwarded: false,
 		EnableSSH:   false,
+		SSHUsername: "git",
 		CSRFSecret:  "1234567890abcdef1234567890abcdef",
 	}
 
-	/* Load config file(s) */
+	/* Get the current username as the default SSH username. */
+	if osUser, err := user.Current(); err != nil {
+		util.Debugln("failed to get current user as default for SSH, falling back to \"git\":", err.Error())
+	} else {
+		conf.SSHUsername = osUser.Username
+	}
+
+	/* Load config file(s). */
 	configs := []string{
 		filepath.Join("/etc", "goit", "goit.conf"),
 	}
@@ -60,7 +72,7 @@ func LoadConfig() (config, error) {
 		}
 	}
 
-	/* Check required config values */
+	/* Check required config values. */
 	if conf.DataPath == "" {
 		return config{}, errors.New("data path unset")
 	}
diff --git a/src/repo/repo.go b/src/repo/repo.go
index a35398a..8ac9b27 100644
--- a/src/repo/repo.go
+++ b/src/repo/repo.go
@@ -5,6 +5,7 @@ package repo
 
 import (
 	"regexp"
+	"strings"
 
 	"github.com/Jamozed/Goit/src/goit"
 	"github.com/Jamozed/Goit/src/util"
@@ -13,17 +14,19 @@ import (
 )
 
 type HeaderFields struct {
-	Name, Description, Url  string
-	Readme, Licence, Mirror string
-	Editable                bool
+	Name, Description, URL, SSH string
+	Readme, Licence, Mirror     string
+	Editable, EnableSSH         bool
 }
 
 func GetHeaderFields(auth bool, user *goit.User, repo *goit.Repo, host string) HeaderFields {
 	return HeaderFields{
 		Name: repo.Name, Description: repo.Description,
-		Url:      util.If(goit.Conf.UsesHTTPS, "https://", "http://") + host + "/" + repo.Name,
-		Editable: (auth && repo.OwnerId == user.Id),
-		Mirror:   util.If(repo.IsMirror, repo.Upstream, ""),
+		URL:       util.If(goit.Conf.UsesHTTPS, "https://", "http://") + host + "/" + repo.Name,
+		Editable:  (auth && repo.OwnerId == user.Id),
+		Mirror:    util.If(repo.IsMirror, repo.Upstream, ""),
+		EnableSSH: goit.Conf.EnableSSH,
+		SSH:       goit.Conf.SSHUsername + "@" + host[:strings.LastIndex(host, ":")] + ":" + repo.Name,
 	}
 }