Skip to content

Commit a9da71c

Browse files
author
Utsav Krishnan
committed
clean; add command comments; refator
1 parent a7066ab commit a9da71c

File tree

12 files changed

+53
-95
lines changed

12 files changed

+53
-95
lines changed

.github/workflows/go.yml

+4-8
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,18 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515

16-
- name: Set up Go 1.13
16+
- name: Set up Go 1.16.4
1717
uses: actions/setup-go@v1
1818
with:
19-
go-version: 1.13
19+
go-version: 1.16.4
2020
id: go
2121

2222
- name: Check out code into the Go module directory
2323
uses: actions/checkout@v2
2424

2525
- name: Get dependencies
2626
run: |
27-
go get -v -t -d ./...
28-
if [ -f Gopkg.toml ]; then
29-
curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
30-
dep ensure
31-
fi
27+
go mod download
3228
3329
- name: Build
34-
run: cd project && go build -v .
30+
run: go build -v . && ./cses-cli help

cmd/github.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func pushCommit(ref *github.Reference, tree *github.Tree, opts *GithubConfig) (e
4848
}
4949
parent.Commit.SHA = parent.SHA
5050

51-
commitMessage := fmt.Sprintf("file updated by %v [%v]" , opts.AuthorName, opts.AuthorEmail)
51+
commitMessage := "file updated"
5252
date := time.Now()
5353
author := &github.CommitAuthor{Date: &date, Name: &opts.AuthorName, Email: &opts.AuthorEmail}
5454
commit := &github.Commit{Author: author, Message: &commitMessage, Tree: tree, Parents: []github.Commit{*parent.Commit}}

cmd/helper.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ func writeCodeFile(filename string, text string, template string) bool {
136136
}
137137
_, err = f.WriteString("/*\n" + text + "*/\n" + template)
138138
if err != nil {
139-
f.Close()
139+
err := f.Close()
140+
check(err)
141+
140142
return false
141143
}
142144

