From e4f49be2dfdbd06c3c24e798aba398cf72bff0d7 Mon Sep 17 00:00:00 2001 From: INSEOB KANG Date: Sun, 21 May 2023 17:02:40 +0900 Subject: [PATCH 1/3] mod: change package ioutil->os --- system/fs_mock.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/system/fs_mock.go b/system/fs_mock.go index 5577fb848..197bad026 100644 --- a/system/fs_mock.go +++ b/system/fs_mock.go @@ -2,7 +2,6 @@ package system import ( "fmt" - "io/ioutil" "os" "path" "regexp" @@ -135,7 +134,7 @@ func (fs *MockFilesystem) stat(name string) (os.FileInfo, error) { _, ok := fs.Files[name] if ok { // return file based mock FileInfo - tmpFile, err := ioutil.TempFile("", name) + tmpFile, err := os.CreateTemp("", name) if err != nil { return nil, err } @@ -148,7 +147,7 @@ func (fs *MockFilesystem) stat(name string) (os.FileInfo, error) { for path := range fs.Files { if strings.HasPrefix(path, dirName) { // return dir based mock FileInfo, TempDir don't like "/" in between - tmpDir, err := ioutil.TempDir("", strings.ReplaceAll(name, "/", "_")) + tmpDir, err := os.MkdirTemp("", strings.ReplaceAll(name, "/", "_")) if err != nil { return nil, err } From 11d1aa7de061d207340f9899e1736d8808ba49c1 Mon Sep 17 00:00:00 2001 From: INSEOB KANG Date: Thu, 8 Jun 2023 07:34:59 +0900 Subject: [PATCH 2/3] remain ioutil change io, os packages --- api/robeaux/robeaux.go | 3 +-- cli/generate.go | 3 +-- examples/bebop_ps3_video.go | 3 +-- examples/firmata_travis.go | 4 ++-- platforms/chip/chip_adaptor.go | 6 +++--- platforms/joystick/joystick_driver.go | 4 ++-- platforms/leap/leap_motion_driver_test.go | 6 +++--- platforms/mqtt/mqtt_adaptor.go | 4 ++-- platforms/parrot/bebop/client/examples/video.go | 4 ++-- platforms/particle/adaptor.go | 4 ++-- 10 files changed, 19 insertions(+), 22 deletions(-) diff --git a/api/robeaux/robeaux.go b/api/robeaux/robeaux.go index 709526eb8..59b5242b1 100644 --- a/api/robeaux/robeaux.go +++ b/api/robeaux/robeaux.go @@ -45,7 +45,6 @@ import ( "compress/gzip" "fmt" "io" - "io/ioutil" "os" "path/filepath" "strings" @@ -1014,7 +1013,7 @@ func RestoreAsset(dir, name string) error { if err != nil { return err } - err = ioutil.WriteFile(_filePath(dir, name), data, info.Mode()) + err = os.WriteFile(_filePath(dir, name), data, info.Mode()) if err != nil { return err } diff --git a/cli/generate.go b/cli/generate.go index e70531e91..b3705fe3f 100644 --- a/cli/generate.go +++ b/cli/generate.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "io/ioutil" "os" "strings" "text/template" @@ -149,7 +148,7 @@ func generatePlatform(c config) error { c.dir = dir - exp, err := ioutil.ReadFile(exampleDir + "/main.go") + exp, err := os.ReadFile(exampleDir + "/main.go") if err != nil { return err } diff --git a/examples/bebop_ps3_video.go b/examples/bebop_ps3_video.go index 4f31f373b..74b38b672 100644 --- a/examples/bebop_ps3_video.go +++ b/examples/bebop_ps3_video.go @@ -30,7 +30,6 @@ package main import ( "fmt" "io" - "io/ioutil" "os/exec" "sync/atomic" "time" @@ -70,7 +69,7 @@ func ffmpeg() (stdin io.WriteCloser, stderr io.ReadCloser, err error) { go func() { for { - buf, err := ioutil.ReadAll(stderr) + buf, err := io.ReadAll(stderr) if err != nil { fmt.Println(err) } diff --git a/examples/firmata_travis.go b/examples/firmata_travis.go index 4c836d2a6..970f9c8aa 100644 --- a/examples/firmata_travis.go +++ b/examples/firmata_travis.go @@ -16,7 +16,7 @@ package main import ( "encoding/json" "fmt" - "io/ioutil" + "io" "net/http" "os" "time" @@ -63,7 +63,7 @@ func checkTravis(robot *gobot.Robot) { panic(err) } defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) if err != nil { panic(err) } diff --git a/platforms/chip/chip_adaptor.go b/platforms/chip/chip_adaptor.go index fc6acfc8a..4cc9eda5f 100644 --- a/platforms/chip/chip_adaptor.go +++ b/platforms/chip/chip_adaptor.go @@ -2,7 +2,7 @@ package chip import ( "fmt" - "io/ioutil" + "os" "path/filepath" "strconv" "strings" @@ -119,14 +119,14 @@ func getXIOBase() (baseAddr int, err error) { } for _, labelPath := range labels { - label, err := ioutil.ReadFile(labelPath) + label, err := os.ReadFile(labelPath) if err != nil { return baseAddr, err } if strings.HasPrefix(string(label), expanderID) { expanderPath, _ := filepath.Split(labelPath) basePath := filepath.Join(expanderPath, "base") - base, err := ioutil.ReadFile(basePath) + base, err := os.ReadFile(basePath) if err != nil { return baseAddr, err } diff --git a/platforms/joystick/joystick_driver.go b/platforms/joystick/joystick_driver.go index 85c91c38f..d473bfad3 100644 --- a/platforms/joystick/joystick_driver.go +++ b/platforms/joystick/joystick_driver.go @@ -3,7 +3,7 @@ package joystick import ( "encoding/json" "fmt" - "io/ioutil" + "os" "time" "github.com/veandco/go-sdl2/sdl" @@ -250,7 +250,7 @@ func (j *Driver) findHatName(id uint8, hat uint8, list []hat) string { // loadFile load the joystick config from a .json file func (j *Driver) loadFile() error { - file, e := ioutil.ReadFile(j.configPath) + file, e := os.ReadFile(j.configPath) if e != nil { return e } diff --git a/platforms/leap/leap_motion_driver_test.go b/platforms/leap/leap_motion_driver_test.go index e98d080be..90267ac99 100644 --- a/platforms/leap/leap_motion_driver_test.go +++ b/platforms/leap/leap_motion_driver_test.go @@ -3,7 +3,7 @@ package leap import ( "errors" "io" - "io/ioutil" + "os" "strings" "sync" "testing" @@ -47,7 +47,7 @@ func initTestLeapMotionDriver() (*Driver, *NullReadWriteCloser) { d := NewDriver(a) d.receive = func(ws io.ReadWriteCloser, buf *[]byte) { - file, _ := ioutil.ReadFile("./test/support/example_frame.json") + file, _ := os.ReadFile("./test/support/example_frame.json") copy(*buf, file) } return d, rwc @@ -82,7 +82,7 @@ func TestLeapMotionDriverHalt(t *testing.T) { func TestLeapMotionDriverParser(t *testing.T) { d, _ := initTestLeapMotionDriver() - file, _ := ioutil.ReadFile("./test/support/example_frame.json") + file, _ := os.ReadFile("./test/support/example_frame.json") parsedFrame := d.ParseFrame(file) if parsedFrame.Hands == nil || parsedFrame.Pointables == nil || parsedFrame.Gestures == nil { diff --git a/platforms/mqtt/mqtt_adaptor.go b/platforms/mqtt/mqtt_adaptor.go index 3056ab7d7..9268f2b98 100644 --- a/platforms/mqtt/mqtt_adaptor.go +++ b/platforms/mqtt/mqtt_adaptor.go @@ -3,7 +3,7 @@ package mqtt import ( "crypto/tls" "crypto/x509" - "io/ioutil" + "os" "gobot.io/x/gobot/v2" @@ -204,7 +204,7 @@ func (a *Adaptor) newTLSConfig() *tls.Config { var certpool *x509.CertPool if len(a.ServerCert()) > 0 { certpool = x509.NewCertPool() - pemCerts, err := ioutil.ReadFile(a.ServerCert()) + pemCerts, err := os.ReadFile(a.ServerCert()) if err == nil { certpool.AppendCertsFromPEM(pemCerts) } diff --git a/platforms/parrot/bebop/client/examples/video.go b/platforms/parrot/bebop/client/examples/video.go index f9d0934a9..3946c42c2 100644 --- a/platforms/parrot/bebop/client/examples/video.go +++ b/platforms/parrot/bebop/client/examples/video.go @@ -25,7 +25,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "os/exec" "time" @@ -73,7 +73,7 @@ func main() { go func() { for { - buf, err := ioutil.ReadAll(ffmpegErr) + buf, err := io.ReadAll(ffmpegErr) if err != nil { fmt.Println(err) } diff --git a/platforms/particle/adaptor.go b/platforms/particle/adaptor.go index a291efca6..aed5da0d7 100644 --- a/platforms/particle/adaptor.go +++ b/platforms/particle/adaptor.go @@ -4,7 +4,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "net/url" "strconv" @@ -262,7 +262,7 @@ func (s *Adaptor) request(method string, url string, params url.Values) (m map[s return } - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) if err != nil { return From fa339b1f9a97dc3d7c29b4fd3ab2cec39b6b747d Mon Sep 17 00:00:00 2001 From: dlstjq7685 <31182898+dlstjq7685@users.noreply.github.com> Date: Sat, 10 Jun 2023 21:44:22 +0900 Subject: [PATCH 3/3] wrong change file --- cli/generate.go | 417 ------------------------------------------------ 1 file changed, 417 deletions(-) delete mode 100644 cli/generate.go diff --git a/cli/generate.go b/cli/generate.go deleted file mode 100644 index e70531e91..000000000 --- a/cli/generate.go +++ /dev/null @@ -1,417 +0,0 @@ -package main - -import ( - "fmt" - "io/ioutil" - "os" - "strings" - "text/template" - - "github.com/urfave/cli" -) - -type config struct { - Package string - Name string - UpperName string - FirstLetter string - Example string - dir string -} - -func Generate() cli.Command { - return cli.Command{ - Name: "generate", - Usage: "Generate new Gobot adaptors, drivers, and platforms", - Action: func(c *cli.Context) { - valid := false - for _, s := range []string{"adaptor", "driver", "platform"} { - if s == c.Args().First() { - valid = true - } - } - if !valid { - fmt.Println("Invalid/no subcommand supplied.") - fmt.Println("Usage:") - fmt.Println(" gobot generate adaptor [package] # generate a new Gobot adaptor") - fmt.Println(" gobot generate driver [package] # generate a new Gobot driver") - fmt.Println(" gobot generate platform [package] # generate a new Gobot platform") - return - } - - if len(c.Args()) < 2 { - fmt.Println("Please provide a one word name.") - return - } - - name := strings.ToLower(c.Args()[1]) - packageName := name - if len(c.Args()) > 2 { - packageName = strings.ToLower(c.Args()[2]) - } - upperName := strings.ToUpper(string(name[0])) + string(name[1:]) - - cfg := config{ - Package: packageName, - UpperName: upperName, - Name: name, - FirstLetter: string(name[0]), - dir: ".", - } - - switch c.Args().First() { - case "adaptor": - if err := generateAdaptor(cfg); err != nil { - fmt.Println(err) - } - case "driver": - if err := generateDriver(cfg); err != nil { - fmt.Println(err) - } - case "platform": - pwd, err := os.Getwd() - if err != nil { - fmt.Println(err) - return - } - dir := pwd + "/" + cfg.Name - fmt.Println("Creating", dir) - if err := os.MkdirAll(dir, 0700); err != nil { - fmt.Println(err) - return - } - cfg.dir = dir - - examplesDir := dir + "/examples" - fmt.Println("Creating", examplesDir) - if err := os.MkdirAll(examplesDir, 0700); err != nil { - fmt.Println(err) - return - } - - if err := generatePlatform(cfg); err != nil { - fmt.Println(err) - } - } - }, - } -} - -func generate(c config, file string, tmpl string) error { - fileLocation := c.dir + "/" + file - fmt.Println("Creating", fileLocation) - - f, err := os.Create(fileLocation) - defer f.Close() //nolint:staticcheck // for historical reasons - if err != nil { - return err - } - - t, err := template.New("").Parse(tmpl) - if err != nil { - return err - } - - return t.Execute(f, c) -} - -func generateDriver(c config) error { - if err := generate(c, c.Name+"_driver.go", driver()); err != nil { - return err - } - - return generate(c, c.Name+"_driver_test.go", driverTest()) -} - -func generateAdaptor(c config) error { - if err := generate(c, c.Name+"_adaptor.go", adaptor()); err != nil { - return err - } - - return generate(c, c.Name+"_adaptor_test.go", adaptorTest()) -} - -func generatePlatform(c config) error { - if err := generateDriver(c); err != nil { - return err - } - if err := generateAdaptor(c); err != nil { - return err - } - - dir := c.dir - exampleDir := dir + "/examples" - c.dir = exampleDir - - if err := generate(c, "main.go", example()); err != nil { - return err - } - - c.dir = dir - - exp, err := ioutil.ReadFile(exampleDir + "/main.go") - if err != nil { - return err - } - c.Example = string(exp) - - return generate(c, "README.md", readme()) -} - -func adaptor() string { - return `package {{.Package}} - -type {{.UpperName}}Adaptor struct { - name string -} - -func New{{.UpperName}}Adaptor() *{{.UpperName}}Adaptor { - return &{{.UpperName}}Adaptor{ - name: "{{.UpperName}}", - } -} - -func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Name() string { return {{.FirstLetter}}.name } - -func ({{.FirstLetter}} *{{.UpperName}}Adaptor) SetName(name string) { {{.FirstLetter}}.name = name } - -func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Connect() error { return nil } - -func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Finalize() error { return nil } - -func ({{.FirstLetter}} *{{.UpperName}}Adaptor) Ping() string { return "pong" } -` -} - -func driver() string { - return `package {{.Package}} - -import ( - "time" - - "gobot.io/x/gobot/v2" -) - -const Hello string = "hello" - -type {{.UpperName}}Driver struct { - name string - connection gobot.Connection - interval time.Duration - halt chan bool - gobot.Eventer - gobot.Commander -} - -func New{{.UpperName}}Driver(a *{{.UpperName}}Adaptor) *{{.UpperName}}Driver { - {{.FirstLetter}} := &{{.UpperName}}Driver{ - name: "{{.UpperName}}", - connection: a, - interval: 500*time.Millisecond, - halt: make(chan bool, 0), - Eventer: gobot.NewEventer(), - Commander: gobot.NewCommander(), - } - - {{.FirstLetter}}.AddEvent(Hello) - - {{.FirstLetter}}.AddCommand(Hello, func(params map[string]interface{}) interface{} { - return {{.FirstLetter}}.Hello() - }) - - return {{.FirstLetter}} -} - -func ({{.FirstLetter}} *{{.UpperName}}Driver) Name() string { return {{.FirstLetter}}.name } - -func ({{.FirstLetter}} *{{.UpperName}}Driver) SetName(name string) { {{.FirstLetter}}.name = name } - -func ({{.FirstLetter}} *{{.UpperName}}Driver) Connection() gobot.Connection { - return {{.FirstLetter}}.connection -} - -func ({{.FirstLetter}} *{{.UpperName}}Driver) adaptor() *{{.UpperName}}Adaptor { - return {{.FirstLetter}}.Connection().(*{{.UpperName}}Adaptor) -} - -func ({{.FirstLetter}} *{{.UpperName}}Driver) Hello() string { - return "hello from " + {{.FirstLetter}}.Name() + "!" -} - -func ({{.FirstLetter}} *{{.UpperName}}Driver) Ping() string { - return {{.FirstLetter}}.adaptor().Ping() -} - -func ({{.FirstLetter}} *{{.UpperName}}Driver) Start() error { - go func() { - for { - {{.FirstLetter}}.Publish({{.FirstLetter}}.Event(Hello), {{.FirstLetter}}.Hello()) - - select { - case <- time.After({{.FirstLetter}}.interval): - case <- {{.FirstLetter}}.halt: - return - } - } - }() - return nil -} - -func ({{.FirstLetter}} *{{.UpperName}}Driver) Halt() error { - {{.FirstLetter}}.halt <- true - return nil -} - -` -} - -func example() string { - return ` -package main - -import ( - "../" - "fmt" - "time" - - "gobot.io/x/gobot/v2" -) - -func main() { - gbot := gobot.NewMaster() - - conn := {{.Package}}.New{{.UpperName}}Adaptor() - dev := {{.Package}}.New{{.UpperName}}Driver(conn) - - work := func() { - dev.On(dev.Event({{.Package}}.Hello), func(data interface{}) { - fmt.Println(data) - }) - - gobot.Every(1200*time.Millisecond, func() { - fmt.Println(dev.Ping()) - }) - } - - robot := gobot.NewRobot( - "robot", - []gobot.Connection{conn}, - []gobot.Device{dev}, - work, - ) - - gbot.AddRobot(robot) - gbot.Start() -} -` -} - -func driverTest() string { - return `package {{.Package}} - -import ( - "testing" - "time" - - "gobot.io/x/gobot/v2" - "gobot.io/x/gobot/v2/gobottest" -) - -var _ gobot.Driver = (*{{.UpperName}}Driver)(nil) - -func Test{{.UpperName}}Driver(t *testing.T) { - d := New{{.UpperName}}Driver(New{{.UpperName}}Adaptor()) - - gobottest.Assert(t, d.Name(), "{{.UpperName}}") - gobottest.Assert(t, d.Connection().Name(), "{{.UpperName}}") - - ret := d.Command(Hello)(nil) - gobottest.Assert(t, ret.(string), "hello from {{.UpperName}}!") - - gobottest.Assert(t, d.Ping(), "pong") - - gobottest.Assert(t, len(d.Start()), 0) - - time.Sleep(d.interval) - - sem := make(chan bool, 0) - - d.On(d.Event(Hello), func(data interface{}) { - sem <- true - }) - - select { - case <-sem: - case <-time.After(600 * time.Millisecond): - t.Errorf("Hello Event was not published") - } - - gobottest.Assert(t, len(d.Halt()), 0) - - d.On(d.Event(Hello), func(data interface{}) { - sem <- true - }) - - select { - case <-sem: - t.Errorf("Hello Event should not publish after Halt") - case <-time.After(600 * time.Millisecond): - } -} - -` -} - -func adaptorTest() string { - return `package {{.Package}} - -import ( - "testing" - - "gobot.io/x/gobot/v2" - "gobot.io/x/gobot/v2/gobottest" -) - -var _ gobot.Adaptor = (*{{.UpperName}}Adaptor)(nil) - -func Test{{.UpperName}}Adaptor(t *testing.T) { - a := New{{.UpperName}}Adaptor() - - gobottest.Assert(t, a.Name(), "{{.UpperName}}") - - gobottest.Assert(t, len(a.Connect()), 0) - - gobottest.Assert(t, a.Ping(), "pong") - - gobottest.Assert(t, len(a.Connect()), 0) - - gobottest.Assert(t, len(a.Finalize()), 0) -} -` -} - -func readme() string { - return `# {{.Package}} - -Gobot (http://gobot.io/) is a framework for robotics and physical computing using Go - -This repository contains the Gobot adaptor and driver for {{.Package}}. - -For more information about Gobot, check out the github repo at -https://gobot.io/x/gobot/v2 - -## Installing -` + "```bash\ngo get path/to/repo/{{.Package}}\n```" + ` - -## Using -` + "```go{{.Example}}\n```" + ` - -## Connecting - -Explain how to connect to the device here... - -## License - -Copyright (c) 2018 . Licensed under the license. -` -}