Skip to content

Commit

Permalink
Refactor + bug fixes + add GetConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Kharitontsev-Beglov committed Sep 22, 2021
1 parent 6325983 commit 350700c
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 41 deletions.
8 changes: 0 additions & 8 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ import (
"path/filepath"
)

type Client struct {
Jar *cookiejar.Jar `json:"cookies"`
User string `json:"login"`
Password string `json:"password"`
client *http.Client
path string
}

var Instance *Client

const (
Expand Down
7 changes: 0 additions & 7 deletions client/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,6 @@ func (c *Client) GetConfig(path string) (cfg *config.Config, err error) {
return
}

type Contest struct {
ContestId string
ContestName string
ContestStatus string
ContestStarted string
}

func (c *Client) GetAvailableContests() (res []Contest, err error) {
body, err := util.GetBody(c.client, HOST+"/contests?mask=1")
if err != nil {
Expand Down
17 changes: 0 additions & 17 deletions client/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,6 @@ import (
"strings"
)

type Submission struct {
ID string
Problem *config.Problem
Attempt string
Time string
Compiler *config.Compiler
Result string
}

type Test struct {
ID string
Result string
TimeUsed string
MemoryUsed string
Comment string
}

var (
findSubmissionsRegex, _ = regexp.Compile("<(TR|tr) (CLASS|class)[\\d\\D]*?>([\\d\\D]+?)</(TR|tr)>")
findColumnsRegex, _ = regexp.Compile("<(TD|td)>([\\d\\D]+?)</(TD|td)>")
Expand Down
36 changes: 36 additions & 0 deletions client/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package client

import (
"github.com/khbminus/tscli/config"
"net/http"
"net/http/cookiejar"
)

type Client struct {
Jar *cookiejar.Jar `json:"cookies"`
User string `json:"login"`
Password string `json:"password"`
client *http.Client
path string
}
type Submission struct {
ID string
Problem *config.Problem
Attempt string
Time string
Compiler *config.Compiler
Result string
}
type Test struct {
ID string
Result string
TimeUsed string
MemoryUsed string
Comment string
}
type Contest struct {
ContestId string
ContestName string
ContestStatus string
ContestStarted string
}
42 changes: 36 additions & 6 deletions cmd/local.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
package cmd

import (
"errors"
"fmt"
"github.com/khbminus/tscli/client"
"github.com/khbminus/tscli/config"
"github.com/khbminus/tscli/util"
"github.com/logrusorgru/aurora/v3"
"github.com/olekukonko/tablewriter"
"os"
"path"
"strconv"
)

const (
ConfigPath = "./.tscli.local"
ConfigName = ".tscli.local"
)

func GetConfig() (*config.Config, error) {
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
for {
if cwd == "/" || cwd == "" {
fmt.Println(aurora.Red("Can't find a local config. Please run tscli local parse"))
return nil, errors.New("no config found")
}
if _, err := os.Stat(cwd + "/" + ConfigName); err == nil {
return config.NewConfig(cwd + "/" + ConfigName)
} else if os.IsNotExist(err) {
cwd = path.Dir(cwd)
} else {
panic(err)
}
}
}

func ShowLocalConfig() error {
cfg, err := config.NewConfig(ConfigPath)
cfg, err := GetConfig()
if err != nil {
return err
}
var compilerName string = "Doesn't set"
compilerName := "Doesn't set"
if cfg.Compilers != nil && cfg.DefaultLang != -1 {
compilerName = cfg.Compilers[cfg.DefaultLang].CompilerName
}
Expand All @@ -43,7 +68,7 @@ func ShowLocalConfig() error {

func ParseConfig() error {
fmt.Println(aurora.Yellow("Getting new config..."))
cfg, err := client.Instance.GetConfig(ConfigPath)
cfg, err := client.Instance.GetConfig("./" + ConfigName)
if err != nil {
return err
}
Expand All @@ -64,9 +89,14 @@ func ChooseContest(contestId string) error {
}
index := -1
if contestId == "" {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"index", "Id", "Name", "Started", "Status"})
table.SetAutoWrapText(false)
table.SetAlignment(tablewriter.ALIGN_CENTER)
for i, v := range contests {
fmt.Printf("%v) %v\n", i, v)
table.Append([]string{strconv.Itoa(i), v.ContestId, v.ContestName, v.ContestStarted, v.ContestStatus})
}
table.Render()
index = util.ChooseIndex(len(contests))
} else {
for i, contest := range contests {
Expand Down Expand Up @@ -94,7 +124,7 @@ func ChooseContest(contestId string) error {
}

func ChangeDefaultLang() error {
cfg, err := config.NewConfig(ConfigPath)
cfg, err := GetConfig()
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func SubmitAndWatch(problem config.Problem, compiler config.Compiler, solution s
return err
}

cfg, err := config.NewConfig(ConfigPath)
cfg, err := GetConfig()
submits, err := client.Instance.GetAllSubmits(*cfg)
nowId := submits[0].ID
if err != nil {
Expand Down Expand Up @@ -52,6 +52,7 @@ func SubmitAndWatch(problem config.Problem, compiler config.Compiler, solution s
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"#", "Time used", "Memory used", "Status", "Comment"})
table.SetAutoWrapText(false)
table.SetAlignment(tablewriter.ALIGN_CENTER)
for _, test := range feedback {
var status string
switch test.Result {
Expand Down
8 changes: 6 additions & 2 deletions tscli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/devfacet/gocmd/v3"
"github.com/khbminus/tscli/client"
"github.com/khbminus/tscli/cmd"
"github.com/khbminus/tscli/config"
"github.com/logrusorgru/aurora/v3"
"github.com/mitchellh/go-homedir"
"io/ioutil"
Expand Down Expand Up @@ -43,6 +42,11 @@ func main() {
client.Init(ClientPath)
return cmd.ChooseContest(flags.Local.SetContest.ContestId)
})

gocmd.HandleFlag("Local.Parse", func(gcmd *gocmd.Cmd, args []string) error {
client.Init(ClientPath)
return cmd.ParseConfig()
})
gocmd.HandleFlag("Local.SetCompiler", func(cgmd *gocmd.Cmd, args []string) error {
client.Init(ClientPath)
return cmd.ChangeDefaultLang()
Expand All @@ -67,7 +71,7 @@ func main() {
fmt.Println(aurora.Red("Unknown filename"))
return nil
}
cfg, err := config.NewConfig(cmd.ConfigPath)
cfg, err := cmd.GetConfig()
if err != nil {
fmt.Println(aurora.Red("Error at config load"))
return err
Expand Down

0 comments on commit 350700c

Please sign in to comment.