2023-07-20 09:26:25 +00:00
|
|
|
package diff
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
|
2023-08-02 15:00:34 +00:00
|
|
|
"git.badhouseplants.net/allanger/shoebill/internal/config/release"
|
|
|
|
"git.badhouseplants.net/allanger/shoebill/internal/config/repository"
|
|
|
|
"git.badhouseplants.net/allanger/shoebill/internal/lockfile"
|
2023-07-20 09:26:25 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Diff struct {
|
|
|
|
AddedReleases release.Releases
|
|
|
|
DeletedReleases release.Releases
|
|
|
|
UpdatedReleases release.Releases
|
|
|
|
PreservedReleases release.Releases
|
|
|
|
AddedRepositories repository.Repositories
|
|
|
|
DeletedRepositories repository.Repositories
|
|
|
|
UpdatedRepositories repository.Repositories
|
|
|
|
PreservedRepositories repository.Repositories
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
ACTION_ADD = "add"
|
|
|
|
ACTION_UPDATE = "update"
|
|
|
|
ACTION_DELETE = "delete"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO(@allanger): Naming should be better
|
|
|
|
func DiffReleases(src, dest release.Releases) (*Diff, error) {
|
|
|
|
diff := &Diff{}
|
|
|
|
|
|
|
|
for _, rSrc := range src {
|
|
|
|
found := false
|
|
|
|
for _, rDest := range dest {
|
|
|
|
if rSrc.Release == rDest.Release {
|
|
|
|
found = true
|
|
|
|
if reflect.DeepEqual(rSrc, rDest) {
|
|
|
|
diff.PreservedReleases = append(diff.PreservedReleases, rSrc)
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if err := rDest.RepositoryObj.KindFromUrl(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
diff.UpdatedReleases = append(diff.UpdatedReleases, rDest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
// for i, entry := range lockFile {
|
|
|
|
// if entry.Release == rSrc.Release {
|
|
|
|
// lockFile[i] = lockFile[len(lockFile)-1]
|
|
|
|
// lockFile = lockFile[:len(lockFile)-1]
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
diff.DeletedReleases = append(diff.DeletedReleases, rSrc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, rDest := range dest {
|
|
|
|
found := false
|
|
|
|
for _, rSrc := range src {
|
|
|
|
if rSrc.Release == rDest.Release {
|
|
|
|
found = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
if err := rDest.RepositoryObj.KindFromUrl(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
diff.AddedReleases = append(diff.AddedReleases, rDest)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return diff, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (diff *Diff) Resolve(repositories repository.Repositories, path string) (lockfile.LockFile, error) {
|
|
|
|
lockfile := lockfile.LockFile{}
|
|
|
|
reposWished := repository.Repositories{}
|
|
|
|
for _, p := range diff.PreservedReleases {
|
|
|
|
lockfile = append(lockfile, p.LockEntry())
|
|
|
|
reposWished = append(reposWished, p.RepositoryObj)
|
|
|
|
}
|
|
|
|
for _, a := range diff.AddedReleases {
|
|
|
|
logrus.Infof("adding %s", a.Release)
|
|
|
|
lockfile = append(lockfile, a.LockEntry())
|
|
|
|
reposWished = append(reposWished, a.RepositoryObj)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, u := range diff.UpdatedReleases {
|
|
|
|
logrus.Infof("updating %s", u.Release)
|
|
|
|
lockfile = append(lockfile, u.LockEntry())
|
|
|
|
reposWished = append(reposWished, u.RepositoryObj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repo Wished is the list of all repos that are required by the current setup, we need to
|
|
|
|
|
|
|
|
// Existing repos are all the repos in the lockfile
|
|
|
|
for _, repoExisting := range repositories {
|
|
|
|
found := false
|
|
|
|
i := 0
|
|
|
|
for _, repoWished := range reposWished {
|
|
|
|
// If there is the same repo in the wished repos and in the lockfile
|
|
|
|
// We need either to udpate, or preserve. If it can't be found, just remove
|
|
|
|
// from the reposWished slice
|
|
|
|
if repoWished.Name == repoExisting.Name {
|
|
|
|
// If !found, should be gone from the repo
|
|
|
|
found = true
|
|
|
|
if err := repoWished.ValidateURL(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := repoWished.KindFromUrl(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(reposWished, repoExisting) {
|
|
|
|
diff.UpdatedRepositories = append(diff.UpdatedRepositories, repoWished)
|
|
|
|
} else {
|
|
|
|
diff.PreservedRepositories = append(diff.PreservedRepositories, repoWished)
|
|
|
|
}
|
|
|
|
// Delete the
|
|
|
|
} else {
|
|
|
|
reposWished[i] = repoWished
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reposWished = reposWished[:i]
|
|
|
|
if !found {
|
|
|
|
diff.DeletedRepositories = append(diff.DeletedRepositories, repoExisting)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
diff.AddedRepositories = append(diff.AddedRepositories, reposWished...)
|
|
|
|
|
|
|
|
return lockfile, nil
|
|
|
|
}
|