GitRoot

Craft your forge, Build your project, Grow your community freely
  1// SPDX-FileCopyrightText: 2025 Romain Maneschi <romain@gitroot.dev>
  2//
  3// SPDX-License-Identifier: EUPL-1.2
  4
  5package configuration
  6
  7import (
  8	"bytes"
  9	"fmt"
 10	"io/fs"
 11	"os"
 12	"path/filepath"
 13	"strings"
 14
 15	"github.com/goccy/go-yaml"
 16	"gitroot.dev/libs/golang/plugin/model"
 17)
 18
 19type Configuration struct {
 20	SshAddr          string
 21	HttpAddr         string
 22	DomainName       string
 23	ExternalSshAddr  string
 24	ExternalHttpAddr string
 25
 26	RootCommiterName   string
 27	RootRepositoryName string
 28	DefaultBranch      string
 29
 30	NbWorkerInBackground                            int
 31	NbMaxBranchesPerProjectBeforeRejectingAnonymous int
 32
 33	ExecConf ExecConf
 34
 35	pathRepositories          string
 36	pathData                  string
 37	pathDataPlugin            string
 38	pathDataWeb               string
 39	pathCache                 string
 40	repositoriesFilename      string
 41	fileUsersName             string
 42	pathConfig                string
 43	forgeConfigName           string
 44	filePluginsName           string
 45	fileRepoConfigurationName string
 46}
 47
 48func NewConfiguration(rootPath string) *Configuration {
 49	return &Configuration{
 50		SshAddr:          fmt.Sprintf("%s:%d", "0.0.0.0", 4545),
 51		HttpAddr:         fmt.Sprintf("%s:%d", "0.0.0.0", 4546),
 52		DomainName:       "localhost",
 53		ExternalSshAddr:  "ssh://localhost:4545/",
 54		ExternalHttpAddr: "http://localhost:4546/",
 55
 56		RootCommiterName:   "GitRoot",
 57		RootRepositoryName: "root",
 58		DefaultBranch:      "main",
 59
 60		NbWorkerInBackground:                            3,
 61		NbMaxBranchesPerProjectBeforeRejectingAnonymous: 10,
 62
 63		ExecConf: ExecDefaultConf,
 64
 65		pathRepositories: fmt.Sprintf("%s/repositories", rootPath),
 66		pathData:         fmt.Sprintf("%s/data", rootPath),
 67		pathDataPlugin:   fmt.Sprintf("%s/data/plugins", rootPath),
 68		pathDataWeb:      fmt.Sprintf("%s/data/web", rootPath),
 69		pathCache:        fmt.Sprintf("%s/data/cache", rootPath),
 70
 71		repositoriesFilename:      "repositories.yml",
 72		fileUsersName:             "users.yml",
 73		pathConfig:                ".gitroot",
 74		forgeConfigName:           "forgeConfig.yml",
 75		filePluginsName:           "plugins.yml",
 76		fileRepoConfigurationName: "repoConfiguration.yml",
 77	}
 78}
 79
 80func (c *Configuration) LoadFromFile(filecontent []byte) error {
 81	return yaml.Unmarshal(filecontent, c)
 82}
 83
 84func (c *Configuration) Marshall() ([]byte, error) {
 85	warning := "# /!\\ A restart of GitRoot is need before change in this file take effect. /!\\ #\n"
 86	sep := fmt.Sprintf("%s\n", strings.Repeat("#", len(warning)-1))
 87	buf := bytes.NewBufferString(sep)
 88	buf.WriteString(warning)
 89	buf.WriteString(sep)
 90	buf.WriteString("\n")
 91	conf, err := yaml.MarshalWithOptions(c, yaml.WithComment(
 92		yaml.CommentMap{
 93			"$.sshaddr":  []*yaml.Comment{yaml.LineComment("The ssh address to listen to with ip:port form")},
 94			"$.httpaddr": []*yaml.Comment{yaml.LineComment("The http address to listen to with ip:port form")},
 95		},
 96	))
 97	if err != nil {
 98		return nil, err
 99	}
100	buf.Write(conf)
101	return buf.Bytes(), nil
102}
103
104func (c *Configuration) ForgeConf() model.ForgeConf {
105	return model.ForgeConf{
106		Domain:             c.DomainName,
107		ExternalSshAddr:    c.ExternalSshAddr,
108		ExternalHttpAddr:   c.ExternalHttpAddr,
109		RootRepositoryName: c.RootRepositoryName,
110	}
111}
112
113func (c *Configuration) GetNbMaxBranchesPerProjectBeforeRejectingAnonymous() int {
114	return c.NbMaxBranchesPerProjectBeforeRejectingAnonymous
115}
116
117func (c *Configuration) GetEmail(pseudo string) string {
118	return fmt.Sprintf("%s@%s", pseudo, c.DomainName)
119}
120
121func (c *Configuration) RootCommiterPseudo() string {
122	return c.RootCommiterName
123}
124
125func (c *Configuration) IsRootCommiter(pseudo string) bool {
126	return c.RootCommiterName == pseudo
127}
128
129func (c *Configuration) ForgeConfigName() string {
130	return c.RootRepositoryName
131}
132
133func (c *Configuration) DefaultBranchName() string {
134	return c.DefaultBranch
135}
136
137func (c *Configuration) IsForgeRepo(name string) bool {
138	return c.RootRepositoryName == name
139}
140
141func (c *Configuration) PathRepositories() string {
142	return c.pathRepositories
143}
144
145func (c *Configuration) PathFileForgeConfig() string {
146	return fmt.Sprintf("%s/%s", c.pathConfig, c.forgeConfigName)
147}
148
149func (c *Configuration) PathFileUsers() string {
150	return fmt.Sprintf("%s/%s", c.pathConfig, c.fileUsersName)
151}
152
153func (c *Configuration) PathFileRepositories() string {
154	return fmt.Sprintf("%s/%s", c.pathConfig, c.repositoriesFilename)
155}
156
157func (c *Configuration) PathFilePlugins() string {
158	return fmt.Sprintf("%s/%s", c.pathConfig, c.filePluginsName)
159}
160
161func (c *Configuration) PathFileRepoConfigurationName() string {
162	return fmt.Sprintf("%s/%s", c.pathConfig, c.fileRepoConfigurationName)
163}
164
165func (c *Configuration) GetDirPathForRepo(repoName string) string {
166	return fmt.Sprintf("%s/%s", c.pathRepositories, repoName)
167}
168
169func (c *Configuration) GetDirPathData() string {
170	return c.pathData
171}
172
173func (c *Configuration) PathDataPlugin() string {
174	return c.pathDataPlugin
175}
176
177func (c *Configuration) DataWeb(repoName string) fs.FS {
178	return os.DirFS(c.PathDataWeb(repoName))
179}
180
181func (c *Configuration) PathDataWeb(repoName string) string {
182	return fmt.Sprintf("%s/%s/", c.pathDataWeb, repoName)
183}
184
185func (c *Configuration) Cache(repoName string, pluginName string) fs.FS {
186	return os.DirFS(filepath.Join(c.pathCache, repoName, pluginName))
187}
188
189func (c *Configuration) PathCacheProject(repoName string, pluginName string) string {
190	return filepath.Join(c.pathCache, repoName, pluginName)
191}
192
193func (c *Configuration) PathCache() string {
194	return c.pathCache
195}
196
197func (c *Configuration) GetDirPathDataPlugin(pluginName string) string {
198	return fmt.Sprintf("%s/%s", c.pathDataPlugin, pluginName)
199}
200
201func (c *Configuration) NbWorkerBackground() int {
202	return c.NbWorkerInBackground
203}
204
205func (c *Configuration) GetExecConf() ExecConf {
206	return c.ExecConf
207}