-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.go
115 lines (100 loc) · 3.03 KB
/
update.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package workflow
import (
"fmt"
"path/filepath"
"github.com/ava-labs/avalanchego/database"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/spf13/afero"
"github.com/ava-labs/apm/git"
"github.com/ava-labs/apm/storage"
"github.com/ava-labs/apm/util"
)
var _ Workflow = &Update{}
type UpdateConfig struct {
Executor Executor
Registry storage.Storage[storage.RepoList]
InstalledVMs storage.Storage[storage.InstallInfo]
SourcesList storage.Storage[storage.SourceInfo]
DB database.Database
TmpPath string
PluginPath string
Installer Installer
RepositoriesPath string
Auth http.BasicAuth
GitFactory git.Factory
RepoFactory storage.RepositoryFactory
Fs afero.Fs
}
func NewUpdate(config UpdateConfig) *Update {
return &Update{
executor: config.Executor,
registry: config.Registry,
installedVMs: config.InstalledVMs,
db: config.DB,
tmpPath: config.TmpPath,
pluginPath: config.PluginPath,
installer: config.Installer,
sourcesList: config.SourcesList,
repositoriesPath: config.RepositoriesPath,
auth: config.Auth,
gitFactory: config.GitFactory,
repoFactory: config.RepoFactory,
fs: config.Fs,
}
}
type Update struct {
executor Executor
db database.Database
registry storage.Storage[storage.RepoList]
installedVMs storage.Storage[storage.InstallInfo]
sourcesList storage.Storage[storage.SourceInfo]
installer Installer
auth http.BasicAuth
tmpPath string
pluginPath string
repositoriesPath string
gitFactory git.Factory
repoFactory storage.RepositoryFactory
fs afero.Fs
}
func (u Update) Execute() error {
itr := u.sourcesList.Iterator()
defer itr.Release()
for itr.Next() {
aliasBytes := itr.Key()
alias := string(aliasBytes)
organization, repo := util.ParseAlias(alias)
sourceInfo, err := itr.Value()
if err != nil {
return err
}
previousCommit := sourceInfo.Commit
repositoryPath := filepath.Join(u.repositoriesPath, organization, repo)
latestCommit, err := u.gitFactory.GetRepository(sourceInfo.URL, repositoryPath, sourceInfo.Branch, &u.auth)
if err != nil {
return err
}
if latestCommit == previousCommit {
fmt.Printf("Already at latest for %s@%s.\n", alias, latestCommit)
continue
}
workflow := NewUpdateRepository(UpdateRepositoryConfig{
RepoName: repo,
RepositoryPath: repositoryPath,
AliasBytes: aliasBytes,
PreviousCommit: previousCommit,
LatestCommit: latestCommit,
Repository: u.repoFactory.GetRepository(aliasBytes),
Registry: u.registry,
SourceInfo: sourceInfo,
SourcesList: u.sourcesList,
Fs: u.fs,
})
if err := u.executor.Execute(workflow); err != nil {
return err
}
}
return nil
}