Skip to content

Commit

Permalink
Merge pull request #288 from fromanirh/static-check-lanes
Browse files Browse the repository at this point in the history
add lane with static checks
  • Loading branch information
jaypipes authored Nov 19, 2021
2 parents a6b3926 + 1979b8e commit 004673d
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 60 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,34 @@ on:

# see: https://docs.github.com/en/actions/using-github-hosted-runners/about-github-hosted-runners
jobs:
# tier 0: system-independent checks
format:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: set up golang
uses: actions/setup-go@v2
with:
go-version: 1.16

- name: format
run: ./hack/check-format.sh

lint:
runs-on: ubuntu-20.04
steps:
- name: Check out code
uses: actions/checkout@v2

- name: Verify
uses: golangci/golangci-lint-action@v2
with:
version: v1.41.1
args: --timeout=15m0s --verbose

# tier-1
# main development platfotm, gets features first and it's most tested
build-ubuntu-2004:
Expand Down
8 changes: 4 additions & 4 deletions alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import (
type WithOption = option.Option

var (
WithChroot = option.WithChroot
WithSnapshot = option.WithSnapshot
WithAlerter = option.WithAlerter
WithNullAlerter = option.WithNullAlerter
WithChroot = option.WithChroot
WithSnapshot = option.WithSnapshot
WithAlerter = option.WithAlerter
WithNullAlerter = option.WithNullAlerter
// match the existing environ variable to minimize surprises
WithDisableWarnings = option.WithNullAlerter
WithDisableTools = option.WithDisableTools
Expand Down
35 changes: 20 additions & 15 deletions cmd/ghw-snapshot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ import (
)

var (
// version of application at compile time (-X 'main.version=$(VERSION)').
version = "(Unknown Version)"
// buildHash GIT hash of application at compile time (-X 'main.buildHash=$(GITCOMMIT)').
buildHash = "No Git-hash Provided."
// buildDate of application at compile time (-X 'main.buildDate=$(BUILDDATE)').
buildDate = "No Build Date Provided."
// show debug output
debug = false
// output filepath to save snapshot to
Expand All @@ -47,19 +41,25 @@ func trace(msg string, args ...interface{}) {
fmt.Printf(msg, args...)
}

func systemFingerprint() string {
func systemFingerprint() (string, error) {
hn, err := os.Hostname()
if err != nil {
return "unknown"
return "unknown", err
}
m := md5.New()
io.WriteString(m, hn)
return fmt.Sprintf("%x", m.Sum(nil))
_, err = io.WriteString(m, hn)
if err != nil {
return "unknown", err
}
return fmt.Sprintf("%x", m.Sum(nil)), nil
}

func defaultOutPath() string {
fp := systemFingerprint()
return fmt.Sprintf("%s-%s-%s.tar.gz", runtime.GOOS, runtime.GOARCH, fp)
func defaultOutPath() (string, error) {
fp, err := systemFingerprint()
if err != nil {
return "unknown", err
}
return fmt.Sprintf("%s-%s-%s.tar.gz", runtime.GOOS, runtime.GOARCH, fp), nil
}

func execute(cmd *cobra.Command, args []string) error {
Expand All @@ -75,15 +75,20 @@ func execute(cmd *cobra.Command, args []string) error {
}

if outPath == "" {
outPath = defaultOutPath()
outPath, err = defaultOutPath()
if err != nil {
return err
}
trace("using default output filepath %s\n", outPath)
}

return snapshot.PackFrom(outPath, scratchDir)
}

func main() {
rootCmd.Execute()
if err := rootCmd.Execute(); err != nil {
trace("execution failed: %v\n", err)
}
}

func init() {
Expand Down
9 changes: 9 additions & 0 deletions hack/check-format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -eu

DIFF=$( gofmt -s -d . )
if [ -n "${DIFF}" ]; then
echo "${DIFF}"
exit 1
fi
exit 0
22 changes: 2 additions & 20 deletions pkg/block/block_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func diskPartUUID(ctx *context.Context, part string) string {
return ""
}

if out == nil || len(out) == 0 {
if len(out) == 0 {
return ""
}

Expand All @@ -249,10 +249,7 @@ func diskIsRemovable(paths *linuxpath.Paths, disk string) bool {
return false
}
removable := strings.TrimSpace(string(contents))
if removable == "1" {
return true
}
return false
return removable == "1"
}

func disks(ctx *context.Context, paths *linuxpath.Paths) []*Disk {
Expand Down Expand Up @@ -457,18 +454,3 @@ func parseMountEntry(line string) *mountEntry {
res.Options = opts
return res
}

func partitionMountPoint(paths *linuxpath.Paths, part string) string {
mp, _, _ := partitionInfo(paths, part)
return mp
}

func partitionType(paths *linuxpath.Paths, part string) string {
_, pt, _ := partitionInfo(paths, part)
return pt
}

func partitionIsReadOnly(paths *linuxpath.Paths, part string) bool {
_, _, ro := partitionInfo(paths, part)
return ro
}
7 changes: 6 additions & 1 deletion pkg/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ func (ctx *Context) Do(fn func() error) error {
if err != nil {
return err
}
defer ctx.Teardown()
defer func() {
err := ctx.Teardown()
if err != nil {
ctx.Warn("teardown error: %v", err)
}
}()
return fn()
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/gpu/gpu_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ func TestGPUWithoutNUMANodeInfo(t *testing.T) {
if err != nil {
t.Fatalf("Unable to unpack %q into %q: %v", workstationSnapshot, tmpRoot, err)
}
defer snapshot.Cleanup(tmpRoot)
defer func() {
_ = snapshot.Cleanup(tmpRoot)
}()

err = os.Remove(filepath.Join(tmpRoot, "/sys/class/drm/card0/device/numa_node"))
if err != nil && !errors.Is(err, os.ErrNotExist) {
Expand Down
7 changes: 0 additions & 7 deletions pkg/pci/pci.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package pci
import (
"encoding/json"
"fmt"
"regexp"

"github.com/jaypipes/pcidb"

Expand All @@ -27,12 +26,6 @@ type Address pciaddr.Address
// backward compatibility, to be removed in 1.0.0
var AddressFromString = pciaddr.FromString

var (
regexAddress *regexp.Regexp = regexp.MustCompile(
`^(([0-9a-f]{0,4}):)?([0-9a-f]{2}):([0-9a-f]{2})\.([0-9a-f]{1})$`,
)
)

type Device struct {
// The PCI address of the device
Address string `json:"address"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/pci/pci_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestPCIDeviceRevision(t *testing.T) {
},
}
for _, tCase := range tCases {
t.Run(fmt.Sprintf("%s", tCase.addr), func(t *testing.T) {
t.Run(tCase.addr, func(t *testing.T) {
dev := info.GetDevice(tCase.addr)
if dev == nil {
t.Fatalf("got nil device for address %q", tCase.addr)
Expand Down
5 changes: 1 addition & 4 deletions pkg/snapshot/clonetree_net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ func ExpectedCloneNetContent() []string {
}

filterLink := func(linkDest string) bool {
if strings.Contains(linkDest, "devices/virtual/net") {
return false
}
return true
return !strings.Contains(linkDest, "devices/virtual/net")
}

return cloneContentByClass("net", ifaceEntries, filterNone, filterLink)
Expand Down
3 changes: 2 additions & 1 deletion pkg/snapshot/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ func PackWithWriter(fw io.Writer, sourceRoot string) error {
}

func createSnapshot(tw *tar.Writer, buildDir string) error {
return filepath.Walk(buildDir, func(path string, fi os.FileInfo, err error) error {
return filepath.Walk(buildDir, func(path string, fi os.FileInfo, _ error) error {
if path == buildDir {
return nil
}
var link string
var err error

if fi.Mode()&os.ModeSymlink != 0 {
trace("processing symlink %s\n", path)
Expand Down
4 changes: 1 addition & 3 deletions pkg/snapshot/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ package snapshot
var trace func(msg string, args ...interface{})

func init() {
trace = func(msg string, args ...interface{}) {
return
}
trace = func(msg string, args ...interface{}) {}
}

func SetTraceFunction(fn func(msg string, args ...interface{})) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/topology/topology_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func distancesForNode(ctx *context.Context, nodeID int) ([]int, error) {
}

items := strings.Fields(strings.TrimSpace(string(data)))
dists := make([]int, len(items), len(items)) // TODO: can a NUMA cell be offlined?
dists := make([]int, len(items)) // TODO: can a NUMA cell be offlined?
for idx, item := range items {
dist, err := strconv.Atoi(item)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
)

const (
UNKNOWN = "unknown"
disableWarningsEnv = "GHW_DISABLE_WARNINGS"
UNKNOWN = "unknown"
)

type closer interface {
Expand Down

0 comments on commit 004673d

Please sign in to comment.