forked from operator-framework/catalogd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
obsoletes operator-framework#113 Signed-off-by: Anik <anikbhattacharya93@gmail.com>
- Loading branch information
Showing
5 changed files
with
96 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package storage | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/operator-framework/operator-registry/alpha/declcfg" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// Storage is a store of FBC content of catalogs added to a cluster. | ||
// It can be used to Store or Delete FBC in the host's filesystem | ||
type Storage struct { | ||
RootDirectory string | ||
} | ||
|
||
func NewStorage(rootDir string) Storage { | ||
return Storage{ | ||
RootDirectory: rootDir, | ||
} | ||
} | ||
|
||
func (s *Storage) Store(_ context.Context, owner client.Object, fbc *declcfg.DeclarativeConfig) error { | ||
fbcFile, err := os.Create(s.fbcPath(owner.GetName())) | ||
if err != nil { | ||
return err | ||
} | ||
defer fbcFile.Close() | ||
|
||
if err := declcfg.WriteJSON(*fbc, fbcFile); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (s *Storage) Delete(_ context.Context, owner client.Object) error { | ||
return ignoreNotExist(os.Remove(s.fbcPath(owner.GetName()))) | ||
} | ||
|
||
func (s *Storage) fbcPath(catalogName string) string { | ||
return filepath.Join(s.RootDirectory, fmt.Sprintf("%s.json", catalogName)) | ||
} | ||
|
||
func ignoreNotExist(err error) error { | ||
if errors.Is(err, os.ErrNotExist) { | ||
return nil | ||
} | ||
return err | ||
} |