shoebill/pkg/cluster/cluster.go

90 lines
2.0 KiB
Go
Raw Normal View History

2023-07-20 09:26:25 +00:00
package cluster
import (
"errors"
"fmt"
"os"
2023-08-02 15:00:34 +00:00
"git.badhouseplants.net/allanger/shoebill/internal/utils/githelper"
"git.badhouseplants.net/allanger/shoebill/pkg/lockfile"
"git.badhouseplants.net/allanger/shoebill/pkg/release"
2023-07-20 09:26:25 +00:00
)
type Cluster struct {
// Public
Name string
Git string
Releases []string
Provider string
2023-10-11 12:14:20 +00:00
DotSops string
2023-07-20 09:26:25 +00:00
// Internal
ReleasesObj release.Releases `yaml:"-"`
}
type Clusters []*Cluster
func (c *Cluster) CloneRepo(gh githelper.Githelper, workdir string, dry bool) error {
return gh.CloneRepo(workdir, c.Git, dry)
}
func (c *Cluster) BootstrapRepo(gh githelper.Githelper, workdir string, dry bool) error {
// - Create an empty lockfile
lockfilePath := fmt.Sprintf("%s/%s", workdir, lockfile.LOCKFILE_NAME)
if _, err := os.Stat(lockfilePath); errors.Is(err, os.ErrNotExist) {
file, err := os.Create(lockfilePath)
if err != nil {
return err
}
if _, err := file.WriteString("[]"); err != nil {
return err
}
srcDir := fmt.Sprintf("%s/src", workdir)
if err := os.MkdirAll(srcDir, 0777); err != nil {
return err
}
_, err = os.Create(fmt.Sprintf("%s/.gitkeep", srcDir))
if err != nil {
return err
}
2023-08-02 15:00:34 +00:00
if err := gh.AddAllAndCommit(workdir, "Bootstrap the shoebill repo"); err != nil {
2023-07-20 09:26:25 +00:00
return err
}
if !dry {
if err := gh.Push(workdir); err != nil {
return err
}
}
2023-10-11 12:14:20 +00:00
}
if len(c.DotSops) > 0 {
dotsopsPath := fmt.Sprintf("%s/.sops.yaml", workdir)
if _, err := os.Stat(dotsopsPath); errors.Is(err, os.ErrNotExist) {
file, err := os.Create(dotsopsPath)
if err != nil {
return err
}
if _, err := file.WriteString(c.DotSops); err != nil {
return err
}
if err := gh.AddAllAndCommit(workdir, "Create a sops config file"); err != nil {
return err
}
if !dry {
if err := gh.Push(workdir); err != nil {
return err
}
}
}
2023-07-20 09:26:25 +00:00
}
return nil
}
func (c *Cluster) PopulateReleases(releases release.Releases) {
c.ReleasesObj = releases
}
func (c *Cluster) CreateNewLockfile() error {
return nil
}