From 350700c23ca885e607c57bf4e1dc6a4f915233e3 Mon Sep 17 00:00:00 2001 From: Sergey Kharitontsev-Beglov Date: Wed, 22 Sep 2021 12:30:37 +0300 Subject: [PATCH] Refactor + bug fixes + add GetConfig --- client/client.go | 8 -------- client/info.go | 7 ------- client/submit.go | 17 ----------------- client/types.go | 36 ++++++++++++++++++++++++++++++++++++ cmd/local.go | 42 ++++++++++++++++++++++++++++++++++++------ cmd/submit.go | 3 ++- tscli.go | 8 ++++++-- 7 files changed, 80 insertions(+), 41 deletions(-) create mode 100644 client/types.go diff --git a/client/client.go b/client/client.go index 61b0d81..a9ffa73 100644 --- a/client/client.go +++ b/client/client.go @@ -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 ( diff --git a/client/info.go b/client/info.go index 446b51a..1d121dc 100644 --- a/client/info.go +++ b/client/info.go @@ -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 { diff --git a/client/submit.go b/client/submit.go index ef35da7..54bb6fa 100644 --- a/client/submit.go +++ b/client/submit.go @@ -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]+?)") findColumnsRegex, _ = regexp.Compile("<(TD|td)>([\\d\\D]+?)") diff --git a/client/types.go b/client/types.go new file mode 100644 index 0000000..dd830d2 --- /dev/null +++ b/client/types.go @@ -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 +} diff --git a/cmd/local.go b/cmd/local.go index 5872748..3892c9b 100644 --- a/cmd/local.go +++ b/cmd/local.go @@ -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 } @@ -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 } @@ -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 { @@ -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 } diff --git a/cmd/submit.go b/cmd/submit.go index 7747aeb..1f998f4 100644 --- a/cmd/submit.go +++ b/cmd/submit.go @@ -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 { @@ -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 { diff --git a/tscli.go b/tscli.go index 82d4668..7c79c42 100644 --- a/tscli.go +++ b/tscli.go @@ -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" @@ -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() @@ -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