shoebill/pkg/lockfile/lockfile.go

91 lines
2.0 KiB
Go
Raw Normal View History

2023-07-20 09:26:25 +00:00
package lockfile
import (
"fmt"
"os"
"git.badhouseplants.net/allanger/shoebill/pkg/repository"
2023-07-20 09:26:25 +00:00
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
2023-08-02 15:00:34 +00:00
const LOCKFILE_NAME = "shoebill.lock.yaml"
2023-07-20 09:26:25 +00:00
type LockEntry struct {
Chart string
Release string
Version string
Namespace string
RepoUrl string
RepoName string
2023-10-12 11:23:50 +00:00
Values []string
Secrets []string
2023-07-20 09:26:25 +00:00
}
type LockRepository struct {
URL string
Name string
}
type LockFile []*LockEntry
2023-10-12 11:23:50 +00:00
// Init the LockFile object by reading the yaml file
func NewFromFile(lockfileDirPath string) (LockFile, error) {
2023-07-20 09:26:25 +00:00
var lockEntries LockFile
2023-10-12 11:23:50 +00:00
lockfilePath := fmt.Sprintf("%s/%s", lockfileDirPath, LOCKFILE_NAME)
2023-07-20 09:26:25 +00:00
logrus.Infof("reading the lockfile file: %s", lockfilePath)
2023-10-12 11:23:50 +00:00
lockFileData, err := os.ReadFile(lockfilePath)
2023-07-20 09:26:25 +00:00
if err != nil {
return nil, err
}
2023-10-12 11:23:50 +00:00
if err := yaml.Unmarshal(lockFileData, &lockEntries); err != nil {
2023-07-20 09:26:25 +00:00
return nil, err
}
2023-10-12 11:23:50 +00:00
2023-07-20 09:26:25 +00:00
return lockEntries, nil
}
func (lockfile LockFile) ReposFromLockfile() (repository.Repositories, error) {
2023-10-12 11:23:50 +00:00
repositories := repository.Repositories{}
2023-07-20 09:26:25 +00:00
for _, lockentry := range lockfile {
2023-10-12 11:23:50 +00:00
newRepoEntry := &repository.Repository{
2023-07-20 09:26:25 +00:00
URL: lockentry.RepoUrl,
Name: lockentry.RepoName,
}
2023-10-12 11:23:50 +00:00
repositories = append(repositories, newRepoEntry)
2023-07-20 09:26:25 +00:00
}
2023-10-12 11:23:50 +00:00
// Lockfile contains an entry per a release, so one repo might be met several times
2023-07-20 09:26:25 +00:00
allKeys := make(map[string]bool)
2023-10-12 11:23:50 +00:00
dedupedRepositories := repository.Repositories{}
2023-07-20 09:26:25 +00:00
2023-10-12 11:23:50 +00:00
for _, repo := range repositories {
2023-07-20 09:26:25 +00:00
if _, value := allKeys[repo.Name]; !value {
allKeys[repo.Name] = true
2023-10-12 11:23:50 +00:00
dedupedRepositories = append(dedupedRepositories, repo)
2023-07-20 09:26:25 +00:00
}
}
2023-10-12 11:23:50 +00:00
for _, repoEntry := range dedupedRepositories {
if err := repoEntry.KindFromUrl(); err != nil {
2023-07-20 09:26:25 +00:00
return nil, err
}
}
2023-10-12 11:23:50 +00:00
return dedupedRepositories, nil
2023-07-20 09:26:25 +00:00
}
func (lf LockFile) WriteToFile(dir string) error {
lockfilePath := fmt.Sprintf("%s/%s", dir, LOCKFILE_NAME)
lockfileContent, err := yaml.Marshal(lf)
if err != nil {
return err
}
if err := os.WriteFile(lockfilePath, lockfileContent, os.ModeExclusive); err != nil {
return nil
}
return nil
}