Skip to content

Commit

Permalink
refactor: ensure there is a newline before returns and branch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Mar 6, 2025
1 parent 957c776 commit a44fa5d
Show file tree
Hide file tree
Showing 196 changed files with 754 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ linters:
- nakedret
- nilerr
# - nilnil
# - nlreturn
- nlreturn
# - noctx
- nolintlint
# - nosprintfhostport
Expand Down
3 changes: 3 additions & 0 deletions artifact/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func V1ImageFromRemoteName(imageName string, imageOptions ...remote.Option) (v1.
return nil, fmt.Errorf("couldn’t pull remote image %s: %v", tag, err)
}
}

return image, nil
}

Expand All @@ -115,6 +116,7 @@ func NewFromRemoteName(imageName string, imageOptions ...remote.Option) (scalibr
if err != nil {
return nil, fmt.Errorf("failed to load image from remote name %q: %w", imageName, err)
}

return NewFromImage(image)
}

Expand All @@ -140,5 +142,6 @@ func NewFromImage(image v1.Image) (scalibrfs.FS, error) {
if err = unpacker.UnpackSquashed(outDir, image); err != nil {
return nil, fmt.Errorf("failed to unpack image into directory %q: %w", outDir, err)
}

return scalibrfs.DirFS(outDir), nil
}
7 changes: 7 additions & 0 deletions artifact/image/layerscanning/image/file_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func (f *fileNode) Stat() (fs.FileInfo, error) {
if f.isWhiteout {
return nil, fs.ErrNotExist
}

return f, nil
}

Expand All @@ -77,6 +78,7 @@ func (f *fileNode) Read(b []byte) (n int, err error) {
if err != nil {
return 0, err
}

return f.file.Read(b)
}

Expand All @@ -91,6 +93,7 @@ func (f *fileNode) ReadAt(b []byte, off int64) (n int, err error) {
if err != nil {
return 0, err
}

return f.file.ReadAt(b, off)
}

Expand All @@ -104,6 +107,7 @@ func (f *fileNode) Seek(offset int64, whence int) (n int64, err error) {
if err != nil {
return 0, err
}

return f.file.Seek(offset, whence)
}

Expand All @@ -112,8 +116,10 @@ func (f *fileNode) Close() error {
if f.file != nil {
err := f.file.Close()
f.file = nil

return err
}

return nil
}

Expand All @@ -130,6 +136,7 @@ func (f *fileNode) RealFilePath() string {
// Name returns the name of the fileNode. Name is also used to implement the fs.FileInfo interface.
func (f *fileNode) Name() string {
_, filename := path.Split(f.virtualPath)

return filename
}

Expand Down
2 changes: 2 additions & 0 deletions artifact/image/layerscanning/image/file_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func TestStat(t *testing.T) {
if diff := cmp.Diff(tc.wantErr, gotErr, cmpopts.EquateErrors()); diff != "" {
t.Errorf("Stat(%v) returned unexpected error (-want +got): %v", tc.node, diff)
}

return
}

Expand Down Expand Up @@ -389,6 +390,7 @@ func TestReadAt(t *testing.T) {

if diff := cmp.Diff(tc.wantErr, gotErr, cmpopts.EquateErrors()); diff != "" {
t.Errorf("ReadAt(%v) returned unexpected error (-want +got): %v", tc.node, diff)

return
}

Expand Down
1 change: 1 addition & 0 deletions artifact/image/layerscanning/image/find_base_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,6 @@ func findBaseImageIndex(histories []v1.History) (int, error) {
}
}
}

return 0, ErrBaseImageNotFound
}
1 change: 1 addition & 0 deletions artifact/image/layerscanning/image/find_base_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func TestFindBaseImageIndex(t *testing.T) {
gotIndex, gotErr := findBaseImageIndex(test.histories)
if test.wantError != gotErr {
t.Errorf("findBaseImageIndex(%v) returned error: %v, want error: %v", test.histories, gotErr, test.wantError)

return
}

Expand Down
16 changes: 16 additions & 0 deletions artifact/image/layerscanning/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func validateConfig(config *Config) error {
if config.MaxSymlinkDepth < 0 {
return fmt.Errorf("%w: max symlink depth must be non-negative: %d", ErrInvalidConfig, config.MaxSymlinkDepth)
}

return nil
}

Expand All @@ -111,6 +112,7 @@ func (img *Image) ChainLayers() ([]scalibrImage.ChainLayer, error) {
for _, chainLayer := range img.chainLayers {
scalibrChainLayers = append(scalibrChainLayers, chainLayer)
}

return scalibrChainLayers, nil
}

Expand All @@ -125,6 +127,7 @@ func FromRemoteName(imageName string, config *Config, imageOptions ...remote.Opt
if err != nil {
return nil, fmt.Errorf("failed to load image from remote name %q: %w", imageName, err)
}

return FromV1Image(v1Image, config)
}

Expand All @@ -135,6 +138,7 @@ func FromTarball(tarPath string, config *Config) (*Image, error) {
if err != nil {
return nil, fmt.Errorf("failed to load image from tarball with path %q: %w", tarPath, err)
}

return FromV1Image(v1Image, config)
}

Expand Down Expand Up @@ -246,6 +250,7 @@ func FromV1Image(v1Image v1.Image, config *Config) (*Image, error) {
if err != nil {
return fmt.Errorf("failed to fill chain layer with v1 layer tar: %w", err)
}

return nil
}()

Expand All @@ -263,6 +268,7 @@ func FromV1Image(v1Image v1.Image, config *Config) (*Image, error) {
for _, isRequired := range requiredTargets {
if isRequired {
stillHaveRequiredTargets = true

break
}
}
Expand All @@ -271,6 +277,7 @@ func FromV1Image(v1Image v1.Image, config *Config) (*Image, error) {
break
}
}

return &outputImage, nil
}

