Skip to content

Commit

Permalink
bump gosn-v2 to reduce api calls and latency, and provide retry mechn…
Browse files Browse the repository at this point in the history
…ism. various refactoring.
  • Loading branch information
jonhadfield committed Jan 6, 2024
1 parent 65af183 commit 7296cc9
Show file tree
Hide file tree
Showing 30 changed files with 232 additions and 259 deletions.
6 changes: 3 additions & 3 deletions checklists.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import (
"github.com/jonhadfield/gosn-v2/items"
)

func conflictedWarning([]items.Tasklist) string {
if len(items.Tasklists{}) > 0 {
func conflictedWarning(tl []items.Tasklist) string {
if len(tl) > 0 {
return color.Yellow.Sprintf("%d conflicted versions", len(items.Tasklists{}))
}

return "-"
}

func (ci *ListChecklistsInput) Run() (err error) {
func (ci *ListChecklistsInput) Run() error {
checklists, err := getChecklists(ci.Session)
if err != nil {
return err
Expand Down
20 changes: 4 additions & 16 deletions cmd/sncli/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,9 @@ func cmdAdd() *cli.Command {
},
},
Action: func(c *cli.Context) error {
var opts configOptsOutput
opts, err := getOpts(c)
if err != nil {
return err
}
opts := getOpts(c)
// useStdOut = opts.useStdOut
err = processAddTags(c, opts)

return err
return processAddTags(c, opts)
},
},
{
Expand Down Expand Up @@ -90,15 +84,9 @@ func cmdAdd() *cli.Command {
},
},
Action: func(c *cli.Context) error {
opts, err := getOpts(c)
if err != nil {
return err
}
// useStdOut = opts.useStdOut

err = processAddNotes(c, opts)
opts := getOpts(c)

return err
return processAddNotes(c, opts)
},
},
},
Expand Down
19 changes: 9 additions & 10 deletions cmd/sncli/checklist.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package main

import (
"fmt"

"github.com/jonhadfield/gosn-v2/cache"
"github.com/jonhadfield/gosn-v2/common"
sncli "github.com/jonhadfield/sn-cli"
"github.com/urfave/cli/v2"
)

func cmdChecklist() *cli.Command {
return &cli.Command{
Name: "checklist",
Usage: "manage checklists",
Name: "checklist",
Usage: "manage checklists",
Hidden: true,
BashComplete: func(c *cli.Context) {
addTasks := []string{"list"}
if c.NArg() > 0 {
Expand All @@ -25,13 +28,9 @@ func cmdChecklist() *cli.Command {
Name: "list",
Usage: "list checklists",
Action: func(c *cli.Context) error {
var opts configOptsOutput
opts, err := getOpts(c)
if err != nil {
return err
}
var sess cache.Session
sess, _, err = cache.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug)
opts := getOpts(c)

sess, _, err := cache.GetSession(common.NewHTTPClient(), opts.useSession, opts.sessKey, opts.server, opts.debug)
if err != nil {
return err
}
Expand Down Expand Up @@ -63,7 +62,7 @@ func cmdChecklist() *cli.Command {
// },
// Action: func(c *cli.Context) error {
// var opts configOptsOutput
// opts, err := getOpts(c)
// opts := getOpts(c)
// if err != nil {
// return err
// }
Expand Down
11 changes: 2 additions & 9 deletions cmd/sncli/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,9 @@ func cmdDebug() *cli.Command {
str = c.Args().First()
}

var opts configOptsOutput
opts, err := getOpts(c)
if err != nil {
return err
}
// useStdOut = opts.useStdOut

var sess session.Session
opts := getOpts(c)

sess, _, err = session.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug)
sess, _, err := session.GetSession(nil, opts.useSession, opts.sessKey, opts.server, opts.debug)
if err != nil {
return err
}
Expand Down
31 changes: 6 additions & 25 deletions cmd/sncli/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,9 @@ func cmdDelete() *cli.Command {
},
},
Action: func(c *cli.Context) error {
opts, err := getOpts(c)
if err != nil {
return err
}

// useStdOut = opts.useStdOut
opts := getOpts(c)

err = processDeleteTags(c, opts)

return err
return processDeleteTags(c, opts)
},
},
{
Expand All @@ -78,14 +71,9 @@ func cmdDelete() *cli.Command {
},
},
Action: func(c *cli.Context) error {
opts, err := getOpts(c)
if err != nil {
return err
}
opts := getOpts(c)

err = processDeleteNote(c, opts)

return err
return processDeleteNote(c, opts)
},
},
{
Expand All @@ -107,16 +95,9 @@ func cmdDelete() *cli.Command {
},
},
Action: func(c *cli.Context) error {
opts, err := getOpts(c)
if err != nil {
return err
}

// useStdOut = opts.useStdOut

err = processDeleteItems(c, opts)
opts := getOpts(c)

return err
return processDeleteItems(c, opts)
},
},
},
Expand Down
12 changes: 2 additions & 10 deletions cmd/sncli/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ func cmdEdit() *cli.Command {
},
},
Action: func(c *cli.Context) error {
opts, err := getOpts(c)
if err != nil {
return err
}
// useStdOut = opts.useStdOut
opts := getOpts(c)

return processEditTag(c, opts)
},
Expand Down Expand Up @@ -80,12 +76,8 @@ func cmdEdit() *cli.Command {
},
},
Action: func(c *cli.Context) error {
opts, err := getOpts(c)
if err != nil {
return err
}
opts := getOpts(c)
// useStdOut = opts.useStdOut

return processEditNote(c, opts)
},
},
Expand Down
38 changes: 9 additions & 29 deletions cmd/sncli/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/jonhadfield/gosn-v2/cache"
"github.com/jonhadfield/gosn-v2/common"
"github.com/jonhadfield/gosn-v2/items"
sncli "github.com/jonhadfield/sn-cli"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -46,10 +47,7 @@ func cmdGet() *cli.Command {
return err
},
Action: func(c *cli.Context) error {
opts, err := getOpts(c)
if err != nil {
return err
}
opts := getOpts(c)

// useStdOut = opts.useStdOut

Expand All @@ -67,7 +65,7 @@ func cmdGet() *cli.Command {
},
}

