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