Validate tty file name function is added.
This commit is contained in:
parent
0068c92a7b
commit
9acae0ee0c
3 changed files with 35 additions and 5 deletions
5
go.mod
5
go.mod
|
@ -1,3 +1,8 @@
|
|||
module code.foilhatguy.casa/pub/stty
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
golang.org/x/sys v0.15.0 // indirect
|
||||
golang.org/x/term v0.15.0 // indirect
|
||||
)
|
||||
|
|
4
go.sum
Normal file
4
go.sum
Normal file
|
@ -0,0 +1,4 @@
|
|||
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
|
||||
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4=
|
||||
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
|
31
stty.go
31
stty.go
|
@ -2,12 +2,33 @@ package stty
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"golang.org/x/sys/unix"
|
||||
"golang.org/x/term"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
type stty struct {
|
||||
}
|
||||
|
||||
func ValidateTtyFile(filepath string) error {
|
||||
file, err := os.Open(filepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
err = unix.Access(filepath, unix.W_OK)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if term.IsTerminal(int(file.Fd())) {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("File is not a tty.")
|
||||
}
|
||||
|
||||
func Get() (*stty, error) {
|
||||
checkSttyCMD := exec.Command("stty", "--version")
|
||||
sttyOutput, err := checkSttyCMD.Output()
|
||||
|
@ -37,19 +58,19 @@ func (s *stty) CheckBaudRate(portPath string) (baudrate int, err error) {
|
|||
return baudrate, nil
|
||||
}
|
||||
|
||||
func (s *stty) TestBaudRate(portPath string, baudrate int) (bool, error) {
|
||||
func (s *stty) TestBaudRate(portPath string, baudrate int) (error) {
|
||||
err := s.SetBaudRate(portPath, baudrate)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("Failed to set baudrate using stty. Because: " + err.Error())
|
||||
return fmt.Errorf("Failed to set baudrate using stty. Because: " + err.Error())
|
||||
}
|
||||
|
||||
newBaudRate, err := s.CheckBaudRate(portPath)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("Failed to check baudrate using stty. Because: " + err.Error())
|
||||
return fmt.Errorf("Failed to check baudrate using stty. Because: " + err.Error())
|
||||
}
|
||||
|
||||
if newBaudRate == baudrate {
|
||||
return true, nil
|
||||
return nil
|
||||
}
|
||||
return false, nil
|
||||
return fmt.Errorf("Selected baudrate is not supported.")
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue