aboutsummaryrefslogtreecommitdiff
path: root/config.go
blob: 57b5dcdeaf0c573df6b48a314d28cd2b4e73200b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main

import (
	"os"
	"strings"

	yaml "gopkg.in/yaml.v2"
)

const (
	_defaultGodocServer = "pkg.go.dev"
	_defaultBranch      = "master"
)

// Config represents the structure of the yaml file
type Config struct {
	URL      string             `yaml:"url"`
	Packages map[string]Package `yaml:"packages"`
	Godoc    struct {
		Host string `yaml:"host"`
	} `yaml:"godoc"`
}

// Package details the options available for each repo
type Package struct {
	Repo   string `yaml:"repo"`
	Branch string `yaml:"branch"`
	URL    string `yaml:"url"`

	Desc string `yaml:"description"` // plain text only
}

// Parse takes a path to a yaml file and produces a parsed Config
func Parse(path string) (*Config, error) {
	var c Config

	data, err := os.ReadFile(path)
	if err != nil {
		return nil, err
	}

	if err := yaml.Unmarshal(data, &c); err != nil {
		return nil, err
	}

	if c.Godoc.Host == "" {
		c.Godoc.Host = _defaultGodocServer
	} else {
		host := c.Godoc.Host
		host = strings.TrimPrefix(host, "https://")
		host = strings.TrimPrefix(host, "http://")
		host = strings.TrimSuffix(host, "/")
		c.Godoc.Host = host
	}

	// set default branch
	for v, p := range c.Packages {
		if p.Branch == "" {
			p.Branch = _defaultBranch
			c.Packages[v] = p
		}
	}

	return &c, err
}