Skip to content

add a new command to set/query a task's category #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ you can restore a specific version of a file by copying it from the subdirectory

---

### 🏁 <u>ca</u>pture
### 🏁 <u>cap</u>ture

```
$ ret capture [flag]
Expand All @@ -107,6 +107,24 @@ flags are stored in the `.ret/flag.json` file

---

### 😼 <u>cat</u>egory

```
$ ret category [task-category]
```

set or query a task's category with ret

supply no arguments to see the current category

note that task metadata is stored in hidden directory `.ret` and therefore scoped to the cwd

task metadata is stored in the `.ret/task.json` file

🔗 https://github.com/rerrorctf/ret/blob/main/commands/category.go

---

### 📢 <u>cha</u>t

```
Expand Down
63 changes: 63 additions & 0 deletions commands/category.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package commands

import (
"fmt"
"ret/config"
"ret/theme"
"ret/util"
)

func init() {
Commands = append(Commands, Command{
Name: "category",
Emoji: "😼",
Func: Category,
Help: CategoryHelp,
Arguments: []Argument{
{
Name: "task-category",
Optional: true,
List: false,
},
},
SeeAlso: nil})
}

func CategoryHelp() string {
return "set or query a task's category with ret\n\n" +
"supply no arguments to see the current category\n\n" +
"note that task metadata is stored in hidden directory " + theme.ColorCyan + "`.ret`" + theme.ColorReset + " and therefore scoped to the cwd\n\n" +
"task metadata is stored in the " + theme.ColorCyan + "`" + config.TaskFileName + "`" + theme.ColorReset + " file\n"
}

func displayCurrentTaskCategory() {
category := util.GetCurrentTaskCategory()
if len(category) == 0 {
return
}

fmt.Printf("😼 "+theme.ColorBlue+"%s"+theme.ColorReset+"\n", category)
}

func setCurrentTaskCategory(newCategory string) {
oldCategory := util.GetCurrentTaskCategory()

if len(oldCategory) > 0 {
fmt.Printf(theme.ColorGray+"😼 changing category from: "+theme.ColorRed+"%s"+theme.ColorGray+" to: "+theme.ColorGreen+"%s"+theme.ColorReset+"\n", oldCategory, newCategory)
} else {
fmt.Printf(theme.ColorGray+"😼 setting category to: "+theme.ColorGreen+"%s"+theme.ColorReset+"\n", newCategory)
}

util.SetCurrentTaskCategory(newCategory)
}

func Category(args []string) {
if len(args) == 0 {
displayCurrentTaskCategory()
return
}

util.EnsureSkeleton()

setCurrentTaskCategory(args[0])
}
3 changes: 2 additions & 1 deletion data/task.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package data

type Task struct {
Name string `json:"name"`
Name string `json:"name"`
Category string `json:"category"`
}
11 changes: 11 additions & 0 deletions util/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,14 @@ func SetCurrentTaskName(name string) {
task.Name = name
SetCurrentTask(&task)
}

func GetCurrentTaskCategory() string {
task := GetCurrentTask()
return task.Category
}

func SetCurrentTaskCategory(category string) {
task := GetCurrentTask()
task.Category = category
SetCurrentTask(&task)
}