Skip to content

Commit

Permalink
Implement go command to print contest directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Aya Igarashi committed Jan 20, 2022
1 parent 0f77700 commit 531d277
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Usage:
hack [command]
Available Commands:
go Print path to the directory
help Help about any command
init Create directories and download samples
open Open current task page
Expand All @@ -38,6 +39,7 @@ Write configuration and save it as a `~/.config/hack` file.
atcoder:
pass: <password>
user: <username>
basedir: <path/to/directory>
```

Initialize contest directory and download samples.
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/spf13/cobra"

"github.com/Ladicle/hack/pkg/cmd/goo"
"github.com/Ladicle/hack/pkg/cmd/ini"
"github.com/Ladicle/hack/pkg/cmd/open"
"github.com/Ladicle/hack/pkg/cmd/test"
Expand Down Expand Up @@ -34,6 +35,7 @@ func Run() error {
cmd.AddCommand(ini.NewCommand(f, out))
cmd.AddCommand(test.NewCommand(f, out))
cmd.AddCommand(open.NewCommand())
cmd.AddCommand(goo.NewCommand(f, out))

cobra.OnInitialize(func() {
config.MustUnmarshal(path, f)
Expand Down
57 changes: 57 additions & 0 deletions pkg/cmd/goo/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package goo

import (
"fmt"
"io"
"path/filepath"

"github.com/Ladicle/hack/pkg/config"
"github.com/Ladicle/hack/pkg/contest"
"github.com/spf13/cobra"
)

type Options struct {
contestID string
taskID string
}

func NewCommand(f *config.File, out io.Writer) *cobra.Command {
return &cobra.Command{
Use: "go [<CONTEST>] [<TASK>]",
Aliases: []string{"g"},
Short: "Print path to the directory",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
var opts Options
if err := opts.Validate(args); err != nil {
return err
}
return opts.Run(f, out)
},
}
}

func (o *Options) Validate(args []string) error {
switch len(args) {
case 0:
// noop
case 1:
o.contestID = args[0]
case 2:
o.contestID = args[0]
o.taskID = args[1]
}
return nil
}

func (o *Options) Run(f *config.File, out io.Writer) error {
path := contest.GetAtCoderDir(f.BaseDir)
if o.contestID != "" {
path = filepath.Join(path, o.contestID)
}
if o.taskID != "" {
path = filepath.Join(path, o.taskID)
}
_, err := fmt.Fprint(out, path)
return err
}
12 changes: 10 additions & 2 deletions pkg/contest/atcoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"github.com/Ladicle/hack/pkg/session"
)

const atCoderHost = "atcoder.jp"
const (
atCoderDir = "atcoder"
atCoderHost = "atcoder.jp"
)

type AtCoder struct {
ContestID string
Expand Down Expand Up @@ -152,9 +155,14 @@ func GetTaskID(dir string) string {
return fmt.Sprintf("%s_%s", contestID, filepath.Base(dir))
}

// GetAtCoderDir return the AtCoder directory name.
func GetAtCoderDir(baseDir string) string {
return filepath.Join(baseDir, atCoderDir)
}

// GetContestDir return the contest directory name.
func GetContestDir(baseDir, contestID string) string {
return filepath.Join(baseDir, "atcoder", contestID)
return filepath.Join(baseDir, atCoderDir, contestID)
}

// GetTaskDir return the task directory name.
Expand Down

0 comments on commit 531d277

Please sign in to comment.