Skip to content

Commit

Permalink
Don't output usage if args are correct
Browse files Browse the repository at this point in the history
- Added 'cmd.SilenceUsage = true' consistently so that the usage is output if the args are incorrect, but not if there are other problems, e.g. communication error. See spf13/cobra#340 for background on why it's done this way.
- Added a few missing argument validations.
- Nit in terminology: changed "get" to "list" and "API" to "server"
  • Loading branch information
deboer-tim committed Jun 18, 2021
1 parent ff52813 commit 03c29ed
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 8 deletions.
6 changes: 4 additions & 2 deletions commands/clar.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (

var clarCommand = &cobra.Command{
Use: "clar",
Short: "Get clarifications",
Short: "List clarifications",
Args: cobra.NoArgs,
RunE: fetchClars,
PreRunE: configHelper("baseurl"),
}

func fetchClars(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
api, err := contestApi()
if err != nil {
return fmt.Errorf("could not connect to the API; %w", err)
return fmt.Errorf("could not connect to the server; %w", err)
}

clars, err := api.Clarifications()
Expand Down
2 changes: 1 addition & 1 deletion commands/contest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

var contestCommand = &cobra.Command{
Use: "contest",
Short: "Get contests",
Short: "List contests",
RunE: fetchContests,
}

Expand Down
1 change: 1 addition & 0 deletions commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var loginCommand = &cobra.Command{
}

func login(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
viper.Set("username", args[0])
viper.Set("password", args[1])
if err := viper.WriteConfigAs(configFile()); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions commands/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package commands

import (
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -14,6 +15,7 @@ var logoutCommand = &cobra.Command{
}

func logout(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
viper.Set("username", "")
viper.Set("password", "")
if err := viper.WriteConfigAs(configFile()); err != nil {
Expand Down
1 change: 1 addition & 0 deletions commands/post_clar.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var postClarCommand = &cobra.Command{
}

func postClarification(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
api, err := contestApi()
if err != nil {
return fmt.Errorf("could not connect to the server; %w", err)
Expand Down
6 changes: 4 additions & 2 deletions commands/problem.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import (

var problemCommand = &cobra.Command{
Use: "problem",
Short: "Get problems",
Short: "List problems",
Args: cobra.NoArgs,
RunE: fetchProblems,
PreRunE: configHelper("baseurl"),
}

func fetchProblems(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
api, err := contestApi()
if err != nil {
return fmt.Errorf("could not connect to the API; %w", err)
return fmt.Errorf("could not connect to the server; %w", err)
}

p, err := api.Problems()
Expand Down
4 changes: 3 additions & 1 deletion commands/scoreboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
var scoreboardCommand = &cobra.Command{
Use: "scoreboard",
Short: "Show the contest scoreboard",
Args: cobra.NoArgs,
RunE: scoreboard,
PreRunE: configHelper("baseurl"),
}

func scoreboard(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
api, err := contestApi()
if err != nil {
return fmt.Errorf("could not connect to the API; %w", err)
return fmt.Errorf("could not connect to the server; %w", err)
}

problems, err := api.Problems()
Expand Down
2 changes: 2 additions & 0 deletions commands/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var setIdCommand = &cobra.Command{
}

func setUrl(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
viper.Set("baseurl", args[0])
if err := viper.WriteConfigAs(configFile()); err != nil {
return err
Expand All @@ -40,6 +41,7 @@ func setUrl(cmd *cobra.Command, args []string) error {
}

func setId(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
viper.Set("contest", args[0])
if err := viper.WriteConfigAs(configFile()); err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions commands/submissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (

var submissionsCommand = &cobra.Command{
Use: "submissions",
Short: "Shows past submissions and their judgements",
Short: "List past submissions and their judgements",
Args: cobra.NoArgs,
RunE: submissions,
PreRunE: configHelper("baseurl"),
}

func submissions(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
api, err := contestApi()
if err != nil {
return fmt.Errorf("could not connect to the API; %w", err)
return fmt.Errorf("could not connect to the server; %w", err)
}

// Get the problems, languages, and judgementTypes
Expand Down
1 change: 1 addition & 0 deletions commands/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var submitCommand = &cobra.Command{
}

func submit(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
api, err := contestApi()
if err != nil {
return fmt.Errorf("could not connect to the server; %w", err)
Expand Down

0 comments on commit 03c29ed

Please sign in to comment.