Skip to content

Commit

Permalink
chore(pkg/layout): remove unnecessary named returns
Browse files Browse the repository at this point in the history
Signed-off-by: Kit Patella <[email protected]>
  • Loading branch information
mkcp committed Sep 10, 2024
1 parent 9c8e3e1 commit f8216e5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
26 changes: 13 additions & 13 deletions src/pkg/layout/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type Components struct {
var ErrNotLoaded = fmt.Errorf("not loaded")

// Archive archives a component.
func (c *Components) Archive(component v1alpha1.ZarfComponent, cleanupTemp bool) (err error) {
func (c *Components) Archive(component v1alpha1.ZarfComponent, cleanupTemp bool) error {
name := component.Name
if _, ok := c.Dirs[name]; !ok {
return &fs.PathError{
Expand Down Expand Up @@ -75,7 +75,7 @@ func (c *Components) Archive(component v1alpha1.ZarfComponent, cleanupTemp bool)
}

// Unarchive unarchives a component.
func (c *Components) Unarchive(component v1alpha1.ZarfComponent) (err error) {
func (c *Components) Unarchive(component v1alpha1.ZarfComponent) error {
name := component.Name
tb, ok := c.Tarballs[name]
if !ok {
Expand Down Expand Up @@ -138,7 +138,7 @@ func (c *Components) Unarchive(component v1alpha1.ZarfComponent) (err error) {
}

// Create creates a new component directory structure.
func (c *Components) Create(component v1alpha1.ZarfComponent) (cp *ComponentPaths, err error) {
func (c *Components) Create(component v1alpha1.ZarfComponent) (*ComponentPaths, error) {
name := component.Name

_, ok := c.Tarballs[name]
Expand All @@ -150,41 +150,41 @@ func (c *Components) Create(component v1alpha1.ZarfComponent) (cp *ComponentPath
}
}

if err = helpers.CreateDirectory(c.Base, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(c.Base, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}

base := filepath.Join(c.Base, name)

if err = helpers.CreateDirectory(base, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(base, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}

cp = &ComponentPaths{
cp := &ComponentPaths{
Base: base,
}

cp.Temp = filepath.Join(base, TempDir)
if err = helpers.CreateDirectory(cp.Temp, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.Temp, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}

if len(component.Files) > 0 {
cp.Files = filepath.Join(base, FilesDir)
if err = helpers.CreateDirectory(cp.Files, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.Files, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}
}

if len(component.Charts) > 0 {
cp.Charts = filepath.Join(base, ChartsDir)
if err = helpers.CreateDirectory(cp.Charts, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.Charts, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}
for _, chart := range component.Charts {
cp.Values = filepath.Join(base, ValuesDir)
if len(chart.ValuesFiles) > 0 {
if err = helpers.CreateDirectory(cp.Values, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.Values, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}
break
Expand All @@ -194,21 +194,21 @@ func (c *Components) Create(component v1alpha1.ZarfComponent) (cp *ComponentPath

if len(component.Repos) > 0 {
cp.Repos = filepath.Join(base, ReposDir)
if err = helpers.CreateDirectory(cp.Repos, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.Repos, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}
}

if len(component.Manifests) > 0 {
cp.Manifests = filepath.Join(base, ManifestsDir)
if err = helpers.CreateDirectory(cp.Manifests, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.Manifests, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}
}

if len(component.DataInjections) > 0 {
cp.DataInjections = filepath.Join(base, DataInjectionsDir)
if err = helpers.CreateDirectory(cp.DataInjections, helpers.ReadWriteExecuteUser); err != nil {
if err := helpers.CreateDirectory(cp.DataInjections, helpers.ReadWriteExecuteUser); err != nil {
return nil, err
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/pkg/layout/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,14 @@ func New(baseDir string) *PackagePaths {

// ReadZarfYAML reads a zarf.yaml file into memory,
// checks if it's using the legacy layout, and migrates deprecated component configs.
func (pp *PackagePaths) ReadZarfYAML() (pkg v1alpha1.ZarfPackage, warnings []string, err error) {
func (pp *PackagePaths) ReadZarfYAML() (v1alpha1.ZarfPackage, []string, error) {
var pkg v1alpha1.ZarfPackage

if err := utils.ReadYaml(pp.ZarfYAML, &pkg); err != nil {
return v1alpha1.ZarfPackage{}, nil, fmt.Errorf("unable to read zarf.yaml: %w", err)
}

warnings := make([]string, 0)
if pp.IsLegacyLayout() {
warnings = append(warnings, "Detected deprecated package layout, migrating to new layout - support for this package will be dropped in v1.0.0")
}
Expand All @@ -74,7 +77,7 @@ func (pp *PackagePaths) ReadZarfYAML() (pkg v1alpha1.ZarfPackage, warnings []str
}

// MigrateLegacy migrates a legacy package layout to the new layout.
func (pp *PackagePaths) MigrateLegacy() (err error) {
func (pp *PackagePaths) MigrateLegacy() error {
var pkg v1alpha1.ZarfPackage
base := pp.Base

Expand Down
17 changes: 11 additions & 6 deletions src/pkg/layout/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type SBOMs struct {
}

// Unarchive unarchives the package's SBOMs.
func (s *SBOMs) Unarchive() (err error) {
func (s *SBOMs) Unarchive() error {
if s.Path == "" || helpers.InvalidPath(s.Path) {
return &fs.PathError{
Op: "stat",
Expand All @@ -47,7 +47,7 @@ func (s *SBOMs) Unarchive() (err error) {
}

// Archive archives the package's SBOMs.
func (s *SBOMs) Archive() (err error) {
func (s *SBOMs) Archive() error {
if s.Path == "" || helpers.InvalidPath(s.Path) {
return &fs.PathError{
Op: "stat",
Expand All @@ -68,18 +68,23 @@ func (s *SBOMs) Archive() (err error) {
return os.RemoveAll(dir)
}

// StageSBOMViewFiles copies SBOM viewer HTML files to the Zarf SBOM directory.
func (s *SBOMs) StageSBOMViewFiles() (sbomViewFiles, warnings []string, err error) {
// StageSBOMViewFiles copies SBOM viewer HTML files to the Zarf SBOM directory. Returns sbomViewFiles, warnings, and an
// error.
func (s *SBOMs) StageSBOMViewFiles() ([]string, []string, error) {
sbomViewFiles := make([]string, 0)
warnings := make([]string, 0)

if s.IsTarball() {
return nil, nil, fmt.Errorf("unable to process the SBOM files for this package: %s is a tarball", s.Path)
}

// If SBOMs were loaded, temporarily place them in the deploy directory
if !helpers.InvalidPath(s.Path) {
sbomViewFiles, err = filepath.Glob(filepath.Join(s.Path, "sbom-viewer-*"))
files, err := filepath.Glob(filepath.Join(s.Path, "sbom-viewer-*"))
if err != nil {
return nil, nil, err
}
sbomViewFiles = files

if _, err := s.OutputSBOMFiles(SBOMDir, ""); err != nil {
// Don't stop the deployment, let the user decide if they want to continue the deployment
Expand Down Expand Up @@ -107,6 +112,6 @@ func (s *SBOMs) OutputSBOMFiles(outputDir, packageName string) (string, error) {
}

// IsTarball returns true if the SBOMs are a tarball.
func (s SBOMs) IsTarball() bool {
func (s *SBOMs) IsTarball() bool {
return !helpers.IsDir(s.Path) && filepath.Ext(s.Path) == ".tar"
}

0 comments on commit f8216e5

Please sign in to comment.