Skip to content

Commit 6f762c7

Browse files
committed
storage: change methods to value receiver
Given: - None of the methods of the `Storage` are mutating the storage itself. - It must be instantiated to be usable, as there is a strict reliance on values. - The struct itself is light. This seems to be more fitting. Signed-off-by: Hidde Beydals <[email protected]>
1 parent 3c87ad6 commit 6f762c7

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

internal/controller/storage.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func NewStorage(basePath string, hostname string, artifactRetentionTTL time.Dura
8585
}
8686

8787
// NewArtifactFor returns a new v1.Artifact.
88-
func (s *Storage) NewArtifactFor(kind string, metadata metav1.Object, revision, fileName string) v1.Artifact {
88+
func (s Storage) NewArtifactFor(kind string, metadata metav1.Object, revision, fileName string) v1.Artifact {
8989
path := v1.ArtifactPath(kind, metadata.GetNamespace(), metadata.GetName(), fileName)
9090
artifact := v1.Artifact{
9191
Path: path,
@@ -118,18 +118,18 @@ func (s Storage) SetHostname(URL string) string {
118118
}
119119

120120
// MkdirAll calls os.MkdirAll for the given v1.Artifact base dir.
121-
func (s *Storage) MkdirAll(artifact v1.Artifact) error {
121+
func (s Storage) MkdirAll(artifact v1.Artifact) error {
122122
dir := filepath.Dir(s.LocalPath(artifact))
123123
return os.MkdirAll(dir, 0o700)
124124
}
125125

126126
// Remove calls os.Remove for the given v1.Artifact path.
127-
func (s *Storage) Remove(artifact v1.Artifact) error {
127+
func (s Storage) Remove(artifact v1.Artifact) error {
128128
return os.Remove(s.LocalPath(artifact))
129129
}
130130

131131
// RemoveAll calls os.RemoveAll for the given v1.Artifact base dir.
132-
func (s *Storage) RemoveAll(artifact v1.Artifact) (string, error) {
132+
func (s Storage) RemoveAll(artifact v1.Artifact) (string, error) {
133133
var deletedDir string
134134
dir := filepath.Dir(s.LocalPath(artifact))
135135
// Check if the dir exists.
@@ -141,7 +141,7 @@ func (s *Storage) RemoveAll(artifact v1.Artifact) (string, error) {
141141
}
142142

143143
// RemoveAllButCurrent removes all files for the given v1.Artifact base dir, excluding the current one.
144-
func (s *Storage) RemoveAllButCurrent(artifact v1.Artifact) ([]string, error) {
144+
func (s Storage) RemoveAllButCurrent(artifact v1.Artifact) ([]string, error) {
145145
deletedFiles := []string{}
146146
localPath := s.LocalPath(artifact)
147147
dir := filepath.Dir(localPath)
@@ -174,7 +174,7 @@ func (s *Storage) RemoveAllButCurrent(artifact v1.Artifact) ([]string, error) {
174174
// 1. collect all artifact files with an expired ttl
175175
// 2. if we satisfy maxItemsToBeRetained, then return
176176
// 3. else, collect all artifact files till the latest n files remain, where n=maxItemsToBeRetained
177-
func (s *Storage) getGarbageFiles(artifact v1.Artifact, totalCountLimit, maxItemsToBeRetained int, ttl time.Duration) (garbageFiles []string, _ error) {
177+
func (s Storage) getGarbageFiles(artifact v1.Artifact, totalCountLimit, maxItemsToBeRetained int, ttl time.Duration) (garbageFiles []string, _ error) {
178178
localPath := s.LocalPath(artifact)
179179
dir := filepath.Dir(localPath)
180180
artifactFilesWithCreatedTs := make(map[time.Time]string)
@@ -261,7 +261,7 @@ func (s *Storage) getGarbageFiles(artifact v1.Artifact, totalCountLimit, maxItem
261261

262262
// GarbageCollect removes all garbage files in the artifact dir according to the provided
263263
// retention options.
264-
func (s *Storage) GarbageCollect(ctx context.Context, artifact v1.Artifact, timeout time.Duration) ([]string, error) {
264+
func (s Storage) GarbageCollect(ctx context.Context, artifact v1.Artifact, timeout time.Duration) ([]string, error) {
265265
delFilesChan := make(chan []string)
266266
errChan := make(chan error)
267267
// Abort if it takes more than the provided timeout duration.
@@ -323,7 +323,7 @@ func stringInSlice(a string, list []string) bool {
323323
}
324324

325325
// ArtifactExist returns a boolean indicating whether the v1.Artifact exists in storage and is a regular file.
326-
func (s *Storage) ArtifactExist(artifact v1.Artifact) bool {
326+
func (s Storage) ArtifactExist(artifact v1.Artifact) bool {
327327
fi, err := os.Lstat(s.LocalPath(artifact))
328328
if err != nil {
329329
return false
@@ -334,7 +334,7 @@ func (s *Storage) ArtifactExist(artifact v1.Artifact) bool {
334334
// VerifyArtifact verifies if the Digest of the v1.Artifact matches the digest
335335
// of the file in Storage. It returns an error if the digests don't match, or
336336
// if it can't be verified.
337-
func (s *Storage) VerifyArtifact(artifact v1.Artifact) error {
337+
func (s Storage) VerifyArtifact(artifact v1.Artifact) error {
338338
if artifact.Digest == "" {
339339
return fmt.Errorf("artifact has no digest")
340340
}
@@ -382,7 +382,7 @@ func SourceIgnoreFilter(ps []gitignore.Pattern, domain []string) ArchiveFileFilt
382382
// directories and any ArchiveFileFilter matches. While archiving, any environment specific data (for example,
383383
// the user and group name) is stripped from file headers.
384384
// If successful, it sets the digest and last update time on the artifact.
385-
func (s *Storage) Archive(artifact *v1.Artifact, dir string, filter ArchiveFileFilter) (err error) {
385+
func (s Storage) Archive(artifact *v1.Artifact, dir string, filter ArchiveFileFilter) (err error) {
386386
if f, err := os.Stat(dir); os.IsNotExist(err) || !f.IsDir() {
387387
return fmt.Errorf("invalid dir path: %s", dir)
388388
}
@@ -504,7 +504,7 @@ func (s *Storage) Archive(artifact *v1.Artifact, dir string, filter ArchiveFileF
504504

505505
// AtomicWriteFile atomically writes the io.Reader contents to the v1.Artifact path.
506506
// If successful, it sets the digest and last update time on the artifact.
507-
func (s *Storage) AtomicWriteFile(artifact *v1.Artifact, reader io.Reader, mode os.FileMode) (err error) {
507+
func (s Storage) AtomicWriteFile(artifact *v1.Artifact, reader io.Reader, mode os.FileMode) (err error) {
508508
localPath := s.LocalPath(*artifact)
509509
tf, err := os.CreateTemp(filepath.Split(localPath))
510510
if err != nil {
@@ -546,7 +546,7 @@ func (s *Storage) AtomicWriteFile(artifact *v1.Artifact, reader io.Reader, mode
546546

547547
// Copy atomically copies the io.Reader contents to the v1.Artifact path.
548548
// If successful, it sets the digest and last update time on the artifact.
549-
func (s *Storage) Copy(artifact *v1.Artifact, reader io.Reader) (err error) {
549+
func (s Storage) Copy(artifact *v1.Artifact, reader io.Reader) (err error) {
550550
localPath := s.LocalPath(*artifact)
551551
tf, err := os.CreateTemp(filepath.Split(localPath))
552552
if err != nil {
@@ -584,7 +584,7 @@ func (s *Storage) Copy(artifact *v1.Artifact, reader io.Reader) (err error) {
584584

585585
// CopyFromPath atomically copies the contents of the given path to the path of the v1.Artifact.
586586
// If successful, the digest and last update time on the artifact is set.
587-
func (s *Storage) CopyFromPath(artifact *v1.Artifact, path string) (err error) {
587+
func (s Storage) CopyFromPath(artifact *v1.Artifact, path string) (err error) {
588588
f, err := os.Open(path)
589589
if err != nil {
590590
return err
@@ -599,7 +599,7 @@ func (s *Storage) CopyFromPath(artifact *v1.Artifact, path string) (err error) {
599599
}
600600

601601
// CopyToPath copies the contents in the (sub)path of the given artifact to the given path.
602-
func (s *Storage) CopyToPath(artifact *v1.Artifact, subPath, toPath string) error {
602+
func (s Storage) CopyToPath(artifact *v1.Artifact, subPath, toPath string) error {
603603
// create a tmp directory to store artifact
604604
tmp, err := os.MkdirTemp("", "flux-include-")
605605
if err != nil {
@@ -638,7 +638,7 @@ func (s *Storage) CopyToPath(artifact *v1.Artifact, subPath, toPath string) erro
638638
}
639639

640640
// Symlink creates or updates a symbolic link for the given v1.Artifact and returns the URL for the symlink.
641-
func (s *Storage) Symlink(artifact v1.Artifact, linkName string) (string, error) {
641+
func (s Storage) Symlink(artifact v1.Artifact, linkName string) (string, error) {
642642
localPath := s.LocalPath(artifact)
643643
dir := filepath.Dir(localPath)
644644
link := filepath.Join(dir, linkName)
@@ -660,14 +660,14 @@ func (s *Storage) Symlink(artifact v1.Artifact, linkName string) (string, error)
660660
}
661661

662662
// Lock creates a file lock for the given v1.Artifact.
663-
func (s *Storage) Lock(artifact v1.Artifact) (unlock func(), err error) {
663+
func (s Storage) Lock(artifact v1.Artifact) (unlock func(), err error) {
664664
lockFile := s.LocalPath(artifact) + ".lock"
665665
mutex := lockedfile.MutexAt(lockFile)
666666
return mutex.Lock()
667667
}
668668

669669
// LocalPath returns the secure local path of the given artifact (that is: relative to the Storage.BasePath).
670-
func (s *Storage) LocalPath(artifact v1.Artifact) string {
670+
func (s Storage) LocalPath(artifact v1.Artifact) string {
671671
if artifact.Path == "" {
672672
return ""
673673
}

0 commit comments

Comments
 (0)