Skip to content

Commit

Permalink
only use sudo when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
brycekahle committed Sep 28, 2023
1 parent 7e41a8e commit 6726c4d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# binary
btfhub
# arvhices
archive/*
custom-archive/*
# makefile leftovers
.check*
# binary
# JetBrains
.idea/
5 changes: 2 additions & 3 deletions pkg/pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ func yumDownload(ctx context.Context, pkg string, destdir string) error {

destDirParam := fmt.Sprintf("--downloaddir=%s", destdir)

cmd := exec.CommandContext(ctx,
"sudo", "yum", "install", "-y", "--downloadonly", destDirParam, pkg,
)
binary, args := utils.SudoCMD("yum", "install", "-y", "--downloadonly", destDirParam, pkg)
cmd := exec.CommandContext(ctx, binary, args...)

cmd.Stdout = os.Stdout
cmd.Stderr = stderr
Expand Down
3 changes: 2 additions & 1 deletion pkg/repo/rhel.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func (d *RHELRepo) GetKernelPackages(
) error {
altArch := d.archs[arch]
rver := d.releaseVersions[release+":"+altArch]
if err := utils.RunCMD(ctx, "", "sudo", "subscription-manager", "release", fmt.Sprintf("--set=%s", rver)); err != nil {
binary, args := utils.SudoCMD("subscription-manager", "release", fmt.Sprintf("--set=%s", rver))
if err := utils.RunCMD(ctx, "", binary, args...); err != nil {
return err
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/repo/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
"path/filepath"
"strings"

"golang.org/x/exp/maps"

"github.com/aquasecurity/btfhub/pkg/job"
"github.com/aquasecurity/btfhub/pkg/kernel"
"github.com/aquasecurity/btfhub/pkg/pkg"
"github.com/aquasecurity/btfhub/pkg/utils"
"golang.org/x/exp/maps"
)

func parseYumPackages(rdr io.Reader, minVersion kernel.Version) ([]pkg.Package, error) {
Expand Down Expand Up @@ -62,7 +63,8 @@ func parseYumPackages(rdr io.Reader, minVersion kernel.Version) ([]pkg.Package,
func yumSearch(ctx context.Context, pkg string) (*bytes.Buffer, error) {
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
cmd := exec.CommandContext(ctx, "sudo", "yum", "search", "--showduplicates", pkg)
binary, args := utils.SudoCMD("yum", "search", "--showduplicates", pkg)
cmd := exec.CommandContext(ctx, binary, args...)
cmd.Stdout = stdout
cmd.Stderr = stderr
if err := cmd.Run(); err != nil {
Expand Down
8 changes: 8 additions & 0 deletions pkg/utils/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"debug/elf"
"fmt"
"os"
"os/exec"
"strings"
)
Expand Down Expand Up @@ -34,3 +35,10 @@ func RunCMD(ctx context.Context, cwd string, binary string, args ...string) erro

return nil
}

func SudoCMD(binary string, args ...string) (string, []string) {
if os.Getuid() != 0 {
return "sudo", append([]string{binary}, args...)
}
return binary, args
}

0 comments on commit 6726c4d

Please sign in to comment.