Skip to content

Commit

Permalink
add client command
Browse files Browse the repository at this point in the history
  • Loading branch information
paragor committed Aug 9, 2024
1 parent 82e36a4 commit d61a8a4
Show file tree
Hide file tree
Showing 9 changed files with 460 additions and 234 deletions.
90 changes: 84 additions & 6 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,111 @@ package cmd
import (
"encoding/json"
"fmt"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/paragor/todo/pkg/db"
"github.com/paragor/todo/pkg/models"
"github.com/spf13/cobra"
"log"
"net/http"
"strings"
"time"
)

var clientOutput = "table"

func init() {
rootCmd.AddCommand(clientCmd)
clientCmd.Flags().StringVarP(&clientOutput, "output", "o", clientOutput, "output format (json, table)")
}

var clientCmd = &cobra.Command{
Use: "client",
Short: "Run todolist console client",
Run: func(cmd *cobra.Command, args []string) {
if clientOutput != "json" && clientOutput != "table" {
log.Fatalln("unknown output format")
}
repo := db.NewRemoteRepository(cfg.Client.RemoteAddr, cfg.Client.ServerToken, http.DefaultClient)
if err := repo.Ping(); err != nil {
log.Fatalf("cant connect to server: %s", err.Error())
}
tasks, err := repo.All()

input, err := models.ParseHumanInput(strings.Join(args, " "))
if err != nil {
log.Fatalf("cant get tasks: %s", err.Error())
log.Fatalf("cant parse command: %s", err.Error())
}
var result []*models.Task
switch input.Action {
case models.HumanActionInfo:
task, err := repo.Get(*input.ActionUUID)
if err != nil {
log.Fatalf("cant fetch task: %s", err.Error())
}
result = []*models.Task{task}
case models.HumanActionAdd:
task := models.NewTask()
input.Options.ModifyTask(task)
if err := repo.Insert(task); err != nil {
log.Fatalf("cant insert task: %s", err.Error())
}
result = []*models.Task{task}
case models.HumanActionModify:
task, err := repo.Get(*input.ActionUUID)
if err != nil {
log.Fatalf("cant fetch task: %s", err.Error())
}
input.Options.ModifyTask(task)
if err := repo.Insert(task); err != nil {
log.Fatalf("cant insert task: %s", err.Error())
}
result = []*models.Task{task}
case models.HumanActionList:
tasks, err := repo.All()
if err != nil {
log.Fatalf("cant get tasks: %s", err.Error())
}
tasks = input.Options.ToListFilter().Apply(tasks)
result = tasks
default:
log.Fatalf("unkown action: %s", input.Action)
}
data, err := json.MarshalIndent(tasks, "", " ")
if err != nil {
log.Fatalf("cant marshal tasks: %s", err.Error())

if clientOutput == "json" {
fmt.Println(prettyOutputJson(result))
} else {
fmt.Println(prettyOutputTable(result))
}
fmt.Print(string(data))
},
}

func prettyOutputTable(tasks []*models.Task) string {
mbDate := func(date *time.Time) string {
if date == nil {
return ""
} else {
return date.In(time.Local).Format("2006-01-02 15:04")
}
}
tableWriter := table.NewWriter()
tableWriter.AppendHeader(table.Row{"uuid", "project", "description", "status", "tags", "due", "notify"})
for _, task := range tasks {
tableWriter.AppendRow(table.Row{
task.UUID.String(),
task.Project,
task.Description,
task.Status,
strings.Join(task.Tags, ", "),
mbDate(task.Due),
mbDate(task.Notify),
})
}
return tableWriter.Render()
}

func prettyOutputJson(tasks []*models.Task) string {
data, err := json.MarshalIndent(tasks, "", " ")
if err != nil {
log.Fatalf("cant marshal tasks: %s", err.Error())
}
return string(data)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jedib0t/go-pretty/v6 v6.5.9 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muhlemmer/gu v0.3.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/zitadel/logging v0.6.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jedib0t/go-pretty/v6 v6.5.9 h1:ACteMBRrrmm1gMsXe9PSTOClQ63IXDUt03H5U+UV8OU=
github.com/jedib0t/go-pretty/v6 v6.5.9/go.mod h1:zbn98qrYlh95FIhwwsbIip0LYpwSG8SUOScs+v9/t0E=
github.com/jeremija/gosubmit v0.2.7 h1:At0OhGCFGPXyjPYAsCchoBUhE099pcBXmsb4iZqROIc=
github.com/jeremija/gosubmit v0.2.7/go.mod h1:Ui+HS073lCFREXBbdfrJzMB57OI/bdxTiLtrDHHhFPI=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
Expand Down Expand Up @@ -315,6 +317,8 @@ github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcME
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=
Expand Down Expand Up @@ -374,6 +378,8 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
Expand Down
143 changes: 0 additions & 143 deletions pkg/commandparser/parser.go

This file was deleted.

71 changes: 0 additions & 71 deletions pkg/commandparser/parser_test.go

This file was deleted.

Loading

0 comments on commit d61a8a4

Please sign in to comment.