-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
79 lines (70 loc) · 2.32 KB
/
list.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
package list
import (
"context"
"fmt"
"io"
"os"
"github.com/khulnasoftproj/khulnasoft/v2/pkg/checksum"
"github.com/khulnasoftproj/khulnasoft/v2/pkg/config"
reader "github.com/khulnasoftproj/khulnasoft/v2/pkg/config-reader"
"github.com/khulnasoftproj/khulnasoft/v2/pkg/config/khulnasoft"
registry "github.com/khulnasoftproj/khulnasoft/v2/pkg/install-registry"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
)
type Controller struct {
stdout io.Writer
configFinder ConfigFinder
configReader reader.ConfigReader
registryInstaller registry.Installer
fs afero.Fs
}
func NewController(configFinder ConfigFinder, configReader reader.ConfigReader, registInstaller registry.Installer, fs afero.Fs) *Controller {
return &Controller{
stdout: os.Stdout,
configFinder: configFinder,
configReader: configReader,
registryInstaller: registInstaller,
fs: fs,
}
}
func (ctrl *Controller) List(ctx context.Context, param *config.Param, logE *logrus.Entry) error { //nolint:cyclop
cfg := &khulnasoft.Config{}
cfgFilePath, err := ctrl.configFinder.Find(param.PWD, param.ConfigFilePath, param.GlobalConfigFilePaths...)
if err != nil {
return err //nolint:wrapcheck
}
if err := ctrl.configReader.Read(cfgFilePath, cfg); err != nil {
return err //nolint:wrapcheck
}
var checksums *checksum.Checksums
if cfg.ChecksumEnabled() {
checksums = checksum.New()
checksumFilePath, err := checksum.GetChecksumFilePathFromConfigFilePath(ctrl.fs, cfgFilePath)
if err != nil {
return err //nolint:wrapcheck
}
if err := checksums.ReadFile(ctrl.fs, checksumFilePath); err != nil {
return fmt.Errorf("read a checksum JSON: %w", err)
}
defer func() {
if err := checksums.UpdateFile(ctrl.fs, checksumFilePath); err != nil {
logE.WithError(err).Error("update a checksum file")
}
}()
}
registryContents, err := ctrl.registryInstaller.InstallRegistries(ctx, logE, cfg, cfgFilePath, checksums)
if err != nil {
return err //nolint:wrapcheck
}
for registryName, registryContent := range registryContents {
for pkgName := range registryContent.PackageInfos.ToMap(logE) {
if pkgName == "" {
logE.Debug("ignore a package because the package name is empty")
continue
}
fmt.Fprintln(ctrl.stdout, registryName+","+pkgName)
}
}
return nil
}