aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Dessaux2021-11-17 10:30:17 +0100
committerJulien Dessaux2021-11-17 10:30:17 +0100
commiteff9c0c8c6d32c2060841a9b9b98d9d4b38540b2 (patch)
treeaf77a8d0867dc11dfa9130a0bba509b6b55b8246
parentRefactored package structure (diff)
downloadshell-game-launcher-eff9c0c8c6d32c2060841a9b9b98d9d4b38540b2.tar.gz
shell-game-launcher-eff9c0c8c6d32c2060841a9b9b98d9d4b38540b2.tar.bz2
shell-game-launcher-eff9c0c8c6d32c2060841a9b9b98d9d4b38540b2.zip
Refactored to replace github pkg errors dependencies with fmt.ErrorfHEADmaster
-rw-r--r--go.mod1
-rw-r--r--go.sum2
-rw-r--r--pkg/client/input.go5
-rw-r--r--pkg/config/action.go23
-rw-r--r--pkg/config/app.go14
-rw-r--r--pkg/config/command.go13
-rw-r--r--pkg/config/config.go12
-rw-r--r--pkg/config/game.go18
-rw-r--r--pkg/config/menu.go27
9 files changed, 54 insertions, 61 deletions
diff --git a/go.mod b/go.mod
index 1bc7123..1b68bd1 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,6 @@ module shell-game-launcher
go 1.17
require (
- github.com/pkg/errors v0.9.1
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
)
diff --git a/go.sum b/go.sum
index 846bb3d..383a646 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,3 @@
-github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
-github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c h1:DHcbWVXeY+0Y8HHKR+rbLwnoh2F4tNCY7rTiHJ30RmA=
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
diff --git a/pkg/client/input.go b/pkg/client/input.go
index 8c814a6..bcdab13 100644
--- a/pkg/client/input.go
+++ b/pkg/client/input.go
@@ -2,9 +2,8 @@ package client
import (
"bufio"
+ "fmt"
"os"
-
- "github.com/pkg/errors"
)
// getValidInput returns the selected menu command as a string or an error
@@ -15,7 +14,7 @@ func (s *State) getValidInput() (string, error) {
for {
input, err := reader.ReadByte()
if err != nil {
- return "", errors.Wrapf(err, "Could not read byte from stdin")
+ return "", fmt.Errorf("Could not read byte from stdin : %w", err)
}
for _, menuEntry := range menu.MenuEntries {
if []byte(menuEntry.Key)[0] == input {
diff --git a/pkg/config/action.go b/pkg/config/action.go
index c63308e..4693040 100644
--- a/pkg/config/action.go
+++ b/pkg/config/action.go
@@ -1,9 +1,8 @@
package config
import (
+ "fmt"
"strings"
-
- "github.com/pkg/errors"
)
func validateAction(action string) error {
@@ -11,44 +10,44 @@ func validateAction(action string) error {
switch tokens[0] {
case "chmail":
if len(tokens) != 1 {
- return errors.New("chmail action takes no arguments")
+ return fmt.Errorf("chmail action takes no arguments")
}
case "login":
if len(tokens) != 1 {
- return errors.New("login action takes no arguments")
+ return fmt.Errorf("login action takes no arguments")
}
case "menu":
if len(tokens) != 2 {
- return errors.New("menu action takes exactly one argument")
+ return fmt.Errorf("menu action takes exactly one argument")
}
// menu existence is tested in global config
case "passwd":
if len(tokens) != 1 {
- return errors.New("passwd action takes no arguments")
+ return fmt.Errorf("passwd action takes no arguments")
}
case "play":
if len(tokens) != 2 {
- return errors.New("play action takes exactly one argument")
+ return fmt.Errorf("play action takes exactly one argument")
}
// game existence is tested in global config
case "register":
if len(tokens) != 1 {
- return errors.New("register action takes no arguments")
+ return fmt.Errorf("register action takes no arguments")
}
case "replay":
if len(tokens) != 1 {
- return errors.New("replay action takes no arguments")
+ return fmt.Errorf("replay action takes no arguments")
}
case "watch":
if len(tokens) != 1 {
- return errors.New("watch action takes no arguments")
+ return fmt.Errorf("watch action takes no arguments")
}
case "quit":
if len(tokens) != 1 {
- return errors.New("quit action takes no arguments")
+ return fmt.Errorf("quit action takes no arguments")
}
default:
- return errors.New("Invalid action : " + tokens[0])
+ return fmt.Errorf("Invalid action : " + tokens[0])
}
return nil
}
diff --git a/pkg/config/app.go b/pkg/config/app.go
index 2787517..d7024c7 100644
--- a/pkg/config/app.go
+++ b/pkg/config/app.go
@@ -1,9 +1,9 @@
package config
import (
+ "fmt"
"os"
- "github.com/pkg/errors"
"golang.org/x/sys/unix"
)
@@ -27,28 +27,28 @@ type App struct {
func (a *App) validate() error {
// WorkingDirectory
if err := os.MkdirAll(a.WorkingDirectory, 0700); err != nil {
- return errors.Wrapf(err, "Invalid WorkingDirectory : %s", a.WorkingDirectory)
+ return fmt.Errorf("Invalid WorkingDirectory %s : %w", a.WorkingDirectory, err)
}
if err := unix.Access(a.WorkingDirectory, unix.W_OK|unix.R_OK|unix.X_OK); err != nil {
- return errors.Wrapf(err, "invalid WorkingDirectory : %s", a.WorkingDirectory)
+ return fmt.Errorf("invalid WorkingDirectory %s : %w", a.WorkingDirectory, err)
}
// MaxUsers
if a.MaxUsers <= 0 {
- return errors.New("MaxUsers must be a positive integer")
+ return fmt.Errorf("MaxUsers must be a positive integer")
}
// AllowRegistration is just a bool, nothing to validate
// MaxNickLen
if a.MaxNickLen <= 0 {
- return errors.New("MaxNickLen must be a positive integer")
+ return fmt.Errorf("MaxNickLen must be a positive integer")
}
// MenuMaxIdleTime
if a.MenuMaxIdleTime <= 0 {
- return errors.New("MenuMaxIdleTime must be a positive integer")
+ return fmt.Errorf("MenuMaxIdleTime must be a positive integer")
}
// PostLoginCommands
for i := 0; i < len(a.PostLoginCommands); i++ {
if err := validateCommand(a.PostLoginCommands[i]); err != nil {
- return errors.Wrap(err, "Failed to validate PostLoginCommands")
+ return fmt.Errorf("Failed to validate PostLoginCommands : %w", err)
}
}
return nil
diff --git a/pkg/config/command.go b/pkg/config/command.go
index 34142cd..ad19142 100644
--- a/pkg/config/command.go
+++ b/pkg/config/command.go
@@ -1,9 +1,8 @@
package config
import (
+ "fmt"
"strings"
-
- "github.com/pkg/errors"
)
func validateCommand(cmd string) error {
@@ -11,22 +10,22 @@ func validateCommand(cmd string) error {
switch tokens[0] {
case "cp":
if len(tokens) != 3 {
- return errors.New("cp command takes exactly two arguments")
+ return fmt.Errorf("cp command takes exactly two arguments")
}
case "exec":
if len(tokens) <= 1 {
- return errors.New("exec command needs arguments")
+ return fmt.Errorf("exec command needs arguments")
}
case "mkdir":
if len(tokens) != 2 {
- return errors.New("mkdir command takes exactly one argument")
+ return fmt.Errorf("mkdir command takes exactly one argument")
}
case "wait":
if len(tokens) != 1 {
- return errors.New("wait command takes no arguments")
+ return fmt.Errorf("wait command takes no arguments")
}
default:
- return errors.New("Invalid command : " + tokens[0])
+ return fmt.Errorf("Invalid command : " + tokens[0])
}
return nil
}
diff --git a/pkg/config/config.go b/pkg/config/config.go
index b68a26a..b363a0c 100644
--- a/pkg/config/config.go
+++ b/pkg/config/config.go
@@ -1,9 +1,9 @@
package config
import (
+ "fmt"
"os"
- "github.com/pkg/errors"
"gopkg.in/yaml.v3"
)
@@ -23,7 +23,7 @@ func (c *Config) validate() error {
}
// Menus
if len(c.Menus) < 2 {
- return errors.New("A valid configuration needs at least two menu entries named anonymous and logged_in")
+ return fmt.Errorf("A valid configuration needs at least two menu entries named anonymous and logged_in")
}
for k, v := range c.Menus {
if err := v.validate(k); err != nil {
@@ -44,20 +44,20 @@ func LoadFile(path string) (*Config, error) {
var c *Config
f, errOpen := os.Open(path)
if errOpen != nil {
- return nil, errors.Wrapf(errOpen, "Failed to open configuration file %s", path)
+ return nil, fmt.Errorf("Failed to open configuration file %s : %w", path, errOpen)
}
defer f.Close()
decoder := yaml.NewDecoder(f)
if err := decoder.Decode(&c); err != nil {
- return nil, errors.Wrap(err, "Failed to decode configuration file")
+ return nil, fmt.Errorf("Failed to decode configuration file : %w", err)
}
if err := c.validate(); err != nil {
- return nil, errors.Wrap(err, "Failed to validate configuration")
+ return nil, fmt.Errorf("Failed to validate configuration : %w", err)
}
// If all looks good we validate menu consistency
for _, v := range c.Menus {
if err := v.validateConsistency(c); err != nil {
- return nil, errors.Wrap(err, "Failed menu consistency checks")
+ return nil, fmt.Errorf("Failed menu consistency checks : %w", err)
}
}
return c, nil
diff --git a/pkg/config/game.go b/pkg/config/game.go
index b5f4d39..5326073 100644
--- a/pkg/config/game.go
+++ b/pkg/config/game.go
@@ -1,9 +1,9 @@
package config
import (
+ "fmt"
"regexp"
- "github.com/pkg/errors"
"golang.org/x/sys/unix"
)
@@ -26,23 +26,23 @@ type Game struct {
func (g *Game) validate(name string) error {
// Game name
if ok := reValidGameName.MatchString(name); !ok {
- return errors.New("Invalid Game name, must match regex `^[\\w\\._]+$` : " + name)
+ return fmt.Errorf("Invalid Game name, must match regex `^[\\w\\._]+$` : " + name)
}
// ChrootPath TODO
if err := unix.Access(g.ChrootPath, unix.R_OK|unix.X_OK); err != nil {
- return errors.Wrapf(err, "Invalid ChrootPath : %s", g.ChrootPath)
+ return fmt.Errorf("Invalid ChrootPath %s : %w", g.ChrootPath, err)
}
// FileMode
if ok := reValidFileMode.MatchString(g.FileMode); !ok {
- return errors.New("Invalid File Mode, must match regex `^0[\\d]{3}$` : " + name)
+ return fmt.Errorf("Invalid File Mode, must match regex `^0[\\d]{3}$` : " + name)
}
// Commands
if len(g.Commands) == 0 {
- return errors.New("Invalid game " + name + " has no commands")
+ return fmt.Errorf("Invalid game " + name + " has no commands")
}
for i := 0; i < len(g.Commands); i++ {
if err := validateCommand(g.Commands[i]); err != nil {
- return errors.Wrapf(err, "Failed to validate Commands for game %s", name)
+ return fmt.Errorf("Failed to validate Commands for game %s : %w", name, err)
}
}
// Env
@@ -50,12 +50,12 @@ func (g *Game) validate(name string) error {
for _, c := range k {
switch c {
case '=':
- return errors.New("Environment variable key must not contain equal sign")
+ return fmt.Errorf("Environment variable key must not contain equal sign")
case '\000':
- return errors.New("Environment variable key must not contain null character")
+ return fmt.Errorf("Environment variable key must not contain null character")
}
if reSpace.MatchString(string(c)) {
- return errors.New("Environment variable key must not contain spaces")
+ return fmt.Errorf("Environment variable key must not contain spaces")
}
}
}
diff --git a/pkg/config/menu.go b/pkg/config/menu.go
index 7321c7b..a2c5e1e 100644
--- a/pkg/config/menu.go
+++ b/pkg/config/menu.go
@@ -1,10 +1,9 @@
package config
import (
+ "fmt"
"regexp"
"strings"
-
- "github.com/pkg/errors"
)
var reValidMenuName = regexp.MustCompile(`^[\w\._]+$`)
@@ -31,18 +30,18 @@ type MenuEntry struct {
func (m *Menu) validate(name string) error {
// validate name
if ok := reValidMenuName.MatchString(name); !ok {
- return errors.New("Invalid menu name, must be an alphanumeric word and match regex `^[\\w\\._]+$` : " + name)
+ return fmt.Errorf("Invalid menu name, must be an alphanumeric word and match regex `^[\\w\\._]+$` : " + name)
}
// Banner is just any string, nothing to validate
// MenuEntries
if len(m.MenuEntries) == 0 {
- return errors.New("A Menu needs MenuEntries to be valid")
+ return fmt.Errorf("A Menu needs MenuEntries to be valid")
}
// Duplicate detection is natively handled by the yaml parser
for i := 0; i < len(m.MenuEntries); i++ {
m.MenuEntries[i].validate()
if m.MenuEntries[i].Action == "menu "+name {
- return errors.New("A menu shall not loop on itself")
+ return fmt.Errorf("A menu shall not loop on itself")
}
}
// Loop test
@@ -52,10 +51,10 @@ func (m *Menu) validate(name string) error {
func (m *Menu) validateConsistency(c *Config) error {
// Necessary menus
if _, ok := c.Menus["anonymous"]; !ok {
- return errors.New("No anonymous menu declared")
+ return fmt.Errorf("No anonymous menu declared")
}
if _, ok := c.Menus["logged_in"]; !ok {
- return errors.New("No logged_in menu declared")
+ return fmt.Errorf("No logged_in menu declared")
}
// Validate actions
menus := map[string]bool{
@@ -71,13 +70,13 @@ func (m *Menu) validateConsistency(c *Config) error {
if _, ok := c.Menus[tokens[1]]; ok {
menus[tokens[1]] = true
} else {
- return errors.New("menu action " + tokens[1] + " in menu " + k + " does not exist")
+ return fmt.Errorf("menu action " + tokens[1] + " in menu " + k + " does not exist")
}
case "play":
if _, ok := c.Games[tokens[1]]; ok {
playable[tokens[1]] = true
} else {
- return errors.New("play action " + tokens[1] + " in menu " + k + " does not exist")
+ return fmt.Errorf("play action " + tokens[1] + " in menu " + k + " does not exist")
}
}
}
@@ -85,12 +84,12 @@ func (m *Menu) validateConsistency(c *Config) error {
// Check for unreachables
for k, _ := range c.Menus {
if _, ok := menus[k]; !ok {
- return errors.New("unreachable menu : " + k)
+ return fmt.Errorf("unreachable menu : " + k)
}
}
for k, _ := range c.Games {
if _, ok := playable[k]; !ok {
- return errors.New("unplayable game : " + k)
+ return fmt.Errorf("unplayable game : " + k)
}
}
return nil
@@ -99,15 +98,15 @@ func (m *Menu) validateConsistency(c *Config) error {
func (m *MenuEntry) validate() error {
// Key
if ok := reValidKey.MatchString(m.Key); !ok {
- return errors.New("Invalid Key, must be exactly one alphanumeric character and match regex `^\\w$` : " + m.Key)
+ return fmt.Errorf("Invalid Key, must be exactly one alphanumeric character and match regex `^\\w$` : " + m.Key)
}
// Label
if len(m.Label) <= 0 {
- return errors.New("Invalid Label, cannot be empty")
+ return fmt.Errorf("Invalid Label, cannot be empty")
}
// Action
if err := validateAction(m.Action); err != nil {
- return errors.Wrap(err, "Invalid Action in MenuEntry")
+ return fmt.Errorf("Invalid Action in MenuEntry : %w", err)
}
return nil
}