summaryrefslogtreecommitdiff
path: root/cgitrc/config.go
blob: 82b5f69b046137b7e9bc4ccc23ac128bebbc3b34 (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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package cgitrc

import (
	"errors"
	"fmt"
	"io"
	"os"
	"path"
	"strings"
)

// Config is a cgit configuration file.
type Config struct {
	CloneURL string
	Repos    map[string]*Repo

	files map[string]bool
}

// Repo describes a cgit repository configuration.
type Repo struct {
	URL      string
	Desc     string
	CloneURL string
}

// Open reads a config file.
func Open(filename string) (*Config, error) {
	cfg := &Config{
		Repos: make(map[string]*Repo),
		files: make(map[string]bool, 1),
	}
	if err := cfg.fromFile(filename); err != nil {
		return nil, err
	}
	return cfg, nil
}

// ResolveRepoCloneURL returns the effective clone URL for a repository.
// Returns an empty string if no clone URL can be determined.
func (cfg *Config) ResolveRepoCloneURL(r *Repo) string {
	if r.CloneURL != "" {
		return cfg.CloneURL
	}
	if cfg.CloneURL != "" {
		return strings.Replace(cfg.CloneURL, "$CGIT_REPO_URL", r.URL, 1)
	}
	return ""
}

func (cfg *Config) fromFile(filename string) error {
	filename = path.Clean(filename)
	if _, ok := cfg.files[filename]; ok {
		return errors.New("recursive include")
	}
	file, err := os.Open(filename)
	if err != nil {
		return err
	}
	defer file.Close()

	parser := &Parser{}
	if _, err := io.Copy(parser, file); err != nil {
		return err
	}
	if err := parser.Close(); err != nil {
		return err
	}
	cfg.files[filename] = true
	return cfg.fromFields(parser.Fields)
}

func (cfg *Config) fromFields(fields []*ParserField) error {
	var (
		repo    *Repo
		newRepo bool
	)
	for _, f := range fields {
		switch f.Keys[0] {
		case "include":
			if len(f.Keys) != 1 {
				return errors.New("invalid key")
			}
			if err := cfg.fromFile(f.Value); err != nil {
				return wrapFieldErr(f, fmt.Errorf("include %s: %v", f.Value, err))
			}
		case "repo":
			if len(f.Keys) == 2 && f.Keys[1] == "url" {
				repo = &Repo{}
				newRepo = true
			}
			if repo == nil {
				return wrapFieldErr(f, errors.New("unexpected key"))
			}
			if err := repo.setField(f.Keys[1:], f.Value); err != nil {
				return wrapFieldErr(f, err)
			}
			if newRepo {
				cfg.Repos[repo.URL] = repo
				newRepo = false
			}
		default:
			if err := cfg.setField(f.Keys, f.Value); err != nil {
				return wrapFieldErr(f, err)
			}
		}
	}
	return nil
}

func (cfg *Config) setField(keys []string, v string) error {
	if len(keys) != 1 {
		return errors.New("invalid key")
	}
	switch keys[0] {
	case "clone-url":
		cfg.CloneURL = v
	}
	return nil
}

func (r *Repo) setField(keys []string, v string) error {
	if len(keys) != 1 {
		return errors.New("invalid key")
	}
	switch keys[0] {
	case "url":
		r.URL = v
	case "desc":
		r.Desc = v
	case "clone-url":
		r.CloneURL = v
	}
	return nil
}

func wrapFieldErr(f *ParserField, err error) error {
	return fmt.Errorf("on line %d: %v", f.LineNo, err)
}