GitRoot

craft your forge, build your project, grow your community freely
 1package configuration
 2
 3import (
 4	"errors"
 5	"io/fs"
 6	"os"
 7	"path/filepath"
 8
 9	"github.com/goccy/go-yaml"
10)
11
12type RootConfiguration struct {
13	RootRepositoryName string
14	Version            string
15}
16
17func (c *Configuration) LoadFromOrStoreToRootConf(path string, version string) error {
18	rc := &RootConfiguration{
19		RootRepositoryName: c.RootRepositoryName,
20		Version:            version,
21	}
22	content, err := os.ReadFile(path)
23	if errors.Is(err, os.ErrNotExist) {
24		if c, err := yaml.Marshal(rc); err != nil {
25			return err
26		} else {
27			if err := os.MkdirAll(filepath.Dir(path), fs.ModePerm); err != nil {
28				return err
29			}
30			return os.WriteFile(path, c, fs.ModePerm)
31		}
32	} else if err != nil {
33		return err
34	}
35
36	if err := yaml.Unmarshal(content, rc); err != nil {
37		return err
38	}
39	c.RootRepositoryName = rc.RootRepositoryName
40	if version != rc.Version {
41		// TODO manage update
42	}
43	return nil
44}