Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
chermehdi committed Mar 14, 2020
2 parents 61a106c + 8267a34 commit 1f6fc48
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 50 deletions.
10 changes: 5 additions & 5 deletions commands/copy.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package commands

import (
"github.com/urfave/cli/v2"
"fmt"
"github.com/atotto/clipboard"
"github.com/chermehdi/egor/config"
"github.com/fatih/color"
"github.com/atotto/clipboard"
"github.com/urfave/cli/v2"
"io/ioutil"
"os"
"path"
"io/ioutil"
"fmt"
)

// Load the content of a given file
// Load the content of a given file
func GetFileContent(filePath string) (string, error) {
filebytes, err := ioutil.ReadFile(filePath)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion commands/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func SerializeTask(meta EgorMeta) (string, error) {
return buffer.String(), nil
}


func CreateDirectoryStructure(task Task, config Config, rootDir string) (string, error) {
taskDir := path.Join(rootDir, task.Name)
if err := os.Mkdir(taskDir, 0777); err != nil {
Expand Down
12 changes: 11 additions & 1 deletion commands/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func TestCreateDirectoryStructure(t *testing.T) {
Version: "1.0",
Author: "MaxHeap",
}
rootDir := os.TempDir()
rootDir := path.Join(os.TempDir(), "egor")
CreateDirectory(rootDir)
defer DeleteDir(rootDir)

_, err := CreateDirectoryStructure(task, configuration, rootDir)
Expand All @@ -98,3 +99,12 @@ func TestCreateDirectoryStructure(t *testing.T) {
assert.FileExists(t, path.Join(taskDir, "inputs", "test-0.in"))
assert.FileExists(t, path.Join(taskDir, "outputs", "test-0.ans"))
}

func CreateDirectory(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
if err := os.Mkdir(path, 0777); err != nil {
return err
}
}
return nil
}
32 changes: 16 additions & 16 deletions commands/printcase.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package commands

import (
"bufio"
"errors"
"github.com/urfave/cli/v2"
"fmt"
"github.com/chermehdi/egor/config"
"github.com/chermehdi/egor/utils"
"github.com/fatih/color"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"os"
"path"
"fmt"
"bufio"
"strconv"
)

func GetTestCase(egorMeta config.EgorMeta, id int) *TestCaseIO {
var testCase *TestCaseIO
for _, input := range egorMeta.Inputs {
if input.GetId() == id {
testCase = &TestCaseIO {
Id : input.GetId(),
Name : input.Name,
InputPath : input.Path,
testCase = &TestCaseIO{
Id: input.GetId(),
Name: input.Name,
InputPath: input.Path,
OutputPath: "",
Custom: input.Custom,
Custom: input.Custom,
}

break
Expand All @@ -32,12 +32,12 @@ func GetTestCase(egorMeta config.EgorMeta, id int) *TestCaseIO {
if testCase == nil {
return nil
}

for _, output := range egorMeta.Outputs {
if output.Name == testCase.Name {
testCase.OutputPath = output.Path
}
}
}

if testCase == nil {
return nil
Expand All @@ -46,7 +46,7 @@ func GetTestCase(egorMeta config.EgorMeta, id int) *TestCaseIO {
}

func PrintTestCaseInput(testCase *TestCaseIO) {

file, err := config.OpenFileFromPath(testCase.InputPath)
if err != nil {
color.Red("Failed to read test case input")
Expand All @@ -57,7 +57,7 @@ func PrintTestCaseInput(testCase *TestCaseIO) {
fmt.Println(scanner.Text())
}
}

}

func PrintTestCaseOutput(testCase *TestCaseIO) {
Expand Down Expand Up @@ -109,7 +109,7 @@ func PrintCaseAction(context *cli.Context) error {
color.Red("Failed to load egor MetaData ")
return err
}

testCase := GetTestCase(metaData, id)
if testCase == nil {
color.Red(fmt.Sprintf("Could not find test case with id = %d", id))
Expand All @@ -123,15 +123,15 @@ func PrintCaseAction(context *cli.Context) error {
if !context.Bool("input-only") {
PrintTestCaseOutput(testCase)
}

return nil
}

// Command to print a test case. this command can be used to print inputs and/or outputs
// to the consol. The user can choose to print the input only or the output only. The
// user should provide a valid test id.
// Running this command will fetch egor meta data, get the test case with the given id,
// and then print the content of the input and/or of the output files.
// and then print the content of the input and/or of the output files.
var PrintCaseCommand = cli.Command{
Name: "printcase",
Aliases: []string{"pc"},
Expand Down
2 changes: 1 addition & 1 deletion commands/printcase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ func TestGetTestCase(t *testing.T) {
assert.Equal(t, testCase.InputPath, metaData.Inputs[0].Path)
assert.Equal(t, testCase.OutputPath, metaData.Outputs[0].Path)
assert.Equal(t, testCase.Custom, metaData.Inputs[0].Custom)
}
}
16 changes: 8 additions & 8 deletions config/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ import (
"io"
"os"
"path"
"strings"
"strconv"
"strings"
)

type IoFile struct {
Name string
Path string
Custom bool
Name string
Path string
Custom bool
}

func NewIoFile(fileName, filePath string, customCase bool) IoFile {
return IoFile{
Name: fileName,
Path: filePath,
Custom: customCase,
Name: fileName,
Path: filePath,
Custom: customCase,
}
}

func (ioFile *IoFile) GetId() int {
tokens := strings.Split(ioFile.Name, "-")
id, err := strconv.Atoi(tokens[1])
if err != nil {
return 0
return 0
}
return id
}
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package main

import (
"bytes"
"html/template"
"log"
"os"
"github.com/chermehdi/egor/commands"
"github.com/chermehdi/egor/config"
"github.com/urfave/cli/v2"
"html/template"
"log"
"os"
)

const Egor = `
Expand Down
15 changes: 0 additions & 15 deletions utils/formatter_test.go

This file was deleted.

0 comments on commit 1f6fc48

Please sign in to comment.