Changes
3 changed files (+81/-26)
-
-
@@ -42,6 +42,7 @@ dirs:meta: # Website's name. # Default: "Repositories" title: legit # Website's description.
-
@@ -91,7 +92,9 @@ server:name: github.com/pocka # A host/hostname to bind web server to. # Default: "localhost" host: 0.0.0.0 # TCP port legit's web server listens to. # Default: 5555 port: 5555
-
-
-
@@ -2,6 +2,7 @@ package configimport ( "fmt" "math" "os" "path/filepath"
-
@@ -39,48 +40,90 @@ type Config struct {Server struct { Name string `yaml:"name,omitempty"` Host string `yaml:"host"` Port int `yaml:"port"` Port uint `yaml:"port"` } `yaml:"server"` filepath string } func Read(f string) (*Config, error) { b, err := os.ReadFile(f) func NewWithDefaults() *Config { c := Config{} c.Repo.MainBranch = []string{"trunk", "master", "main"} c.Repo.Readme = []string{ "README", "README.txt", "README.md", "README.adoc", "readme", "readme.txt", "readme.md", "readme.adoc", } c.Meta.Title = "Repositories" return &c } func (c *Config) ReadFromFile(path string) error { b, err := os.ReadFile(path) if err != nil { return nil, fmt.Errorf("reading config: %w", err) return fmt.Errorf("Read error (%s): %w", path, err) } c := Config{} if err := yaml.Unmarshal(b, &c); err != nil { return nil, fmt.Errorf("parsing config: %w", err) return fmt.Errorf("Parsing error: %w", err) } c.filepath = path return nil } func (c *Config) Resolve(cwd string) error { var err error basePath := cwd if c.filepath != "" { basePath = filepath.Dir(c.filepath) } if c.Repo.ScanPath, err = resolvePath(c.Repo.ScanPath, f); err != nil { return nil, err if c.Repo.ScanPath == "" { c.Repo.ScanPath = cwd } else if c.Repo.ScanPath, err = resolvePath(c.Repo.ScanPath, basePath); err != nil { return err } // Override templates dir if c.Dirs.Templates != "" { if c.Dirs.Templates, err = resolvePath(c.Dirs.Templates, f); err != nil { return nil, err if c.Dirs.Templates, err = resolvePath(c.Dirs.Templates, basePath); err != nil { return err } } // Override static dir if c.Dirs.Static != "" { if c.Dirs.Static, err = resolvePath(c.Dirs.Static, f); err != nil { return nil, err if c.Dirs.Static, err = resolvePath(c.Dirs.Static, basePath); err != nil { return err } } return &c, nil if c.UI.CommitsPageSize == 0 { c.UI.CommitsPageSize = 30 } if c.Server.Host == "" { c.Server.Host = "localhost" } if c.Server.Port == 0 { c.Server.Port = 5555 } else if c.Server.Port > math.MaxUint16 { return fmt.Errorf("server.port should be in 0 < x <= %d range", math.MaxUint16) } return nil } func resolvePath(target string, configPath string) (string, error) { func resolvePath(target string, basePath string) (string, error) { if filepath.IsAbs(target) { return target, nil } dir := filepath.Dir(configPath) return filepath.Abs(filepath.Join(dir, target)) return filepath.Abs(filepath.Join(basePath, target)) }
-
-
-
@@ -5,7 +5,6 @@ import ("fmt" "io/fs" "log" "math" "net/http" "os"
-
@@ -18,30 +17,40 @@ func main() {var cfg string var host string var port uint flag.StringVar(&cfg, "config", "./config.yaml", "path to config file") var scanPath string flag.StringVar(&cfg, "config", "", "path to config file") flag.StringVar(&host, "server.host", "", "override server.host config") flag.UintVar(&port, "server.port", 0, "override server.port config") flag.StringVar(&scanPath, "repo.scanPath", "", "override repo.scanPath config") flag.Parse() c, err := config.Read(cfg) cwd, err := os.Getwd() if err != nil { log.Fatal(err) } if port > 0 { if port > math.MaxUint16 { log.Fatalf("server.port should be in 0 < x <= %d range", math.MaxUint16) c := config.NewWithDefaults() if cfg != "" { if err := c.ReadFromFile(cfg); err != nil { log.Fatalf("Unable to read config file: %s", err) } } c.Server.Port = int(port) if port > 0 { c.Server.Port = port } if host != "" { c.Server.Host = host } if c.UI.CommitsPageSize == 0 { c.UI.CommitsPageSize = 30 if scanPath != "" { c.Repo.ScanPath = scanPath } if err := c.Resolve(cwd); err != nil { log.Fatal(err) } allowedDirs := make([]string, 1, 3)
-