Expand Down Expand Up @@ -308,6 +315,7 @@ func initializeChainLayers(v1Layers []v1.Layer, configFile *v1.ConfigFile, maxSy
maxSymlinkDepth: maxSymlinkDepth,
})
historyIndex++

continue
}

Expand Down Expand Up @@ -436,13 +444,15 @@ func fillChainLayersWithFilesFromTar(img *Image, tarReader *tar.Reader, originLa
for _, p := range []string{realFilePath, cleanedFilePath, virtualPath} {
if requirer.FileRequired(p, header.FileInfo()) {
required = true

break
}
if _, ok := requiredTargets[p]; ok {
required = true

// The required target has been checked, so it can be marked as not required.
requiredTargets[p] = false

break
}
}
Expand All @@ -462,14 +472,17 @@ func fillChainLayersWithFilesFromTar(img *Image, tarReader *tar.Reader, originLa
newNode, err = img.handleSymlink(virtualPath, originLayerID, tarReader, header, isWhiteout, requiredTargets)
default:
log.Warnf("unsupported file type: %v, path: %s", header.Typeflag, header.Name)

continue
}

if err != nil {
if errors.Is(err, ErrFileReadLimitExceeded) {
log.Warnf("failed to handle tar entry with path %s: %w", virtualPath, err)

continue
}

return nil, fmt.Errorf("failed to handle tar entry with path %s: %w", virtualPath, err)
}

Expand All @@ -486,6 +499,7 @@ func fillChainLayersWithFilesFromTar(img *Image, tarReader *tar.Reader, originLa
layer := currentChainLayer.latestLayer.(*Layer)
layer.fileNodeTree.Insert(virtualPath, newNode)
}

return requiredTargets, nil
}

Expand Down Expand Up @@ -527,6 +541,7 @@ func (img *Image) handleSymlink(virtualPath, originLayerID string, tarReader *ta

if symlink.TargetOutsideRoot(virtualPath, targetPath) {
log.Warnf("Found symlink that points outside the root, skipping: %q -> %q", virtualPath, targetPath)

return nil, fmt.Errorf("%w: %q -> %q", ErrSymlinkPointsOutsideRoot, virtualPath, targetPath)
}

Expand Down Expand Up @@ -651,5 +666,6 @@ func inWhiteoutDir(layer *chainLayer, filePath string) bool {

filePath = dirname
}

return false
}
3 changes: 3 additions & 0 deletions artifact/image/layerscanning/image/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ func (fakeV1Image *fakeV1Image) Layers() ([]v1.Layer, error) {
if fakeV1Image.errorOnLayers {
return nil, fmt.Errorf("error on layers")
}

return fakeV1Image.layers, nil
}

func (fakeV1Image *fakeV1Image) ConfigFile() (*v1.ConfigFile, error) {
if fakeV1Image.errorOnConfigFile {
return nil, fmt.Errorf("error on config file")
}

return fakeV1Image.config, nil
}

Expand Down Expand Up @@ -1036,5 +1038,6 @@ func constructImage(ctx context.Context, version, fakePackageName string) (*v1.I
return nil, fmt.Errorf("unable to create layer: %v", err)
}
image, err := mutate.AppendLayers(empty.Image, layer)

return &image, err
}
7 changes: 7 additions & 0 deletions artifact/image/layerscanning/image/layer.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func (layer *Layer) Uncompressed() (io.ReadCloser, error) {
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrUncompressedReaderMissingFromLayer, err)
}

return uncompressed, nil
}

Expand Down Expand Up @@ -200,6 +201,7 @@ func (chainfs FS) Open(name string) (fs.File, error) {
if err != nil {
return nil, fmt.Errorf("failed to resolve symlink for file node %s: %w", fileNode.virtualPath, err)
}

return resolvedNode, nil
}

Expand All @@ -214,6 +216,7 @@ func (chainfs *FS) Stat(name string) (fs.FileInfo, error) {
if err != nil {
return nil, fmt.Errorf("failed to resolve symlink for file node %s: %w", node.virtualPath, err)
}

return resolvedNode.Stat()
}

Expand Down Expand Up @@ -246,6 +249,7 @@ func (chainfs *FS) ReadDir(name string) ([]fs.DirEntry, error) {
slices.SortFunc(dirEntries, func(a, b fs.DirEntry) int {
return strings.Compare(a.Name(), b.Name())
})

return dirEntries, nil
}

Expand All @@ -260,6 +264,7 @@ func (chainfs *FS) getFileNode(path string) (*fileNode, error) {
if node == nil {
return nil, fs.ErrNotExist
}

return node, nil
}

Expand All @@ -272,6 +277,7 @@ func (chainfs *FS) getFileNodeChildren(path string) ([]*fileNode, error) {
if children == nil {
return nil, fs.ErrNotExist
}

return children, nil
}

Expand All @@ -288,5 +294,6 @@ func normalizePath(path string) string {
if path[0] != '/' {
path = "/" + path
}

return path
}
Loading

0 comments on commit a44fa5d

Please sign in to comment.