cmd/list.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
2+
Copyright © 2021 UTSAV KRISHNAN github.com/ketankr9
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -23,15 +23,13 @@ import (
2323
// listCmd represents the list command
2424
var listCmd = &cobra.Command{
2525
Use: "list",
26-
Short: "A brief description of your command",
27-
Long: `A longer description that spans multiple lines and likely contains examples
28-
and usage of using your command. For example:
29-
30-
Cobra is a CLI library for Go that empowers applications.
31-
This application is a tool to generate the needed files
32-
to quickly create a Cobra application.`,
26+
Example: "cses-cli list, cses-cli list -r",
27+
Short: "list all problem statements along with status",
28+
Long: `Without any flag it lists all the problems from the cache.
29+
To invalidate/refresh/reload the problem list cache use flag '-r'
30+
If you are authenticated it will display the submission status along with the list.
31+
`,
3332
Run: func(cmd *cobra.Command, args []string) {
34-
//fmt.Println("list called")
3533
if cmd.Flag("refresh").Value.String() == "true" {
3634
UpdateProblemListCache(viper.GetString("cookie"))
3735
}

cmd/login.go

+8-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
2+
Copyright © 2021 UTSAV KRISHNAN github.com/ketankr9
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -29,13 +29,12 @@ import (
2929
// loginCmd represents the login command
3030
var loginCmd = &cobra.Command{
3131
Use: "login",
32-
Short: "A brief description of your command",
33-
Long: `A longer description that spans multiple lines and likely contains examples
34-
and usage of using your command. For example:
35-
36-
Cobra is a CLI library for Go that empowers applications.
37-
This application is a tool to generate the needed files
38-
to quickly create a Cobra application.`,
32+
Example: "cses-cli login",
33+
Short: "authenticate to cses.fi server by username and password",
34+
Long: `This command retrieves the csrf token and signs it to the server,
35+
reads the cookies and saves it in the config file along with the username
36+
Note: password is not saved in the config file and is only used to sign the csrf token
37+
`,
3938
Run: func(cmd *cobra.Command, args []string) {
4039
if !doLogin() {
4140
cobra.CompErrorln("failed to login")
@@ -74,7 +73,7 @@ func doLogin() bool {
7473

7574
s.Start()
7675
wg.Wait()
77-
ok := login(csrf, cookie, username, PASSWORD)
76+
ok := Login(csrf, cookie, username, PASSWORD)
7877
s.Stop()
7978

8079
if !ok {
@@ -89,12 +88,3 @@ func doLogin() bool {
8988

9089
return true
9190
}
92-
93-
func login(csrf string, cookie string, username string, pass string) bool {
94-
95-
params := "csrf_token=" + csrf + "&nick=" + username + "&pass=" + pass
96-
if LoginRequest(params, cookie) {
97-
return true
98-
}
99-
return false
100-
}

cmd/requests.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,16 @@ func submitRequest(opts map[string]string, filename string, cookie string) strin
8181
return link
8282
}
8383

84-
func LoginRequest(params string, cookie string) bool {
84+
func Login(csrf string, cookie string, username string, pass string) bool {
85+
86+
params := "csrf_token=" + csrf + "&nick=" + username + "&pass=" + pass
87+
if loginRequest(params, cookie) == username {
88+
return true
89+
}
90+
return false
91+
}
92+
93+
func loginRequest(params string, cookie string) string {
8594

8695
body := strings.NewReader(params)
8796
req, err := http.NewRequest("POST", "https://cses.fi/login", body)
@@ -97,14 +106,10 @@ func LoginRequest(params string, cookie string) bool {
97106
check(err)
98107
defer resp.Body.Close()
99108

100-
fmt.Println("Body", resp.Body)
101-
//doc, err := goquery.NewDocumentFromReader(resp.Body)
109+
doc, err := goquery.NewDocumentFromReader(resp.Body)
102110
check(err)
103111

104-
printResp(resp)
105-
106-
//return doc.Find(".account").Contents().Text()
107-
return resp.StatusCode == 200
112+
return doc.Find(".account").Contents().Text()
108113
}
109114

110115
func ListRequest(cookie string) []*Problem {

cmd/root.go

+3-28
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,8 @@ var (
3131
// rootCmd represents the base command when called without any subcommands
3232
var rootCmd = &cobra.Command{
3333
Use: "cses-cli",
34-
Short: "A brief description of your application",
35-
Long: `A longer description`,
36-
// Uncomment the following line if your bare application
37-
// has an action associated with it:
38-
// Run: func(cmd *cobra.Command, args []string) { },
34+
Short: "command line to solve probmens from https://cses.fi/problemset/",
35+
Long: `command line to solve probmens from https://cses.fi/problemset/`,
3936
}
4037

4138
// Execute adds all child commands to the root command and sets flags appropriately.
@@ -58,32 +55,11 @@ func init() {
5855
// will be global for your application.
5956

6057
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is "+rootPath+"/config.*)")
61-
62-
//rootCmd.PersistentFlags().StringP("username","u", "", "username on cses")
63-
//err := viper.BindPFlag("username", rootCmd.PersistentFlags().Lookup("username"))
64-
//cobra.CheckErr(err)
65-
66-
//rootCmd.PersistentFlags().StringP("csrf","t", "", "csrf token")
67-
//err := viper.BindPFlag("csrf", rootCmd.PersistentFlags().Lookup("csrf"))
68-
//cobra.CheckErr(err)
69-
//
70-
//rootCmd.PersistentFlags().StringP("cookie","c", "", "http cookie")
71-
//err = viper.BindPFlag("cookie", rootCmd.PersistentFlags().Lookup("cookie"))
72-
//cobra.CheckErr(err)
73-
74-
//rootCmd.PersistentFlags().StringP("rootPath","p", rootPath, "path to store problem statements")
75-
//err = viper.BindPFlag("rootPath", rootCmd.PersistentFlags().Lookup("rootPath"))
76-
//cobra.CheckErr(err)
77-
78-
79-
80-
// Cobra also supports local flags, which will only run
81-
// when this action is called directly.
82-
rootCmd.Flags().BoolP("toggle", "o", false, "Help message for toggle")
8358
}
8459

8560
// initConfig reads in config file and ENV variables if set.
8661
func initConfig() {
62+
// disabling it for now
8763
//viper.AutomaticEnv() // read in environment variables first to be overwritten by config
8864

8965
if cfgFile != "" {
@@ -96,7 +72,6 @@ func initConfig() {
9672
viper.AddConfigPath("/etc/cses/")
9773

9874
viper.SetConfigName("config") // name of config file (without extension)
99-
viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name
10075
}
10176

10277
// If a config file is found, read it in.

cmd/show.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,9 @@ import (
2323
var showCmd = &cobra.Command{
2424
Use: "show",
2525
Args: cobra.ExactArgs(1),
26-
Short: "A brief description of your command",
27-
Long: `A longer description that spans multiple lines and likely contains examples
28-
and usage of using your command. For example:
29-
30-
Cobra is a CLI library for Go that empowers applications.
31-
This application is a tool to generate the needed files
32-
to quickly create a Cobra application.`,
26+
Short: "show a problem statement by number",
27+
Example: "cses-cli show 1068",
28+
Long: `Print the problem statement identified by the task number to stdout`,
3329
Run: func(cmd *cobra.Command, args []string) {
3430
ShowProblem(args[0])
3531
},

cmd/solve.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ import (
2626
var solveCmd = &cobra.Command{
2727
Use: "solve",
2828
Args: cobra.ExactArgs(1),
29-
Short: "A brief description of your command",
30-
Long: `A longer description that spans multiple lines and likely contains examples
31-
and usage of using your command. For example:
29+
Example: "cses-cli solve 1068",
30+
Short: "solve any problem identified by the task number",
31+
Long: `This command will print the problem statement by default (use '-s' to disable it)
32+
By default the editor name is read from the config file to overwrite use like '-e subl', where subl is binary for sublime-text editor.
33+
The language preference is also read form the config file`,
3234

33-
Cobra is a CLI library for Go that empowers applications.
34-
This application is a tool to generate the needed files
35-
to quickly create a Cobra application.`,
3635
Run: func(cmd *cobra.Command, args []string) {
37-
if cmd.Flag("show").Value.String() == "true" {
36+
if cmd.Flag("silent").Value.String() == "false" {
3837
ShowProblem(args[0])
3938
}
4039

@@ -45,7 +44,7 @@ to quickly create a Cobra application.`,
4544
func init() {
4645
rootCmd.AddCommand(solveCmd)
4746

48-
solveCmd.Flags().BoolP("show", "s", true, "print problem statement before solving")
47+
solveCmd.Flags().BoolP("silent", "s", false, "do not print problem statement")
4948
solveCmd.Flags().StringP("editor", "e", "", "use editor to solve the problem")
5049
err := viper.BindPFlag("editor", solveCmd.Flags().Lookup("editor"))
5150

cmd/submit.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@ import (
2525
var submitCmd = &cobra.Command{
2626
Use: "submit",
2727
Args: cobra.ExactArgs(1),
28-
Short: "A brief description of your command",
29-
Long: `A longer description that spans multiple lines and likely contains examples
30-
and usage of using your command. For example:
28+
Example: "cses-cli submit 1068.Weird-Algorithm.cpp",
29+
Short: "submit the solution file and fetch the status",
30+
Long: `If the solution is ACCEPTED and github is configured, then it will push the solution to github,
31+
with limitation that the repo has to be public, since it uses github generated token`,
3132

32-
Cobra is a CLI library for Go that empowers applications.
33-
This application is a tool to generate the needed files
34-
to quickly create a Cobra application.`,
3533
Run: func(cmd *cobra.Command, args []string) {
3634
ghConfig := &GithubConfig{}
3735

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/google/go-github v17.0.0+incompatible
99
github.com/google/go-querystring v1.0.0 // indirect
1010
github.com/mitchellh/go-homedir v1.1.0
11-
github.com/sirupsen/logrus v1.8.1 // indirect
11+
github.com/sirupsen/logrus v1.8.1
1212
github.com/spf13/cobra v1.1.3
1313
github.com/spf13/viper v1.7.1
1414
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c

go.sum

-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,6 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
233233
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
234234
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
235235
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
236-
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
237236
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
238237
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
239238
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=

0 commit comments

Comments
 (0)