Skip to content

Commit 350700c

Browse files
author
Sergey Kharitontsev-Beglov
committed
Refactor + bug fixes + add GetConfig
1 parent 6325983 commit 350700c

7 files changed

Lines changed: 80 additions & 41 deletions

File tree

client/client.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ import (
1111
"path/filepath"
1212
)
1313

14-
type Client struct {
15-
Jar *cookiejar.Jar `json:"cookies"`
16-
User string `json:"login"`
17-
Password string `json:"password"`
18-
client *http.Client
19-
path string
20-
}
21-
2214
var Instance *Client
2315

2416
const (

client/info.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,6 @@ func (c *Client) GetConfig(path string) (cfg *config.Config, err error) {
109109
return
110110
}
111111

112-
type Contest struct {
113-
ContestId string
114-
ContestName string
115-
ContestStatus string
116-
ContestStarted string
117-
}
118-
119112
func (c *Client) GetAvailableContests() (res []Contest, err error) {
120113
body, err := util.GetBody(c.client, HOST+"/contests?mask=1")
121114
if err != nil {

client/submit.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,6 @@ import (
1515
"strings"
1616
)
1717

18-
type Submission struct {
19-
ID string
20-
Problem *config.Problem
21-
Attempt string
22-
Time string
23-
Compiler *config.Compiler
24-
Result string
25-
}
26-
27-
type Test struct {
28-
ID string
29-
Result string
30-
TimeUsed string
31-
MemoryUsed string
32-
Comment string
33-
}
34-
3518
var (
3619
findSubmissionsRegex, _ = regexp.Compile("<(TR|tr) (CLASS|class)[\\d\\D]*?>([\\d\\D]+?)</(TR|tr)>")
3720
findColumnsRegex, _ = regexp.Compile("<(TD|td)>([\\d\\D]+?)</(TD|td)>")

client/types.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package client
2+
3+
import (
4+
"github.com/khbminus/tscli/config"
5+
"net/http"
6+
"net/http/cookiejar"
7+
)
8+
9+
type Client struct {
10+
Jar *cookiejar.Jar `json:"cookies"`
11+
User string `json:"login"`
12+
Password string `json:"password"`
13+
client *http.Client
14+
path string
15+
}
16+
type Submission struct {
17+
ID string
18+
Problem *config.Problem
19+
Attempt string
20+
Time string
21+
Compiler *config.Compiler
22+
Result string
23+
}
24+
type Test struct {
25+
ID string
26+
Result string
27+
TimeUsed string
28+
MemoryUsed string
29+
Comment string
30+
}
31+
type Contest struct {
32+
ContestId string
33+
ContestName string
34+
ContestStatus string
35+
ContestStarted string
36+
}

cmd/local.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,48 @@
11
package cmd
22

33
import (
4+
"errors"
45
"fmt"
56
"github.com/khbminus/tscli/client"
67
"github.com/khbminus/tscli/config"
78
"github.com/khbminus/tscli/util"
89
"github.com/logrusorgru/aurora/v3"
10+
"github.com/olekukonko/tablewriter"
11+
"os"
12+
"path"
13+
"strconv"
914
)
1015

1116
const (
12-
ConfigPath = "./.tscli.local"
17+
ConfigName = ".tscli.local"
1318
)
1419

20+
func GetConfig() (*config.Config, error) {
21+
cwd, err := os.Getwd()
22+
if err != nil {
23+
panic(err)
24+
}
25+
for {
26+
if cwd == "/" || cwd == "" {
27+
fmt.Println(aurora.Red("Can't find a local config. Please run tscli local parse"))
28+
return nil, errors.New("no config found")
29+
}
30+
if _, err := os.Stat(cwd + "/" + ConfigName); err == nil {
31+
return config.NewConfig(cwd + "/" + ConfigName)
32+
} else if os.IsNotExist(err) {
33+
cwd = path.Dir(cwd)
34+
} else {
35+
panic(err)
36+
}
37+
}
38+
}
39+
1540
func ShowLocalConfig() error {
16-
cfg, err := config.NewConfig(ConfigPath)
41+
cfg, err := GetConfig()
1742
if err != nil {
1843
return err
1944
}
20-
var compilerName string = "Doesn't set"
45+
compilerName := "Doesn't set"
2146
if cfg.Compilers != nil && cfg.DefaultLang != -1 {
2247
compilerName = cfg.Compilers[cfg.DefaultLang].CompilerName
2348
}
@@ -43,7 +68,7 @@ func ShowLocalConfig() error {
4368

4469
func ParseConfig() error {
4570
fmt.Println(aurora.Yellow("Getting new config..."))
46-
cfg, err := client.Instance.GetConfig(ConfigPath)
71+
cfg, err := client.Instance.GetConfig("./" + ConfigName)
4772
if err != nil {
4873
return err
4974
}
@@ -64,9 +89,14 @@ func ChooseContest(contestId string) error {
6489
}
6590
index := -1
6691
if contestId == "" {
92+
table := tablewriter.NewWriter(os.Stdout)
93+
table.SetHeader([]string{"index", "Id", "Name", "Started", "Status"})
94+
table.SetAutoWrapText(false)
95+
table.SetAlignment(tablewriter.ALIGN_CENTER)
6796
for i, v := range contests {
68-
fmt.Printf("%v) %v\n", i, v)
97+
table.Append([]string{strconv.Itoa(i), v.ContestId, v.ContestName, v.ContestStarted, v.ContestStatus})
6998
}
99+
table.Render()
70100
index = util.ChooseIndex(len(contests))
71101
} else {
72102
for i, contest := range contests {
@@ -94,7 +124,7 @@ func ChooseContest(contestId string) error {
94124
}
95125

96126
func ChangeDefaultLang() error {
97-
cfg, err := config.NewConfig(ConfigPath)
127+
cfg, err := GetConfig()
98128
if err != nil {
99129
return err
100130
}

cmd/submit.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func SubmitAndWatch(problem config.Problem, compiler config.Compiler, solution s
1818
return err
1919
}
2020

21-
cfg, err := config.NewConfig(ConfigPath)
21+
cfg, err := GetConfig()
2222
submits, err := client.Instance.GetAllSubmits(*cfg)
2323
nowId := submits[0].ID
2424
if err != nil {
@@ -52,6 +52,7 @@ func SubmitAndWatch(problem config.Problem, compiler config.Compiler, solution s
5252
table := tablewriter.NewWriter(os.Stdout)
5353
table.SetHeader([]string{"#", "Time used", "Memory used", "Status", "Comment"})
5454
table.SetAutoWrapText(false)
55+
table.SetAlignment(tablewriter.ALIGN_CENTER)
5556
for _, test := range feedback {
5657
var status string
5758
switch test.Result {

tscli.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"github.com/devfacet/gocmd/v3"
66
"github.com/khbminus/tscli/client"
77
"github.com/khbminus/tscli/cmd"
8-
"github.com/khbminus/tscli/config"
98
"github.com/logrusorgru/aurora/v3"
109
"github.com/mitchellh/go-homedir"
1110
"io/ioutil"
@@ -43,6 +42,11 @@ func main() {
4342
client.Init(ClientPath)
4443
return cmd.ChooseContest(flags.Local.SetContest.ContestId)
4544
})
45+
46+
gocmd.HandleFlag("Local.Parse", func(gcmd *gocmd.Cmd, args []string) error {
47+
client.Init(ClientPath)
48+
return cmd.ParseConfig()
49+
})
4650
gocmd.HandleFlag("Local.SetCompiler", func(cgmd *gocmd.Cmd, args []string) error {
4751
client.Init(ClientPath)
4852
return cmd.ChangeDefaultLang()
@@ -67,7 +71,7 @@ func main() {
6771
fmt.Println(aurora.Red("Unknown filename"))
6872
return nil
6973
}
70-
cfg, err := config.NewConfig(cmd.ConfigPath)
74+
cfg, err := cmd.GetConfig()
7175
if err != nil {
7276
fmt.Println(aurora.Red("Error at config load"))
7377
return err

0 commit comments

Comments
 (0)