1
- package commands
1
+ package commands
2
2
3
3
import (
4
4
"bufio"
5
+ "fmt"
6
+ "github.com/chermehdi/egor/config"
5
7
"github.com/fatih/color"
6
8
"github.com/urfave/cli/v2"
7
- "fmt"
8
9
"os"
9
- "github.com/chermehdi/egor/config"
10
10
"path"
11
11
"strconv"
12
12
)
13
13
14
- func readFromStdin () ( []string , error ) {
14
+ func readFromStdin () []string {
15
15
scn := bufio .NewScanner (os .Stdin )
16
16
var lines []string
17
17
for scn .Scan () {
@@ -24,32 +24,54 @@ func readFromStdin() ([]string, error) {
24
24
}
25
25
lines = append (lines , line )
26
26
}
27
-
28
- // TODO(Eroui) add check for errors
29
- return lines , nil
27
+ return lines
30
28
}
31
29
32
30
func writeLinesToFile (filename string , lines []string ) {
33
31
f , err := os .Create (filename )
34
32
if err != nil {
35
- fmt .Println (err )
36
- return
33
+ fmt .Println (err )
34
+ return
37
35
}
38
36
39
37
for _ , line := range lines {
40
- fmt .Fprintln (f , line )
41
- if err != nil {
42
- fmt .Println (err )
43
- return
44
- }
45
- }
38
+ fmt .Fprintln (f , line )
39
+ if err != nil {
40
+ fmt .Println (err )
41
+ return
42
+ }
43
+ }
44
+ }
45
+
46
+ func AddNewCaseInput (input_lines []string ,
47
+ case_name string ,
48
+ meta_data config.EgorMeta ) (config.EgorMeta , error ) {
49
+
50
+ input_file_name := case_name + ".in"
51
+ writeLinesToFile ("inputs/" + input_file_name , input_lines )
52
+ input_file := config .NewIoFile (input_file_name , "inputs/" + input_file_name , true )
53
+ meta_data .Inputs = append (meta_data .Inputs , input_file )
54
+
55
+ return meta_data , nil
56
+ }
57
+
58
+ func AddNewCaseOutput (output_lines []string ,
59
+ case_name string ,
60
+ meta_data config.EgorMeta ) (config.EgorMeta , error ) {
61
+
62
+ output_file_name := case_name + ".ans"
63
+ writeLinesToFile ("outputs/" + output_file_name , output_lines )
64
+ output_file := config .NewIoFile (output_file_name , "outputs/" + output_file_name , true )
65
+ meta_data .Outputs = append (meta_data .Outputs , output_file )
66
+
67
+ return meta_data , nil
46
68
}
47
69
48
70
// TODO(Eroui): add checks on errors
49
71
func CustomCaseAction (context * cli.Context ) error {
50
72
color .Green ("Creating Custom Test Case..." )
51
-
52
- // Load meta data
73
+
74
+ // Load meta data
53
75
cwd , err := os .Getwd ()
54
76
if err != nil {
55
77
return err
@@ -60,41 +82,39 @@ func CustomCaseAction(context *cli.Context) error {
60
82
return err
61
83
}
62
84
63
- color .Green ("Provide your input:" )
64
- input_lines , _ := readFromStdin ()
65
-
66
- color .Green ("Provide your output:" )
67
- output_lines , _ := readFromStdin ()
68
-
69
85
case_name := "test-" + strconv .Itoa (len (meta_data .Inputs ))
86
+ color .Green ("Provide your input:" )
87
+ input_lines := readFromStdin ()
88
+ meta_data , err = AddNewCaseInput (input_lines , case_name , meta_data )
70
89
71
- input_file_name := case_name + ".in"
72
- output_file_name := case_name + ".out"
73
-
74
- writeLinesToFile ("inputs/" + input_file_name , input_lines )
75
- writeLinesToFile ("outputs/" + output_file_name , output_lines )
76
-
77
- input_file := config .NewIoFile (input_file_name , "inputs/" + input_file_name , true )
78
- output_file := config .NewIoFile (input_file_name , "outputs/" + output_file_name , true )
79
-
80
- meta_data .Inputs = append (meta_data .Inputs , input_file )
81
- meta_data .Outputs = append (meta_data .Outputs , output_file )
90
+ if ! context .Bool ("no-output" ) {
91
+ color .Green ("Provide your output:" )
92
+ output_lines := readFromStdin ()
93
+ meta_data , err = AddNewCaseOutput (output_lines , case_name , meta_data )
94
+ }
82
95
83
96
meta_data .SaveToFile (path .Join (cwd , "egor-meta.json" ))
84
-
97
+
85
98
if err != nil {
86
- fmt .Println (err )
99
+ color .Red ("Failed to Generate Custom Case" )
100
+ return err
87
101
}
88
-
102
+
89
103
color .Green ("Created Custom Test Case..." )
90
104
return nil
91
105
}
92
106
93
107
var CaseCommand = cli.Command {
94
- Name : "case" ,
95
- Aliases : []string {"c" },
96
- Usage : "Create a custom test case." ,
97
- UsageText : "Add custom test cases to egor task." ,
98
- Action : CustomCaseAction ,
99
- // TODO(Eroui): add necessary flags
108
+ Name : "case" ,
109
+ Aliases : []string {"c" },
110
+ Usage : "Create a custom test case." ,
111
+ UsageText : "Add custom test cases to egor task." ,
112
+ Action : CustomCaseAction ,
113
+ Flags : []cli.Flag {
114
+ & cli.BoolFlag {
115
+ Name : "no-output" ,
116
+ Usage : "This test case doesnt have output" ,
117
+ Value : false ,
118
+ },
119
+ },
100
120
}
0 commit comments