main
gapodo 2023-05-06 23:34:13 +02:00
parent 51de812638
commit 7d4b9549fa
9 changed files with 101 additions and 59 deletions

View File

@ -10,22 +10,26 @@ import (
"github.com/spf13/cobra"
"git.kle.li/tools/go-import-redirector/pkg/configuration"
"git.kle.li/tools/go-import-redirector/pkg/meta"
"git.kle.li/tools/go-import-redirector/pkg/server"
)
var cfgFile string
var bind string
var hotReload bool
var cfg *configuration.Configuration
var srv *server.Server
var startupDone bool
var (
cfgFile string
bind string
hotReload bool
cfg *configuration.Configuration
srv *server.Server
startupDone bool
)
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "go-import-redirector",
Short: "run a webserver serving html documents with the meta annotation for golang imports",
Long: ``,
Run: rootRun,
Use: "go-import-redirector",
Version: meta.GetVersion() + " (Commit: " + meta.GetShortCommit() + ")",
Short: "run a webserver serving html documents with the meta annotation for golang imports",
Long: ``,
Run: rootRun,
}
func Execute() {
@ -50,7 +54,7 @@ func initConfig() {
cfg = configuration.New(cfgFile, hotReload, reloadConfig)
}
func rootRun(cmd *cobra.Command, args []string) {
func rootRun(_ *cobra.Command, _ []string) {
// spew.Dump(cfg.GetPackageList())
startupDone = true
srv.Start(cfg.GetPackageList())
@ -60,7 +64,6 @@ func rootRun(cmd *cobra.Command, args []string) {
// Waiting for SIGINT (kill -2)
<-stop
}
func reloadConfig() {

10
go.mod
View File

@ -3,8 +3,12 @@ module git.kle.li/tools/go-import-redirector
go 1.20
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/fsnotify/fsnotify v1.6.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
)
require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
@ -12,10 +16,8 @@ require (
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.15.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/text v0.5.0 // indirect

8
go.sum
View File

@ -56,6 +56,7 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po=
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
@ -97,6 +98,7 @@ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
@ -128,8 +130,10 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
@ -138,9 +142,11 @@ github.com/pelletier/go-toml/v2 v2.0.6 h1:nrzqCb7j9cDFj2coyLNLaZuJTLjWjlaz6nvTvI
github.com/pelletier/go-toml/v2 v2.0.6/go.mod h1:eumQOmlWiOPt5WriQQqoM5y18pDHwha2N+QD+EUNTek=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/afero v1.9.3 h1:41FoI0fD7OR7mGcKE/aOiLkGreyf8ifIOQmJANWogMk=
github.com/spf13/afero v1.9.3/go.mod h1:iUV7ddyEEZPO5gA3zD4fJt6iStLlL+Lg4m2cihcDf8Y=
@ -163,6 +169,7 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
@ -455,6 +462,7 @@ google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpAD
google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGjtUeSXeh4=
google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=

View File

@ -8,7 +8,7 @@ import (
"os"
"git.kle.li/tools/go-import-redirector/pkg/configuration/file"
"git.kle.li/tools/go-import-redirector/pkg/packageList"
"git.kle.li/tools/go-import-redirector/pkg/packagelist"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -17,11 +17,10 @@ import (
type Configuration struct {
v *viper.Viper
r func()
packageList *packageList.PackageList
packageList *packagelist.PackageList
}
func New(cfgFile string, hotReload bool, reloadFunction func()) *Configuration {
c := Configuration{
v: viper.New(),
}
@ -65,7 +64,7 @@ func New(cfgFile string, hotReload bool, reloadFunction func()) *Configuration {
return &c
}
func (c *Configuration) updateConfig(in fsnotify.Event) {
func (c *Configuration) updateConfig(_ fsnotify.Event) {
c.loadCfgFile()
if c.r != nil {
@ -73,7 +72,7 @@ func (c *Configuration) updateConfig(in fsnotify.Event) {
}
}
func (c *Configuration) GetPackageList() *packageList.PackageList {
func (c *Configuration) GetPackageList() *packagelist.PackageList {
return c.packageList
}
@ -84,12 +83,6 @@ func (c *Configuration) loadCfgFile() {
os.Exit(1)
}
// fmt.Printf("\n\n\n" + time.Now().String() + "\n")
// fmt.Println(c.v.ConfigFileUsed())
// spew.Dump(c.v.AllSettings())
// spew.Dump(cfgFile)
// fmt.Printf("\n\n\n")
if c.v.GetString("host") == "" {
fmt.Fprintln(os.Stderr, "Host not set in config")
os.Exit(1)
@ -102,5 +95,5 @@ func (c *Configuration) loadCfgFile() {
cfgFile.UpdateCfgWithDefaults()
c.packageList = packageList.NewPackageList(cfgFile.Host, cfgFile.Modules)
c.packageList = packagelist.NewPackageList(cfgFile.Host, cfgFile.Modules)
}

View File

@ -3,6 +3,8 @@
package constants
const DefaultBranch string = "main"
const DefaultVcs string = "git"
const DefaultSourceType string = "manual"
const (
DefaultBranch string = "main"
DefaultVcs string = "git"
DefaultSourceType string = "manual"
)

26
pkg/meta/meta.go Normal file
View File

@ -0,0 +1,26 @@
// Copyright 2023 Michael Amann and contributors
// SPDX-License-Identifier: AGPL-3.0-only
package meta
import "strings"
var (
version = "dev"
commit = "none"
)
func GetVersion() string {
return strings.TrimSpace(version)
}
func GetCommit() string {
return strings.TrimSpace(commit)
}
func GetShortCommit() string {
if len(GetCommit()) <= 8 {
return GetCommit()
}
return GetCommit()[:8]
}

View File

@ -1,7 +1,7 @@
// Copyright 2023 Michael Amann and contributors
// SPDX-License-Identifier: AGPL-3.0-only
package packageList
package packagelist
import (
"fmt"
@ -16,6 +16,11 @@ type PackageList struct {
host string
}
const (
templateErrorMsg = "Failed to parse template."
templateErrorCode = 500
)
func NewPackageList(host string, modules []*file.Module) *PackageList {
pl := &PackageList{
packages: map[string]*Package{},
@ -49,7 +54,6 @@ func (pl *PackageList) addNewPackage(m *file.Module) {
}
func (p *Package) genValues(m *file.Module, host string) {
p.Import = host + m.Path + " " + m.VcsType + " " + m.Repo
if m.Title == "" {
@ -108,9 +112,8 @@ func isNonValue(s string) bool {
s = strings.TrimSpace(s)
if s == "" || s == "_" {
return true
} else {
return false
}
return false
}
func (p *Package) String() string {
@ -133,31 +136,36 @@ func (pl *PackageList) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
if pkg, ok := pl.GetMostSpecificPackage(""); ok {
indexTmpl.Execute(w, map[string]interface{}{
err := indexTmpl.Execute(w, map[string]interface{}{
"host": pl.host,
"pkgs": pl.packages,
"pkg": pkg,
})
return
} else {
indexTmpl.Execute(w, map[string]interface{}{
"host": pl.host,
"pkgs": pl.packages,
})
if err != nil {
http.Error(w, templateErrorMsg, templateErrorCode)
}
return
}
} else {
fmt.Println("Path is " + r.URL.Path)
if pkg, ok := pl.GetMostSpecificPackage(r.URL.Path); ok {
pkgTmpl.Execute(w, map[string]interface{}{
"pkg": pkg,
})
return
} else {
http.NotFound(w, r)
return
err := indexTmpl.Execute(w, map[string]interface{}{
"host": pl.host,
"pkgs": pl.packages,
})
if err != nil {
http.Error(w, templateErrorMsg, templateErrorCode)
}
return
}
fmt.Println("Path is " + r.URL.Path)
if pkg, ok := pl.GetMostSpecificPackage(r.URL.Path); ok {
err := pkgTmpl.Execute(w, map[string]interface{}{
"pkg": pkg,
})
if err != nil {
http.Error(w, templateErrorMsg, templateErrorCode)
}
return
}
http.NotFound(w, r)
}
func (pl *PackageList) GetMostSpecificPackage(path string) (*Package, bool) {

View File

@ -1,7 +1,7 @@
// Copyright 2023 Michael Amann and contributors
// SPDX-License-Identifier: AGPL-3.0-only
package packageList
package packagelist
import "html/template"

View File

@ -10,14 +10,14 @@ import (
"sync"
"time"
"git.kle.li/tools/go-import-redirector/pkg/packageList"
"git.kle.li/tools/go-import-redirector/pkg/packagelist"
)
type Server struct {
srv *http.Server
wg *sync.WaitGroup
bind string
pkgs *packageList.PackageList
pkgs *packagelist.PackageList
}
func New(bind string) *Server {
@ -28,7 +28,7 @@ func New(bind string) *Server {
return s
}
func (s *Server) startHttpServer() {
func (s *Server) startHTTPServer() {
s.srv = &http.Server{Addr: s.bind}
http.DefaultServeMux = new(http.ServeMux)
http.HandleFunc("/", s.pkgs.ServeHTTP)
@ -43,15 +43,15 @@ func (s *Server) startHttpServer() {
}()
}
func (s *Server) Restart(pkgs *packageList.PackageList) {
func (s *Server) Restart(pkgs *packagelist.PackageList) {
s.Shutdown()
s.Start(pkgs)
}
func (s *Server) Start(pkgs *packageList.PackageList) {
func (s *Server) Start(pkgs *packagelist.PackageList) {
s.pkgs = pkgs
s.wg.Add(1)
s.startHttpServer()
s.startHTTPServer()
}
func (s *Server) Shutdown() {