Skip to content

Commit b3804da

Browse files
committed
Working version of egor case
1 parent d6c76f8 commit b3804da

File tree

2 files changed

+64
-5
lines changed

2 files changed

+64
-5
lines changed

commands/case.go

+35-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import (
55
"github.com/fatih/color"
66
"github.com/urfave/cli/v2"
77
"fmt"
8-
"os"
8+
"os"
9+
"github.com/chermehdi/egor/config"
10+
"path"
11+
"strconv"
912
)
1013

1114
func readFromStdin() ([]string, error) {
@@ -22,7 +25,7 @@ func readFromStdin() ([]string, error) {
2225
lines = append(lines, line)
2326
}
2427

25-
// TODO add check for errors
28+
// TODO(Eroui) add check for errors
2629
return lines, nil
2730
}
2831

@@ -42,19 +45,46 @@ func writeLinesToFile(filename string, lines []string) {
4245
}
4346
}
4447

45-
4648
// TODO(Eroui): add checks on errors
4749
func CustomCaseAction(context *cli.Context) error {
4850
color.Green("Creating Custom Test Case...")
4951

52+
// Load meta data
53+
cwd, err := os.Getwd()
54+
if err != nil {
55+
return err
56+
}
57+
58+
meta_data, err := config.LoadMetaFromPath(path.Join(cwd, "egor-meta.json"))
59+
if err != nil {
60+
return err
61+
}
62+
5063
color.Green("Provide your input:")
5164
input_lines, _ := readFromStdin()
5265

5366
color.Green("Provide your output:")
5467
output_lines, _ := readFromStdin()
68+
69+
case_name := "test-" + strconv.Itoa(len(meta_data.Inputs))
5570

56-
writeLinesToFile("sample.in", input_lines)
57-
writeLinesToFile("sample.out", output_lines)
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)
82+
83+
meta_data.SaveToFile(path.Join(cwd, "egor-meta.json"))
84+
85+
if err != nil {
86+
fmt.Println(err)
87+
}
5888

5989
color.Green("Created Custom Test Case...")
6090
return nil

config/meta.go

+29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io"
99
"path"
10+
"os"
1011
)
1112

1213
type IoFile struct {
@@ -88,9 +89,37 @@ func (egor *EgorMeta) Save(w io.Writer) error {
8889
return err
8990
}
9091

92+
func (egor *EgorMeta) SaveToFile(file_path string) error {
93+
file, _ := CreateFile(file_path)
94+
return egor.Save(file)
95+
}
96+
9197
// TODO(chermehdi): probably this should't be a member function
9298
func (egor *EgorMeta) Load(r io.Reader) error {
9399
decoder := json2.NewDecoder(r)
94100
err := decoder.Decode(egor)
95101
return err
96102
}
103+
104+
func LoadMeta(r io.Reader) (EgorMeta, error) {
105+
var egor_meta EgorMeta
106+
decoder := json2.NewDecoder(r)
107+
err := decoder.Decode(&egor_meta)
108+
return egor_meta, err
109+
}
110+
111+
func LoadMetaFromPath(file_path string) (EgorMeta, error) {
112+
file, _ := OpenFileFromPath(file_path)
113+
return LoadMeta(file)
114+
}
115+
116+
// TODO(Eroui): this is a duplicate function from parse.go
117+
// consider moving this somewhere common or use the other one
118+
func CreateFile(filePath string) (*os.File, error) {
119+
return os.OpenFile(filePath, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0777)
120+
}
121+
122+
func OpenFileFromPath(filePath string) (*os.File, error) {
123+
file, err := os.Open(filePath)
124+
return file, err
125+
}

0 commit comments

Comments
 (0)