Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pkg/gpio: wrap errors and remove second trim #2858

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions pkg/gpio/gpio_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
baseStr := strings.TrimSpace(string(buf))
num, err := strconv.Atoi(baseStr)
if err != nil {
return 0, fmt.Errorf("could not convert %s contents %s to integer: %v", filename, baseStr, err)
return 0, fmt.Errorf("could not convert %s contents %s to integer: %w", filename, baseStr, err)

Check warning on line 52 in pkg/gpio/gpio_linux.go

View check run for this annotation

Codecov / codecov/patch

pkg/gpio/gpio_linux.go#L52

Added line #L52 was not covered by tests
}
return num, nil
}
Expand All @@ -68,25 +68,25 @@
// Get label (name of the controller)
buf, err := os.ReadFile(filepath.Join(c, "label"))
if err != nil {
return 0, fmt.Errorf("failed to read label of %s: %v", c, err)
return 0, fmt.Errorf("failed to read label of %s: %w", c, err)

Check warning on line 71 in pkg/gpio/gpio_linux.go

View check run for this annotation

Codecov / codecov/patch

pkg/gpio/gpio_linux.go#L71

Added line #L71 was not covered by tests
}
label := strings.TrimSpace(string(buf))

// Check that this is the controller we want
if strings.TrimSpace(label) != controller {
if label != controller {
continue
}

// Get base offset (the first GPIO managed by this chip)
base, err := readInt(filepath.Join(c, "base"))
if err != nil {
return 0, fmt.Errorf("failed to read base: %v", err)
return 0, fmt.Errorf("failed to read base: %w", err)

Check warning on line 83 in pkg/gpio/gpio_linux.go

View check run for this annotation

Codecov / codecov/patch

pkg/gpio/gpio_linux.go#L83

Added line #L83 was not covered by tests
}

// Get the number of GPIOs managed by this chip.
ngpio, err := readInt(filepath.Join(c, "ngpio"))
if err != nil {
return 0, fmt.Errorf("failed to read number of gpios: %v", err)
return 0, fmt.Errorf("failed to read number of gpios: %w", err)

Check warning on line 89 in pkg/gpio/gpio_linux.go

View check run for this annotation

Codecov / codecov/patch

pkg/gpio/gpio_linux.go#L89

Added line #L89 was not covered by tests
}
if int(pin) >= ngpio {
return 0, fmt.Errorf("requested pin %d of controller %s, but controller only has %d pins", pin, controller, ngpio)
Expand All @@ -108,7 +108,7 @@
}
defer outFile.Close()
if _, err := outFile.WriteString(dir); err != nil {
return fmt.Errorf("failed to set gpio %d to %s: %v", pin, dir, err)
return fmt.Errorf("failed to set gpio %d to %s: %w", pin, dir, err)

Check warning on line 111 in pkg/gpio/gpio_linux.go

View check run for this annotation

Codecov / codecov/patch

pkg/gpio/gpio_linux.go#L111

Added line #L111 was not covered by tests
}
return nil
}
Expand All @@ -119,7 +119,7 @@
path := filepath.Join(gpioPath, fmt.Sprintf("gpio%d", pin), "value")
buf, err := os.ReadFile(path)
if err != nil {
return Low, fmt.Errorf("failed to read value of gpio %d: %v", pin, err)
return Low, fmt.Errorf("failed to read value of gpio %d: %w", pin, err)

Check warning on line 122 in pkg/gpio/gpio_linux.go

View check run for this annotation

Codecov / codecov/patch

pkg/gpio/gpio_linux.go#L122

Added line #L122 was not covered by tests
}
switch string(buf) {
case "0\n":
Expand Down