Small task automation and simplification tool for bare-bones git hosting. https://aphrodite.dev/~notebook/projects/gitmgr.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

106 lines
2.5 KiB

package main
import (
"fmt"
"github.com/coreos/go-systemd/activation"
"net"
"os"
"strconv"
"strings"
"time"
)
type ListenMode uint8
const (
ListenModeHttp = iota
ListenModeUnix
httpTimeout = 5 * time.Second
maxDescLengthErr = "max description length invalid format: %s is not a number\n%e\n"
)
type Config struct {
Listen net.Listener
ListenMode ListenMode
GitRoot string
IsExported string
MaxDescriptionLength int
CloneUrlTemplate string
VerboseLogging bool
UrlPrefix string
Title string
PublicGitLinkTemplate string
DefaultHeadRef string
}
func getEnv(name, defaultValue string) string {
val := os.Getenv(name)
if len(val) == 0 {
return defaultValue
}
return val
}
func getMaxDescLength(defaultVal string) (int, error) {
maxDescLengthStr := getEnv("MAX_DESCRIPTION_LENGTH", defaultVal)
maxDescLength, err := strconv.Atoi(maxDescLengthStr)
if err != nil {
return 0, fmt.Errorf(maxDescLengthErr, maxDescLengthStr, err)
}
return maxDescLength, nil
}
func getListenData(defaultAddr string) (net.Listener, error) {
systemdListeners, err := activation.Listeners()
if err != nil {
return nil, err
}
var listener net.Listener
if len(systemdListeners) == 0 {
listen := getEnv("LISTEN", defaultAddr)
var listenMode ListenMode
if strings.Contains(listen, ":") &&
net.ParseIP(strings.SplitN(listen, ":", 2)[0]) != nil {
listenMode = ListenModeHttp
} else {
listenMode = ListenModeUnix
}
listener, err = net.Listen(
[]string{"tcp", "unix"}[listenMode],
listen,
)
} else {
listener = systemdListeners[0] // Doesn't support more than a listener
}
return listener, err
}
func GetConfig() (*Config, error) {
maxDescLength, err := getMaxDescLength("120")
if err != nil {
return nil, err
}
listen, err := getListenData("/run/gitmgr.sock")
if err != nil {
return nil, err
}
_, isVerbose := os.LookupEnv("VERBOSE")
return &Config{
Listen: listen,
GitRoot: getEnv("GIT_ROOT", "/srv/git"),
IsExported: getEnv("EXPORTED_FILE", "git-daemon-export-ok"),
MaxDescriptionLength: maxDescLength,
CloneUrlTemplate: getEnv("CLONE_URL_TEMPLATE", ""),
VerboseLogging: isVerbose,
UrlPrefix: getEnv("URL_PREFIX", ""),
Title: getEnv("TITLE", "gitmgr"),
PublicGitLinkTemplate: getEnv("PUBLIC_GIT_LINK_TEMPLATE", "edit?repository={{.}}"),
DefaultHeadRef: getEnv("DEFAULT_HEAD", "heads/main"),
}, nil
}