Skip to content

Commit 1e2afcc

Browse files
Erouichermehdi
authored andcommitted
Resolve some go lints
- Resolve some go comments lints - Replace error.New(fmt.Sprintf statements with fmt.Errorf
1 parent b909e4d commit 1e2afcc

File tree

9 files changed

+58
-56
lines changed

9 files changed

+58
-56
lines changed

commands/case.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package commands
33
import (
44
"bufio"
55
"fmt"
6-
"github.com/chermehdi/egor/config"
7-
"github.com/fatih/color"
8-
"github.com/urfave/cli/v2"
96
"os"
107
"path"
118
"strconv"
9+
10+
"github.com/chermehdi/egor/config"
11+
"github.com/fatih/color"
12+
"github.com/urfave/cli/v2"
1213
)
1314

1415
// Read from stdin till ctrl D or Command D
@@ -44,7 +45,7 @@ func writeLinesToFile(filename string, lines []string) error {
4445
return nil
4546
}
4647

47-
// Create and save user specified custom case input, and update the given egor meta data
48+
// AddNewCaseInput Create and save user specified custom case input, and update the given egor meta data
4849
func AddNewCaseInput(inputLines []string,
4950
caseName string,
5051
metaData config.EgorMeta) (config.EgorMeta, error) {
@@ -60,7 +61,7 @@ func AddNewCaseInput(inputLines []string,
6061
return metaData, nil
6162
}
6263

63-
// Create and save user specified custom csae output, and update the given egor meta data
64+
// AddNewCaseOutput Create and save user specified custom csae output, and update the given egor meta data
6465
func AddNewCaseOutput(outputLines []string,
6566
caseName string,
6667
metaData config.EgorMeta) (config.EgorMeta, error) {
@@ -76,7 +77,7 @@ func AddNewCaseOutput(outputLines []string,
7677
return metaData, nil
7778
}
7879

79-
// Create a user custom test case
80+
// CustomCaseAction Create a user custom test case
8081
func CustomCaseAction(context *cli.Context) error {
8182
color.Green("Creating Custom Test Case...")
8283

@@ -132,7 +133,7 @@ func CustomCaseAction(context *cli.Context) error {
132133
return nil
133134
}
134135

135-
// Command to add costum test cases to the current task.
136+
// CaseCommand Command to add costum test cases to the current task.
136137
// Running this command will ask the user to provide their input and output, then
137138
// saves the new test case data into appropriate files and add their meta data into
138139
// the current task egor meta data.

commands/config.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package commands
22

33
import (
4-
"errors"
54
"fmt"
65
"strconv"
76
"strings"
@@ -31,20 +30,20 @@ func UpdateConfiguration(config *config.Config, key, value string) error {
3130
config.CustomTemplate[lang] = value
3231
} else {
3332
// Unknow key
34-
return errors.New(fmt.Sprintf("Unknown configuration property %s", key))
33+
return fmt.Errorf("Unknown configuration property %s", key)
3534
}
3635
return nil
3736
}
3837

39-
// Sets the configuration property identified by the first argument
38+
// SetAction Sets the configuration property identified by the first argument
4039
// To the value identified by the second argument.
4140
// Note: The configuration keys are not case sensitive, if a configuration key provided
4241
// is not recognized, an error is thrown
4342
func SetAction(context *cli.Context) error {
4443
argLen := context.Args().Len()
4544
if argLen != 2 {
4645
color.Red(fmt.Sprintln("Usage egor config set key value"))
47-
return errors.New(fmt.Sprintf("Expected 2 parameters, got %d", argLen))
46+
return fmt.Errorf("Expected 2 parameters, got %d", argLen)
4847
}
4948
key, value := context.Args().Get(0), context.Args().Get(1)
5049

@@ -59,13 +58,13 @@ func SetAction(context *cli.Context) error {
5958
return config.SaveConfiguration(configuration)
6059
}
6160

62-
// Gets and prints the current configuration associted to the first argument,
61+
// GetAction Gets and prints the current configuration associted to the first argument,
6362
// or Prints all if no argument is specified
6463
func GetAction(context *cli.Context) error {
6564
argLen := context.Args().Len()
6665
if argLen > 1 {
6766
color.Red(fmt.Sprintln("Usage egor config get <key?>"))
68-
return errors.New(fmt.Sprintf("Expected at most 1 parameter, got %d", argLen))
67+
return fmt.Errorf("Expected at most 1 parameter, got %d", argLen)
6968
}
7069
configuration, err := config.LoadDefaultConfiguration()
7170
if err != nil {

commands/copy.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ package commands
22

33
import (
44
"fmt"
5+
"io/ioutil"
6+
"os"
7+
"path"
8+
59
"github.com/atotto/clipboard"
610
"github.com/chermehdi/egor/config"
711
"github.com/fatih/color"
812
"github.com/urfave/cli/v2"
9-
"io/ioutil"
10-
"os"
11-
"path"
1213
)
1314

14-
// Load the content of a given file
15+
// GetFileContent Load the content of a given file
1516
func GetFileContent(filePath string) (string, error) {
1617
filebytes, err := ioutil.ReadFile(filePath)
1718
if err != nil {
@@ -65,7 +66,7 @@ func CopyAction(*cli.Context) error {
6566
return nil
6667
}
6768

68-
// Command to copy task source file to clipboard for easy submit.
69+
// CopyCommand Command to copy task source file to clipboard for easy submit.
6970
// Running this command will fetch egor meta data, get the content of the task source
7071
// and then copy the content to the clipboard.
7172
var CopyCommand = cli.Command{

commands/create.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ package commands
33
import (
44
"bufio"
55
"fmt"
6-
"github.com/chermehdi/egor/config"
7-
"github.com/fatih/color"
8-
"github.com/urfave/cli/v2"
96
"io"
107
"os"
118
"strings"
9+
10+
"github.com/chermehdi/egor/config"
11+
"github.com/fatih/color"
12+
"github.com/urfave/cli/v2"
1213
)
1314

1415
func ReadLine(reader io.Reader) string {
@@ -93,7 +94,7 @@ func fillTaskFromQuestions(task *config.Task, reader io.Reader) error {
9394
return nil
9495
}
9596

96-
// Creates a task directory structure, either in a default manner (a task with a generic name, and default settings).
97+
// CreateTaskAction Creates a task directory structure, either in a default manner (a task with a generic name, and default settings).
9798
// Or by specifying the values in an interactive manner, if `-i` flag is specified.
9899
func CreateTaskAction(context *cli.Context) error {
99100
// Default task containing default values.
@@ -133,7 +134,7 @@ func CreateTaskAction(context *cli.Context) error {
133134
return nil
134135
}
135136

136-
// Command will ask the user for a bunch of questions about the task name
137+
// CreateTaskCommand Command will ask the user for a bunch of questions about the task name
137138
// Input and outputs and other metadata required to create the task
138139
var CreateTaskCommand = cli.Command{
139140
Name: "create",

commands/parse.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323

2424
const listenAddr = ":4243"
2525

26-
// TaskExistsError is an rror indicating the task directory exists. Used to be able to skip task creation and move the next one when parsing a contest.
27-
var TaskExistsError = errors.New("Task already exists, creation skipped!")
26+
// ErrTaskExists is an rror indicating the task directory exists. Used to be able to skip task creation and move the next one when parsing a contest.
27+
var ErrTaskExists = errors.New("Task already exists, creation skipped!")
2828

29-
// Serialize task into a JSON string.
29+
// SerializeTask Serialize task into a JSON string.
3030
func SerializeTask(meta EgorMeta) (string, error) {
3131
var buffer bytes.Buffer
3232
encoder := json2.NewEncoder(&buffer)
@@ -40,7 +40,7 @@ func CreateDirectoryStructure(task Task, config Config, rootDir string, context
4040
taskDir := path.Join(rootDir, task.Name)
4141
if err := os.Mkdir(taskDir, 0777); err != nil {
4242
if os.IsExist(err) {
43-
return "", TaskExistsError
43+
return "", ErrTaskExists
4444
}
4545
return "", err
4646
}
@@ -234,7 +234,7 @@ func ParseAction(context *cli.Context) error {
234234

235235
taskDir, err := CreateDirectoryStructure(*task, *config, cwd, context)
236236
if err != nil {
237-
if err == TaskExistsError {
237+
if err == ErrTaskExists {
238238
color.Magenta("Skipping creating task %s as it already exists", task.Name)
239239
continue
240240
} else {
@@ -249,7 +249,7 @@ func ParseAction(context *cli.Context) error {
249249
return nil
250250
}
251251

252-
// Command to parse tasks from the `Competitive Companion` Chrome extension.
252+
// ParseCommand Command to parse tasks from the `Competitive Companion` Chrome extension.
253253
// Running this command, will start a server on the default port that the extension
254254
// uses, and will create a directory structure containing input files, expected output files,
255255
// and an additional `egor-meta.json` file, and finally your task file, which is usually a `main.cpp` or `Main.java`

commands/printcase.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import (
44
"bufio"
55
"errors"
66
"fmt"
7+
"os"
8+
"path"
9+
"strconv"
10+
711
"github.com/chermehdi/egor/config"
812
"github.com/chermehdi/egor/utils"
913
"github.com/fatih/color"
1014
"github.com/urfave/cli/v2"
11-
"os"
12-
"path"
13-
"strconv"
1415
)
1516

1617
func GetTestCase(egorMeta config.EgorMeta, id int) *TestCaseIO {
@@ -88,7 +89,7 @@ func PrintCaseAction(context *cli.Context) error {
8889

8990
if err != nil {
9091
color.Red(fmt.Sprintf("Cannot parse test id = '%s', a number required!", context.Args().Get(0)))
91-
return errors.New(fmt.Sprintf("Failed to parse test id = %s", context.Args().Get(0)))
92+
return fmt.Errorf("Failed to parse test id = %s", context.Args().Get(0))
9293
}
9394

9495
cwd, err := os.Getwd()
@@ -113,7 +114,7 @@ func PrintCaseAction(context *cli.Context) error {
113114
testCase := GetTestCase(metaData, id)
114115
if testCase == nil {
115116
color.Red(fmt.Sprintf("Could not find test case with id = %d", id))
116-
return errors.New(fmt.Sprintf("Unknown test case with id %d", id))
117+
return fmt.Errorf("Unknown test case with id %d", id)
117118
}
118119

119120
if !context.Bool("output-only") {
@@ -127,7 +128,7 @@ func PrintCaseAction(context *cli.Context) error {
127128
return nil
128129
}
129130

130-
// Command to print a test case. this command can be used to print inputs and/or outputs
131+
// PrintCaseCommand Command to print a test case. this command can be used to print inputs and/or outputs
131132
// to the consol. The user can choose to print the input only or the output only. The
132133
// user should provide a valid test id.
133134
// Running this command will fetch egor meta data, get the test case with the given id,

commands/run.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package commands
22

33
import (
44
"bytes"
5-
"errors"
65
"fmt"
76
"io/ioutil"
87
"os"
@@ -43,7 +42,7 @@ type DiffChecker struct {
4342
func (c *DiffChecker) Check(got, expected string) error {
4443
// Compare the trimmed output from both input and output
4544
if strings.TrimRight(got, " \t\n\r") != strings.TrimRight(expected, " \t\n\r") {
46-
return errors.New(fmt.Sprintf("Checker failed, expected:\n%s\nfound:\n%s", expected, got))
45+
return fmt.Errorf("Checker failed, expected:\n%s\nfound:\n%s", expected, got)
4746
}
4847
return nil
4948
}
@@ -58,15 +57,15 @@ func (c *TokenChecker) Check(got, expected string) error {
5857
inputTokens := strings.Fields(got)
5958
outputTokens := strings.Fields(expected)
6059
if len(inputTokens) != len(outputTokens) {
61-
return errors.New(fmt.Sprintf("Checker failed, number of tokens different: expected %d, got %d\n\r",
62-
len(outputTokens), len(inputTokens)))
60+
return fmt.Errorf("Checker failed, number of tokens different: expected %d, got %d\n\r",
61+
len(outputTokens), len(inputTokens))
6362
}
6463
i := 0
6564
n := len(inputTokens)
6665
for i < n {
6766
if inputTokens[i] != outputTokens[i] {
68-
return errors.New(fmt.Sprintf("Checker failed, token %d does not match: expected %s, got %s\n\r",
69-
i, outputTokens[i], inputTokens[i]))
67+
return fmt.Errorf("Checker failed, token %d does not match: expected %s, got %s\n\r",
68+
i, outputTokens[i], inputTokens[i])
7069
}
7170
i = i + 1
7271
}
@@ -473,7 +472,7 @@ func NewJudgeFor(meta config.EgorMeta, configuration *config.Config, checker Che
473472
case "python":
474473
return &PythonJudge{Meta: meta, checker: checker}, nil
475474
}
476-
return nil, errors.New(fmt.Sprintf("Cannot find judge for the given lang %s", meta.TaskLang))
475+
return nil, fmt.Errorf("Cannot find judge for the given lang %s", meta.TaskLang)
477476
}
478477

479478
// Resolve the checker by name, otherwise fallback to the DiffChecker

commands/showcases.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package commands
22

33
import (
4+
"os"
5+
"path"
6+
47
"github.com/chermehdi/egor/config"
58
"github.com/fatih/color"
69
"github.com/jedib0t/go-pretty/table"
710
"github.com/urfave/cli/v2"
8-
"os"
9-
"path"
1011
)
1112

1213
type TestCaseIO struct {
@@ -17,7 +18,7 @@ type TestCaseIO struct {
1718
Custom bool
1819
}
1920

20-
// parse input and output from egor meta to TestCase
21+
// GetTestCases parse input and output from egor meta to TestCase
2122
func GetTestCases(egorMeta config.EgorMeta) []*TestCaseIO {
2223
testCasesMap := make(map[string]*TestCaseIO)
2324
testCases := make([]*TestCaseIO, 0)
@@ -42,7 +43,7 @@ func GetTestCases(egorMeta config.EgorMeta) []*TestCaseIO {
4243
return testCases
4344
}
4445

45-
// print test cases table
46+
// PrintTestCasesTable print test cases table
4647
func PrintTestCasesTable(testCases []*TestCaseIO) {
4748
t := table.NewWriter()
4849
t.SetOutputMirror(os.Stdout)
@@ -60,12 +61,12 @@ func PrintTestCasesTable(testCases []*TestCaseIO) {
6061
t.Render()
6162
}
6263

63-
// print task test cases
64+
// PrintTestCases print task test cases
6465
func PrintTestCases(egorMeta config.EgorMeta) {
6566
PrintTestCasesTable(GetTestCases(egorMeta))
6667
}
6768

68-
// list test cases information command action
69+
// ShowCasesAction list test cases information command action
6970
// TODO(Eroui): [Refactoring] Duplicate code while loading meta data, consider refactoring...
7071
func ShowCasesAction(_ *cli.Context) error {
7172
cwd, err := os.Getwd()
@@ -95,7 +96,7 @@ func ShowCasesAction(_ *cli.Context) error {
9596
return nil
9697
}
9798

98-
// Command to print the list of test cases input and outputs into the consol.
99+
// ShowCasesCommand Command to print the list of test cases input and outputs into the consol.
99100
// Running this command will fetch egor meta data and load all inputs and outputs meta data
100101
// and prints it as an array into the consol.
101102
var ShowCasesCommand = cli.Command{

0 commit comments

Comments
 (0)