aboutsummaryrefslogtreecommitdiff
path: root/config.go
blob: 25b32fd083014e694db974f54b479ebdd75f51c0 (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main

import (
	"os"
	"strings"

	yaml "gopkg.in/yaml.v3"
)

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

// Config defines the configuration for a Sally server.
type Config struct {
	// URL is the base URL for all vanity imports.
	URL string `yaml:"url"` // required

	// Packages is a map of package name to package details.
	Packages map[string]PackageConfig `yaml:"packages"`

	// Godoc specifies where to redirect to for documentation.
	Godoc GodocConfig `yaml:"godoc"`
}

// GodocConfig is the configuration for the documentation server.
type GodocConfig struct {
	// Host is the hostname of the documentation server.
	//
	// Defaults to pkg.go.dev.
	Host string `yaml:"host"`
}

// PackageConfig is the configuration for a single Go module
// that is served by Sally.
type PackageConfig struct {
	// Repo is the URL to the Git repository for the module
	// without the https:// prefix.
	// This URL must serve the Git HTTPS protocol.
	//
	// For example, "github.com/uber-go/sally".
	Repo string `yaml:"repo"` // required

	// URL is the base URL of the vanity import for this module.
	//
	// Defaults to the URL specified in the top-level config.
	URL string `yaml:"url"`

	// VCS is the version control system of this module.
	//
	// Defaults to git.
	VCS string `yaml:"vcs"`

	// Desc is a plain text description of this module.
	Desc string `yaml:"description"`
}

// 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 values for the packages.
	for name, pkg := range c.Packages {
		if pkg.VCS == "" {
			pkg.VCS = "git"
		}

		c.Packages[name] = pkg
	}

	return &c, err
}