Upgraded do to 1.23 to get errors.Join function

This commit is contained in:
Foil-hat-guy 2025-05-08 20:42:53 +03:00
parent 6a88132acf
commit f1cbf9c85d
No known key found for this signature in database
GPG key ID: 221CC305A7B23591
4 changed files with 13 additions and 37 deletions

View file

@ -4,32 +4,6 @@ import (
"fmt"
)
func filterNilErrors(errs []error) (notNilErrs []error) {
for _, err := range errs {
if err != nil {
notNilErrs = append(notNilErrs, err)
}
}
return notNilErrs
}
func Join(errs ...error) (result error) {
errs = filterNilErrors(errs)
if len(errs) == 0 {
return nil
}
for index, err := range errs {
if index == 0 {
result = err
} else {
result = fmt.Errorf("%w: %v", result, err)
}
}
return result
}
func ERR_NOT_TTY() error {
return fmt.Errorf("File is not a tty")
}

6
go.mod
View file

@ -1,8 +1,8 @@
module code.foilhatguy.casa/pub/stty
go 1.19
go 1.23
require (
golang.org/x/sys v0.15.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/sys v0.15.0
golang.org/x/term v0.15.0
)

View file

@ -1,8 +1,9 @@
package stty
import(
"os/exec"
import (
"errors"
"fmt"
"os/exec"
)
type real struct {
@ -12,7 +13,7 @@ func (r *real) SetBaudRate(portPath string, baudrate int) error {
setBaudRateCMD := exec.Command("stty", "-F", portPath, fmt.Sprint(baudrate))
_, err := setBaudRateCMD.Output()
if err != nil {
return Join(ERR_SET_BAUDRATE_FAILED(), err)
return errors.Join(ERR_SET_BAUDRATE_FAILED(), err)
}
return nil
}
@ -39,7 +40,7 @@ func (r *real) TestBaudRate(portPath string, baudrate int) error {
newBaudRate, err := r.CheckBaudRate(portPath)
if err != nil {
return Join(ERR_CHECK_BAUDRATE_FAILED(), err)
return errors.Join(ERR_CHECK_BAUDRATE_FAILED(), err)
}
if newBaudRate == baudrate {

View file

@ -1,6 +1,7 @@
package stty
import (
"errors"
"fmt"
"os"
"os/exec"
@ -25,7 +26,7 @@ func init() {
checkSttyCMD := exec.Command("stty", "--version")
sttyOutput, err := checkSttyCMD.Output()
if err != nil || len(sttyOutput) == 0 {
cmd = &dummy{Join(ERR_STTY_MISSING(),err)}
cmd = &dummy{errors.Join(ERR_STTY_MISSING(),err)}
}
cmd = &real{}
}
@ -33,13 +34,13 @@ func init() {
func ValidateTtyFile(filepath string) error {
file, err := os.Open(filepath)
if err != nil {
return Join(ERR_CANNOT_OPEN_FILE(), fmt.Errorf(err.Error()))
return errors.Join(ERR_CANNOT_OPEN_FILE(), fmt.Errorf(err.Error()))
}
defer file.Close()
err = unix.Access(filepath, unix.W_OK)
if err != nil {
return Join(ERR_NO_WRITE_ACCESS(), err)
return errors.Join(ERR_NO_WRITE_ACCESS(), err)
}
if term.IsTerminal(int(file.Fd())) {