sess, _, err := cache.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug)
sess, _, err := cache.GetSession(common.NewHTTPClient(), opts.useSession, opts.sessKey, opts.server, opts.debug)
if err != nil {
return err
}
Expand Down Expand Up @@ -210,15 +208,9 @@ func cmdGet() *cli.Command {
return err
},
Action: func(c *cli.Context) error {
opts, err := getOpts(c)
if err != nil {
return err
}
// useStdOut = opts.useStdOut

err = processGetTags(c, opts)
opts := getOpts(c)

return err
return processGetTags(c, opts)
},
},
{
Expand Down Expand Up @@ -261,15 +253,9 @@ func cmdGet() *cli.Command {
},
},
Action: func(c *cli.Context) error {
opts, err := getOpts(c)
if err != nil {
return err
}
opts := getOpts(c)
// useStdOut = opts.useStdOut

err = processGetNotes(c, opts)

return err
return processGetNotes(c, opts)
},
},
{
Expand Down Expand Up @@ -297,10 +283,7 @@ func cmdGet() *cli.Command {
},
},
Action: func(c *cli.Context) error {
opts, err := getOpts(c)
if err != nil {
return err
}
opts := getOpts(c)

// useStdOut = opts.useStdOut

Expand Down Expand Up @@ -340,10 +323,7 @@ func cmdGet() *cli.Command {
},
},
Action: func(c *cli.Context) error {
opts, err := getOpts(c)
if err != nil {
return err
}
opts := getOpts(c)
// useStdOut = opts.useStdOut

return processGetTrash(c, opts)
Expand Down
9 changes: 2 additions & 7 deletions cmd/sncli/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,12 @@ func cmdHealthcheck() *cli.Command {
},
},
Action: func(c *cli.Context) error {
var opts configOptsOutput
opts, err := getOpts(c)
if err != nil {
return err
}
opts := getOpts(c)
// useStdOut = opts.useStdOut

var sess session.Session

sess, _, err = session.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug)

sess, _, err := session.GetSession(nil, opts.useSession, opts.sessKey, opts.server, opts.debug)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/sncli/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/jonhadfield/gosn-v2/cache"
"github.com/jonhadfield/gosn-v2/common"
"github.com/jonhadfield/gosn-v2/items"
sncli "github.com/jonhadfield/sn-cli"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -33,7 +34,7 @@ func processGetItems(c *cli.Context, opts configOptsOutput) (err error) {

var sess cache.Session

sess, _, err = cache.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug)
sess, _, err = cache.GetSession(common.NewHTTPClient(), opts.useSession, opts.sessKey, opts.server, opts.debug)
if err != nil {
return err
}
Expand Down Expand Up @@ -75,8 +76,8 @@ func processGetItems(c *cli.Context, opts configOptsOutput) (err error) {

output = c.String("output")

output = "json"
var bOutput []byte

switch strings.ToLower(output) {
case "json":
bOutput, err = json.MarshalIndent(rawItems, "", " ")
Expand Down
14 changes: 6 additions & 8 deletions cmd/sncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import (
)

const (
snAppName = "sn-cli"
msgAddSuccess = "added"
msgAlreadyExisting = "already existing"
msgDeleted = "deleted"
msgMultipleNotesFoundWithSameTitle = "multiple notes found with the same title"
msgNoteAdded = "note added"
msgNoteDeleted = "note deleted"
msgFailedToDeleteNote = "failed to delete note"
msgNoteNotFound = "note not found"
msgTagSuccess = "tagged"
msgTagSuccess = "item tagged"
msgTagAlreadyExists = "tag already exists"
msgTagAdded = "tag added"
msgTagDeleted = "tag deleted"
Expand All @@ -32,7 +31,6 @@ const (
msgItemsDeleted = "items deleted"
msgNoMatches = "no matches"
msgRegisterSuccess = "registered"
snAppName = "sn-cli"
)

var yamlAbbrevs = []string{"yml", "yaml"}
Expand All @@ -58,7 +56,7 @@ type configOptsOutput struct {
debug bool
}

func getOpts(c *cli.Context) (out configOptsOutput, err error) {
func getOpts(c *cli.Context) (out configOptsOutput) {
out.useStdOut = true

if c.Bool("no-stdout") {
Expand Down Expand Up @@ -89,7 +87,7 @@ func getOpts(c *cli.Context) (out configOptsOutput, err error) {
return
}

func appSetup() (app *cli.App, err error) {
func appSetup() (app *cli.App) {
viper.SetEnvPrefix("sn")
viper.AutomaticEnv()

Expand Down Expand Up @@ -144,11 +142,11 @@ func appSetup() (app *cli.App, err error) {
cli.ShowAppHelpAndExit(c, 1)
}

return app, err
return app
}

func startCLI(args []string) (err error) {
app, _ := appSetup()
app := appSetup()

sort.Sort(cli.FlagsByName(app.Flags))

Expand Down
Loading

0 comments on commit 7296cc9

Please sign in to comment.