Skip to content

Commit

Permalink
Merge pull request #5689 from nalind/linters
Browse files Browse the repository at this point in the history
CI: enable the gofumpt and whitespace linters
  • Loading branch information
openshift-merge-bot[bot] authored Aug 16, 2024
2 parents 688affa + ca3b80e commit 899b05e
Show file tree
Hide file tree
Showing 66 changed files with 411 additions and 443 deletions.
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ run:
linters:
enable:
- gofmt
- gofumpt
- revive
- unconvert
- unparam
- whitespace
5 changes: 2 additions & 3 deletions add.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

// AddAndCopyOptions holds options for add and copy commands.
type AddAndCopyOptions struct {
//Chmod sets the access permissions of the destination content.
// Chmod sets the access permissions of the destination content.
Chmod string
// Chown is a spec for the user who should be given ownership over the
// newly-added content, potentially overriding permissions which would
Expand Down Expand Up @@ -176,7 +176,7 @@ func getURL(src string, chown *idtools.IDPair, mountpoint, renameTarget string,
uid = chown.UID
gid = chown.GID
}
var mode int64 = 0600
var mode int64 = 0o600
if chmod != nil {
mode = int64(*chmod)
}
Expand Down Expand Up @@ -689,7 +689,6 @@ func (b *Builder) userForRun(mountPoint string, userspec string) (specs.User, st
} else {
u.AdditionalGids = groups
}

}
return u, homeDir, err
}
Expand Down
10 changes: 5 additions & 5 deletions bind/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func SetupIntermediateMountNamespace(spec *specs.Spec, bundlePath string) (unmou
if err != nil {
return nil, fmt.Errorf("checking permissions on %q: %w", bundlePath, err)
}
if err = os.Chmod(bundlePath, info.Mode()|0111); err != nil {
if err = os.Chmod(bundlePath, info.Mode()|0o111); err != nil {
return nil, fmt.Errorf("loosening permissions on %q: %w", bundlePath, err)
}

Expand Down Expand Up @@ -115,7 +115,7 @@ func SetupIntermediateMountNamespace(spec *specs.Spec, bundlePath string) (unmou
// other unprivileged users outside of containers, shouldn't be able to
// access.
mnt := filepath.Join(bundlePath, "mnt")
if err = idtools.MkdirAndChown(mnt, 0100, idtools.IDPair{UID: int(rootUID), GID: int(rootGID)}); err != nil {
if err = idtools.MkdirAndChown(mnt, 0o100, idtools.IDPair{UID: int(rootUID), GID: int(rootGID)}); err != nil {
return unmountAll, fmt.Errorf("creating %q owned by the container's root user: %w", mnt, err)
}

Expand All @@ -128,7 +128,7 @@ func SetupIntermediateMountNamespace(spec *specs.Spec, bundlePath string) (unmou

// Create a bind mount for the root filesystem and add it to the list.
rootfs := filepath.Join(mnt, "rootfs")
if err = os.Mkdir(rootfs, 0000); err != nil {
if err = os.Mkdir(rootfs, 0o000); err != nil {
return unmountAll, fmt.Errorf("creating directory %q: %w", rootfs, err)
}
if err = unix.Mount(rootPath, rootfs, "", unix.MS_BIND|unix.MS_REC|unix.MS_PRIVATE, ""); err != nil {
Expand Down Expand Up @@ -159,13 +159,13 @@ func SetupIntermediateMountNamespace(spec *specs.Spec, bundlePath string) (unmou
if info.IsDir() {
// If the source is a directory, make one to use as the
// mount target.
if err = os.Mkdir(stage, 0000); err != nil {
if err = os.Mkdir(stage, 0o000); err != nil {
return unmountAll, fmt.Errorf("creating directory %q: %w", stage, err)
}
} else {
// If the source is not a directory, create an empty
// file to use as the mount target.
file, err := os.OpenFile(stage, os.O_WRONLY|os.O_CREATE, 0000)
file, err := os.OpenFile(stage, os.O_WRONLY|os.O_CREATE, 0o000)
if err != nil {
return unmountAll, fmt.Errorf("creating file %q: %w", stage, err)
}
Expand Down
2 changes: 1 addition & 1 deletion buildah.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ func (b *Builder) Save() error {
if err != nil {
return err
}
if err = ioutils.AtomicWriteFile(filepath.Join(cdir, stateFile), buildstate, 0600); err != nil {
if err = ioutils.AtomicWriteFile(filepath.Join(cdir, stateFile), buildstate, 0o600); err != nil {
return fmt.Errorf("saving builder state to %q: %w", filepath.Join(cdir, stateFile), err)
}
return nil
Expand Down
10 changes: 4 additions & 6 deletions buildah_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ import (
"github.com/stretchr/testify/require"
)

var (
testSystemContext = imagetypes.SystemContext{
SignaturePolicyPath: "tests/policy.json",
SystemRegistriesConfPath: "tests/registries.conf",
}
)
var testSystemContext = imagetypes.SystemContext{
SignaturePolicyPath: "tests/policy.json",
SystemRegistriesConfPath: "tests/registries.conf",
}

func TestMain(m *testing.M) {
var logLevel string
Expand Down
4 changes: 2 additions & 2 deletions chroot/pty_ptmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// this instead of posix_openpt is that it avoids cgo.
func getPtyDescriptors() (int, int, error) {
// Create a pseudo-terminal -- open a copy of the master side.
controlFd, err := unix.Open("/dev/ptmx", os.O_RDWR, 0600)
controlFd, err := unix.Open("/dev/ptmx", os.O_RDWR, 0o600)
if err != nil {
return -1, -1, fmt.Errorf("opening PTY master using /dev/ptmx: %v", err)
}
Expand All @@ -36,7 +36,7 @@ func getPtyDescriptors() (int, int, error) {
return -1, -1, fmt.Errorf("getting PTY number: %v", err)
}
ptyName := fmt.Sprintf("/dev/pts/%d", ptyN)
fd, err := unix.Open(ptyName, unix.O_RDWR|unix.O_NOCTTY, 0620)
fd, err := unix.Open(ptyName, unix.O_RDWR|unix.O_NOCTTY, 0o620)
if err != nil {
return -1, -1, fmt.Errorf("opening PTY %q: %v", ptyName, err)
}
Expand Down
5 changes: 2 additions & 3 deletions chroot/run_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func RunUsingChroot(spec *specs.Spec, bundlePath, homeDir string, stdin io.Reade
if err != nil {
return err
}
if err = ioutils.AtomicWriteFile(filepath.Join(bundlePath, "config.json"), specbytes, 0600); err != nil {
if err = ioutils.AtomicWriteFile(filepath.Join(bundlePath, "config.json"), specbytes, 0o600); err != nil {
return fmt.Errorf("storing runtime configuration: %w", err)
}
logrus.Debugf("config = %v", string(specbytes))
Expand Down Expand Up @@ -265,7 +265,7 @@ func runUsingChrootMain() {
logrus.Warnf("error %s ownership of container PTY %sto %d/%d: %v", op, from, rootUID, rootGID, err)
}
// Set permissions on the PTY.
if err = ctty.Chmod(0620); err != nil {
if err = ctty.Chmod(0o620); err != nil {
logrus.Errorf("error setting permissions of container PTY: %v", err)
os.Exit(1)
}
Expand Down Expand Up @@ -525,7 +525,6 @@ func runUsingChroot(spec *specs.Spec, bundlePath string, ctty *os.File, stdin io
cmd.ExtraFiles = append([]*os.File{preader}, cmd.ExtraFiles...)
if err := setPlatformUnshareOptions(spec, cmd); err != nil {
return 1, fmt.Errorf("setting platform unshare options: %w", err)

}
interrupted := make(chan os.Signal, 100)
cmd.Hook = func(int) error {
Expand Down
6 changes: 3 additions & 3 deletions chroot/run_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ func setupChrootBindMounts(spec *specs.Spec, bundlePath string) (undoBinds func(
// XXX: This was copied from the linux version which supports bind mounting files.
// Leaving it here since I plan to add this to FreeBSD's nullfs.
if m.Type != "nullfs" || srcinfo.IsDir() {
if err = os.MkdirAll(target, 0111); err != nil {
if err = os.MkdirAll(target, 0o111); err != nil {
return undoBinds, fmt.Errorf("creating mountpoint %q in mount namespace: %w", target, err)
}
removes = append(removes, target)
} else {
if err = os.MkdirAll(filepath.Dir(target), 0111); err != nil {
if err = os.MkdirAll(filepath.Dir(target), 0o111); err != nil {
return undoBinds, fmt.Errorf("ensuring parent of mountpoint %q (%q) is present in mount namespace: %w", target, filepath.Dir(target), err)
}
// Don't do this until we can support file mounts in nullfs
Expand All @@ -218,7 +218,7 @@ func setupChrootBindMounts(spec *specs.Spec, bundlePath string) (undoBinds func(
save := saveDir(spec, target)
if err := fileutils.Exists(save); err != nil {
if errors.Is(err, fs.ErrNotExist) {
err = os.MkdirAll(save, 0111)
err = os.MkdirAll(save, 0o111)
}
if err != nil {
return undoBinds, fmt.Errorf("creating file mount save directory %q: %w", save, err)
Expand Down
18 changes: 9 additions & 9 deletions chroot/run_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func setupChrootBindMounts(spec *specs.Spec, bundlePath string) (undoBinds func(
subDev := filepath.Join(spec.Root.Path, "/dev")
if err := unix.Mount("/dev", subDev, "bind", devFlags, ""); err != nil {
if errors.Is(err, os.ErrNotExist) {
err = os.Mkdir(subDev, 0755)
err = os.Mkdir(subDev, 0o755)
if err == nil {
err = unix.Mount("/dev", subDev, "bind", devFlags, "")
}
Expand All @@ -325,7 +325,7 @@ func setupChrootBindMounts(spec *specs.Spec, bundlePath string) (undoBinds func(
subProc := filepath.Join(spec.Root.Path, "/proc")
if err := unix.Mount("/proc", subProc, "bind", procFlags, ""); err != nil {
if errors.Is(err, os.ErrNotExist) {
err = os.Mkdir(subProc, 0755)
err = os.Mkdir(subProc, 0o755)
if err == nil {
err = unix.Mount("/proc", subProc, "bind", procFlags, "")
}
Expand All @@ -340,7 +340,7 @@ func setupChrootBindMounts(spec *specs.Spec, bundlePath string) (undoBinds func(
subSys := filepath.Join(spec.Root.Path, "/sys")
if err := unix.Mount("/sys", subSys, "bind", sysFlags, ""); err != nil {
if errors.Is(err, os.ErrNotExist) {
err = os.Mkdir(subSys, 0755)
err = os.Mkdir(subSys, 0o755)
if err == nil {
err = unix.Mount("/sys", subSys, "bind", sysFlags, "")
}
Expand All @@ -363,9 +363,9 @@ func setupChrootBindMounts(spec *specs.Spec, bundlePath string) (undoBinds func(
if err := unix.Mount(m.Mountpoint, subSys, "bind", sysFlags, ""); err != nil {
msg := fmt.Sprintf("could not bind mount %q, skipping: %v", m.Mountpoint, err)
if strings.HasPrefix(m.Mountpoint, "/sys") {
logrus.Infof(msg)
logrus.Info(msg)
} else {
logrus.Warningf(msg)
logrus.Warning(msg)
}
continue
}
Expand Down Expand Up @@ -432,15 +432,15 @@ func setupChrootBindMounts(spec *specs.Spec, bundlePath string) (undoBinds func(
// The target isn't there yet, so create it. If the source is a directory,
// we need a directory, otherwise we need a non-directory (i.e., a file).
if srcinfo.IsDir() {
if err = os.MkdirAll(target, 0755); err != nil {
if err = os.MkdirAll(target, 0o755); err != nil {
return undoBinds, fmt.Errorf("creating mountpoint %q in mount namespace: %w", target, err)
}
} else {
if err = os.MkdirAll(filepath.Dir(target), 0755); err != nil {
if err = os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
return undoBinds, fmt.Errorf("ensuring parent of mountpoint %q (%q) is present in mount namespace: %w", target, filepath.Dir(target), err)
}
var file *os.File
if file, err = os.OpenFile(target, os.O_WRONLY|os.O_CREATE, 0755); err != nil {
if file, err = os.OpenFile(target, os.O_WRONLY|os.O_CREATE, 0o755); err != nil {
return undoBinds, fmt.Errorf("creating mountpoint %q in mount namespace: %w", target, err)
}
file.Close()
Expand Down Expand Up @@ -593,7 +593,7 @@ func setupChrootBindMounts(spec *specs.Spec, bundlePath string) (undoBinds func(
// Create an empty directory for to use for masking directories.
roEmptyDir := filepath.Join(bundlePath, "empty")
if len(spec.Linux.MaskedPaths) > 0 {
if err := os.Mkdir(roEmptyDir, 0700); err != nil {
if err := os.Mkdir(roEmptyDir, 0o700); err != nil {
return undoBinds, fmt.Errorf("creating empty directory %q: %w", roEmptyDir, err)
}
}
Expand Down
15 changes: 8 additions & 7 deletions chroot/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ func testMinimal(t *testing.T, modify func(g *generate.Generator, rootDir, bundl
// t.TempDir returns /tmp/TestName/001.
// /tmp/TestName/001 has permission 0777, but /tmp/TestName is 0700
tempDir := t.TempDir()
if err = os.Chmod(filepath.Dir(tempDir), 0711); err != nil {
if err = os.Chmod(filepath.Dir(tempDir), 0o711); err != nil {
t.Fatalf("error loosening permissions on %q: %v", tempDir, err)
}

rootDir := filepath.Join(tempDir, "root")
if err := os.Mkdir(rootDir, 0711); err != nil {
if err := os.Mkdir(rootDir, 0o711); err != nil {
t.Fatalf("os.Mkdir(%q): %v", rootDir, err)
}

rootTmpDir := filepath.Join(rootDir, "tmp")
if err := os.Mkdir(rootTmpDir, 01777); err != nil {
if err := os.Mkdir(rootTmpDir, 0o1777); err != nil {
t.Fatalf("os.Mkdir(%q): %v", rootTmpDir, err)
}

Expand All @@ -69,7 +69,7 @@ func testMinimal(t *testing.T, modify func(g *generate.Generator, rootDir, bundl
t.Fatalf("open(%q): %v", specPath, err)
}
defer specBinarySource.Close()
specBinary, err := os.OpenFile(filepath.Join(rootDir, reportCommand), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0711)
specBinary, err := os.OpenFile(filepath.Join(rootDir, reportCommand), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o711)
if err != nil {
t.Fatalf("open(%q): %v", filepath.Join(rootDir, reportCommand), err)
}
Expand All @@ -83,7 +83,7 @@ func testMinimal(t *testing.T, modify func(g *generate.Generator, rootDir, bundl
g.SetProcessArgs([]string{"/" + reportCommand})

bundleDir := filepath.Join(tempDir, "bundle")
if err := os.Mkdir(bundleDir, 0700); err != nil {
if err := os.Mkdir(bundleDir, 0o700); err != nil {
t.Fatalf("os.Mkdir(%q): %v", bundleDir, err)
}

Expand Down Expand Up @@ -223,7 +223,7 @@ func TestProcessCwd(t *testing.T) {
}
testMinimal(t,
func(g *generate.Generator, rootDir, _ string) {
if err := os.Mkdir(filepath.Join(rootDir, "/no-such-directory"), 0700); err != nil {
if err := os.Mkdir(filepath.Join(rootDir, "/no-such-directory"), 0o700); err != nil {
t.Fatalf("mkdir(%q): %v", filepath.Join(rootDir, "/no-such-directory"), err)
}
g.SetProcessCwd("/no-such-directory")
Expand Down Expand Up @@ -431,7 +431,8 @@ func TestMounts(t *testing.T) {
name: "nosuid",
destination: "/nosuid",
options: []string{"nosuid"},
reject: []string{"suid"}},
reject: []string{"suid"},
},
{
name: "nodev,noexec",
destination: "/nodev,noexec",
Expand Down
2 changes: 1 addition & 1 deletion cmd/buildah/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func init() {

func buildCmd(c *cobra.Command, inputArgs []string, iopts buildahcli.BuildOptions) error {
if c.Flag("logfile").Changed {
logfile, err := os.OpenFile(iopts.Logfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
logfile, err := os.OpenFile(iopts.Logfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o600)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/buildah/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ func init() {
commitCommand.SetUsageTemplate(UsageTemplate())
commitListFlagSet(commitCommand, &opts)
rootCmd.AddCommand(commitCommand)

}

func commitListFlagSet(cmd *cobra.Command, opts *commitInputOptions) {
Expand Down Expand Up @@ -125,7 +124,7 @@ func commitListFlagSet(cmd *cobra.Command, opts *commitInputOptions) {
_ = cmd.RegisterFlagCompletionFunc("reference-time", completion.AutocompleteNone)

flags.StringVar(&opts.pull, "pull", "true", "pull SBOM scanner images from the registry if newer or not present in store, if false, only pull SBOM scanner images if not present, if always, pull SBOM scanner images even if the named images are present in store, if never, only use images present in store if available")
flags.Lookup("pull").NoOptDefVal = "true" //allow `--pull ` to be set to `true` as expected.
flags.Lookup("pull").NoOptDefVal = "true" // allow `--pull ` to be set to `true` as expected.

flags.BoolVar(&opts.pullAlways, "pull-always", false, "pull the image even if the named image is present in store")
if err := flags.MarkHidden("pull-always"); err != nil {
Expand Down
6 changes: 2 additions & 4 deletions cmd/buildah/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ import (
"github.com/spf13/pflag"
)

var (
// configuration, including customizations made in containers.conf
needToShutdownStore = false
)
// configuration, including customizations made in containers.conf
var needToShutdownStore = false

func getStore(c *cobra.Command) (storage.Store, error) {
if err := setXDGRuntimeDir(); err != nil {
Expand Down
4 changes: 1 addition & 3 deletions cmd/buildah/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func init() {
flags.StringSliceVar(&opts.unsetLabels, "unsetlabel", nil, "remove image configuration label")

rootCmd.AddCommand(configCommand)

}

func updateCmd(builder *buildah.Builder, cmd string) error {
Expand Down Expand Up @@ -386,8 +385,7 @@ func updateHealthcheck(builder *buildah.Builder, c *cobra.Command, iopts configR
if c.Flag("healthcheck-retries").Changed {
healthcheck.Retries = iopts.healthcheckRetries
args = args + "--retries=" + strconv.Itoa(iopts.healthcheckRetries) + " "
//args = fmt.Sprintf("%s --retries=%d ", args, iopts.healthcheckRetries)

// args = fmt.Sprintf("%s --retries=%d ", args, iopts.healthcheckRetries)
}
if c.Flag("healthcheck-start-period").Changed {
duration, err := time.ParseDuration(iopts.healthcheckStartPeriod)
Expand Down
14 changes: 9 additions & 5 deletions cmd/buildah/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func init() {
Aliases: []string{"list", "ls", "ps"},
Short: "List working containers and their base images",
Long: containersDescription,
//Flags: sortFlags(containersFlags),
// Flags: sortFlags(containersFlags),
RunE: func(cmd *cobra.Command, args []string) error {
return containersCmd(cmd, args, opts)
},
Expand Down Expand Up @@ -168,11 +168,13 @@ func outputContainers(store storage.Store, opts containerOptions, params *contai
continue
}
if opts.json {
JSONContainers = append(JSONContainers, jsonContainer{ID: builder.ContainerID,
JSONContainers = append(JSONContainers, jsonContainer{
ID: builder.ContainerID,
Builder: true,
ImageID: builder.FromImageID,
ImageName: image,
ContainerName: builder.Container})
ContainerName: builder.Container,
})
continue
}
output := containerOutputParams{
Expand Down Expand Up @@ -208,11 +210,13 @@ func outputContainers(store storage.Store, opts containerOptions, params *contai
continue
}
if opts.json {
JSONContainers = append(JSONContainers, jsonContainer{ID: container.ID,
JSONContainers = append(JSONContainers, jsonContainer{
ID: container.ID,
Builder: ours,
ImageID: container.ImageID,
ImageName: imageNameForID(container.ImageID),
ContainerName: name})
ContainerName: name,
})
continue
}
output := containerOutputParams{
Expand Down
Loading

0 comments on commit 899b05e

Please sign in to comment.