aboutsummaryrefslogtreecommitdiff
path: root/pkg/config/config.go
blob: fd198086bb679225c6cc743d9df9eb2c332d0c53 (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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package config

import (
	"errors"
	"fmt"
	"io"
	"os"
	"path"
	"path/filepath"
	"strconv"

	"git.gabrielgio.me/cerrado/pkg/u"
	"git.sr.ht/~emersion/go-scfg"
)

var (
	ErrScanPath        = errors.New("Scan path does not exist")
	ErrRepoPath        = errors.New("Repository path does not exist")
	ErrInvalidProperty = errors.New("Invalid property")
)

type (

	// scan represents piece of the scan from the configuration file.
	scan struct {
		Path   string
		Public bool
	}

	// configuration represents file configuration.
	// fields needs to be exported to cmp to work
	configuration struct {
		Scan         *scan
		RootReadme   string
		ListenAddr   string
		Repositories []*GitRepositoryConfiguration
	}

	// This is a per repository configuration.
	GitRepositoryConfiguration struct {
		Name        string
		Path        string
		Description string
		Public      bool
		About       string
	}

	// ConfigurationRepository represents the configuration repository (as in
	// database repositories).
	// This holds all the function necessary to ask for configuration
	// information.
	ConfigurationRepository struct {
		rootReadme   string
		listenAddr   string
		repositories []*GitRepositoryConfiguration
	}
)

func LoadConfigurationRepository(configPath string) (*ConfigurationRepository, error) {
	f, err := os.Open(configPath)
	if err != nil {
		return nil, err
	}

	config, err := parse(f)
	if err != nil {
		return nil, err
	}

	repo := &ConfigurationRepository{
		rootReadme:   config.RootReadme,
		listenAddr:   config.ListenAddr,
		repositories: config.Repositories,
	}

	if config.Scan.Path != "" {
		err = repo.expandOnScanPath(config.Scan.Path, config.Scan.Public)
		if err != nil {
			return nil, err
		}
	}

	return repo, nil

}

// GetRootReadme returns root read path
func (c *ConfigurationRepository) GetRootReadme() string {
	return c.rootReadme
}

func (c *ConfigurationRepository) GetListenAddr() string {
	return c.listenAddr
}

// GetByName returns configuration of repository for a given name.
// It returns nil if there is not match for it.
func (c *ConfigurationRepository) GetByName(name string) *GitRepositoryConfiguration {
	for _, r := range c.repositories {
		if r.Name == name {
			return r
		}
	}
	return nil
}

// List returns all the configuration for all repositories.
func (c *ConfigurationRepository) List() []*GitRepositoryConfiguration {
	return c.repositories
}

// expandOnScanPath scans the scanPath for folders taking them as repositories
// and applying them default configuration.
func (c *ConfigurationRepository) expandOnScanPath(scanPath string, public bool) error {
	if !u.FileExist(scanPath) {
		return ErrScanPath
	}

	entries, err := os.ReadDir(scanPath)
	if err != nil {
		return err
	}

	for _, e := range entries {
		if !e.IsDir() {
			continue
		}

		fullPath := path.Join(scanPath, e.Name())
		if !c.repoExits(fullPath) {
			c.repositories = append(c.repositories, &GitRepositoryConfiguration{
				Name:   e.Name(),
				Path:   fullPath,
				Public: public,
			})
		}
	}
	return nil
}

func (c *ConfigurationRepository) repoExits(path string) bool {
	for _, r := range c.repositories {
		if path == r.Path {
			return true
		}
	}
	return false
}

func parse(r io.Reader) (*configuration, error) {
	block, err := scfg.Read(r)
	if err != nil {
		return nil, err
	}

	config := defaultConfiguration()

	err = setScan(block, config.Scan)
	if err != nil {
		return nil, err
	}

	err = setRootReadme(block, &config.RootReadme)
	if err != nil {
		return nil, err
	}

	err = setListenAddr(block, &config.ListenAddr)
	if err != nil {
		return nil, err
	}

	err = setRepositories(block, &config.Repositories)
	if err != nil {
		return nil, err
	}

	return config, nil
}

func setRepositories(block scfg.Block, repositories *[]*GitRepositoryConfiguration) error {
	blocks := block.GetAll("repository")

	for _, r := range blocks {
		if len(r.Params) != 1 {
			return fmt.Errorf(
				"Invlid number of params for repository: %w",
				ErrInvalidProperty,
			)
		}

		path := u.FirstOrZero(r.Params)
		repository := defaultRepisotryConfiguration(path)

		for _, d := range r.Children {
			// under repository there is only single param properties
			if len(d.Params) != 1 {
				return fmt.Errorf(
					"Invlid number of params for %s: %w",
					d.Name,
					ErrInvalidProperty,
				)
			}

			switch d.Name {
			case "name":
				if err := setString(d, &repository.Name); err != nil {
					return err
				}
			case "description":
				if err := setString(d, &repository.Description); err != nil {
					return err
				}
			case "public":
				if err := setBool(d, &repository.Public); err != nil {
					return err
				}
			case "about":
				if err := setString(d, &repository.About); err != nil {
					return err
				}
			}
		}

		*repositories = append(*repositories, repository)
	}

	return nil
}

func defaultConfiguration() *configuration {
	return &configuration{
		Scan:         defaultScan(),
		RootReadme:   "",
		ListenAddr:   defaultAddr(),
		Repositories: make([]*GitRepositoryConfiguration, 0),
	}
}

func defaultScan() *scan {
	return &scan{
		Public: false,
		Path:   "",
	}
}

func defaultAddr() string {
	return "tcp://localhost:8080"
}

func defaultRepisotryConfiguration(path string) *GitRepositoryConfiguration {
	return &GitRepositoryConfiguration{
		Path:        path,
		Name:        filepath.Base(path),
		Description: "",
		Public:      false,
		About:       "README.md",
	}
}

func setRootReadme(block scfg.Block, readme *string) error {
	scanDir := block.Get("root-readme")
	return setString(scanDir, readme)
}

func setListenAddr(block scfg.Block, listenAddr *string) error {
	scanDir := block.Get("listen-addr")
	return setString(scanDir, listenAddr)
}

func setScan(block scfg.Block, scan *scan) error {
	scanDir := block.Get("scan")
	if scanDir == nil {
		return nil
	}
	err := setString(scanDir, &scan.Path)
	if err != nil {
		return err
	}

	public := scanDir.Children.Get("public")
	return setBool(public, &scan.Public)
}

func setBool(dir *scfg.Directive, field *bool) error {
	if dir != nil {

		p1, _ := u.First(dir.Params)
		v, err := strconv.ParseBool(p1)
		if err != nil {
			return fmt.Errorf("Error parsing bool param of %s: %w", dir.Name, err)
		}
		*field = v
	}
	return nil
}

func setString(dir *scfg.Directive, field *string) error {
	if dir != nil {
		*field = u.FirstOrZero(dir.Params)
	}
	return nil
}