diff --git a/checklists.go b/checklists.go index 6b97aba..dee36db 100644 --- a/checklists.go +++ b/checklists.go @@ -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 diff --git a/cmd/sncli/add.go b/cmd/sncli/add.go index 4498dce..d48ed4c 100644 --- a/cmd/sncli/add.go +++ b/cmd/sncli/add.go @@ -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) }, }, { @@ -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) }, }, }, diff --git a/cmd/sncli/checklist.go b/cmd/sncli/checklist.go index 4522dea..1fce473 100644 --- a/cmd/sncli/checklist.go +++ b/cmd/sncli/checklist.go @@ -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 { @@ -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 } @@ -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 // } diff --git a/cmd/sncli/debug.go b/cmd/sncli/debug.go index ce809ba..637b328 100644 --- a/cmd/sncli/debug.go +++ b/cmd/sncli/debug.go @@ -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 } diff --git a/cmd/sncli/delete.go b/cmd/sncli/delete.go index e9d7486..f8c19af 100644 --- a/cmd/sncli/delete.go +++ b/cmd/sncli/delete.go @@ -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) }, }, { @@ -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) }, }, { @@ -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) }, }, }, diff --git a/cmd/sncli/edit.go b/cmd/sncli/edit.go index 7bceceb..3ef13d4 100644 --- a/cmd/sncli/edit.go +++ b/cmd/sncli/edit.go @@ -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) }, @@ -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) }, }, diff --git a/cmd/sncli/get.go b/cmd/sncli/get.go index 321fe0f..f2b1ca6 100644 --- a/cmd/sncli/get.go +++ b/cmd/sncli/get.go @@ -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" @@ -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 @@ -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 } @@ -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) }, }, { @@ -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) }, }, { @@ -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 @@ -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) diff --git a/cmd/sncli/healthcheck.go b/cmd/sncli/healthcheck.go index 70aaad5..b25760f 100644 --- a/cmd/sncli/healthcheck.go +++ b/cmd/sncli/healthcheck.go @@ -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 } diff --git a/cmd/sncli/item.go b/cmd/sncli/item.go index 284fca7..81dcd39 100644 --- a/cmd/sncli/item.go +++ b/cmd/sncli/item.go @@ -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" @@ -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 } @@ -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, "", " ") diff --git a/cmd/sncli/main.go b/cmd/sncli/main.go index 877e4db..9e0f2fb 100644 --- a/cmd/sncli/main.go +++ b/cmd/sncli/main.go @@ -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" @@ -32,7 +31,6 @@ const ( msgItemsDeleted = "items deleted" msgNoMatches = "no matches" msgRegisterSuccess = "registered" - snAppName = "sn-cli" ) var yamlAbbrevs = []string{"yml", "yaml"} @@ -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") { @@ -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() @@ -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)) diff --git a/cmd/sncli/main_test.go b/cmd/sncli/main_test.go index e9c8838..b489821 100644 --- a/cmd/sncli/main_test.go +++ b/cmd/sncli/main_test.go @@ -92,7 +92,6 @@ func signIn(server, email, password string) { CacheDB: nil, CacheDBPath: "", } - } func TestMain(m *testing.M) { diff --git a/cmd/sncli/note.go b/cmd/sncli/note.go index 9fd5207..6b1a35b 100644 --- a/cmd/sncli/note.go +++ b/cmd/sncli/note.go @@ -4,7 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/jonhadfield/gosn-v2/common" "io" "io/ioutil" "os" @@ -12,11 +11,11 @@ import ( "strings" "time" + "github.com/asdine/storm/v3/q" "github.com/divan/num2words" "github.com/gookit/color" - - "github.com/asdine/storm/v3/q" "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" @@ -183,7 +182,7 @@ func processEditNote(c *cli.Context, opts configOptsOutput) (err error) { } var cSession cache.Session - cSession, _, err = cache.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug) + cSession, _, err = cache.GetSession(common.NewHTTPClient(), opts.useSession, opts.sessKey, opts.server, opts.debug) if err != nil { return err @@ -382,7 +381,7 @@ func processGetNotes(c *cli.Context, opts configOptsOutput) (err error) { } } - session, _, err := cache.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug) + session, _, err := cache.GetSession(common.NewHTTPClient(), opts.useSession, opts.sessKey, opts.server, opts.debug) if err != nil { return err } @@ -471,7 +470,7 @@ func processGetTrash(c *cli.Context, opts configOptsOutput) (err error) { } } - session, _, err := cache.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug) + session, _, err := cache.GetSession(common.NewHTTPClient(), opts.useSession, opts.sessKey, opts.server, opts.debug) if err != nil { return err } @@ -634,7 +633,7 @@ func processAddNotes(c *cli.Context, opts configOptsOutput) (err error) { } // get session - session, _, err := cache.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug) + session, _, err := cache.GetSession(common.NewHTTPClient(), opts.useSession, opts.sessKey, opts.server, opts.debug) if err != nil { return err } @@ -675,7 +674,7 @@ func processDeleteNote(c *cli.Context, opts configOptsOutput) (err error) { return errors.New("") } - 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 } @@ -716,7 +715,7 @@ func processDeleteNote(c *cli.Context, opts configOptsOutput) (err error) { func processDeleteItems(c *cli.Context, opts configOptsOutput) (err error) { uuid := strings.TrimSpace(c.String("uuid")) - 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 } diff --git a/cmd/sncli/note_test.go b/cmd/sncli/note_test.go index 4d11e2f..aefad68 100644 --- a/cmd/sncli/note_test.go +++ b/cmd/sncli/note_test.go @@ -11,13 +11,20 @@ import ( func TestAddDeleteNote(t *testing.T) { time.Sleep(250 * time.Millisecond) + var outputBuffer bytes.Buffer - app, err := appSetup() - require.NoError(t, err) + + app := appSetup() + app.Writer = &outputBuffer + osArgs := []string{"sncli", "add", "note", "--title", "testNote", "--text", "testAddNote"} - err = app.Run(osArgs) + + err := app.Run(osArgs) + require.NoError(t, err) + stdout := outputBuffer.String() + fmt.Println(stdout) require.NoError(t, err) require.Contains(t, stdout, msgAddSuccess) @@ -34,11 +41,14 @@ func TestAddDeleteNote(t *testing.T) { func TestGetMissingNote(t *testing.T) { time.Sleep(250 * time.Millisecond) var outputBuffer bytes.Buffer - app, err := appSetup() - require.NoError(t, err) + app := appSetup() + app.Writer = &outputBuffer osArgs := []string{"sncli", "get", "note", "--title", "missing note"} - err = app.Run(osArgs) + + err := app.Run(osArgs) + require.NoError(t, err) + stdout := outputBuffer.String() fmt.Println(stdout) require.NoError(t, err) @@ -48,15 +58,13 @@ func TestGetMissingNote(t *testing.T) { func TestDeleteNonExistantNote(t *testing.T) { time.Sleep(250 * time.Millisecond) var outputBuffer bytes.Buffer - app, err := appSetup() - require.NoError(t, err) + app := appSetup() app.Writer = &outputBuffer outputBuffer.Reset() - osArgs := []string{"sncli", "delete", "note", "--title", "testNote"} - err = app.Run(osArgs) + require.NoError(t, app.Run([]string{"sncli", "delete", "note", "--title", "testNote"})) + stdout := outputBuffer.String() fmt.Println(stdout) - require.NoError(t, err) require.Contains(t, stdout, msgNoteNotFound) } diff --git a/cmd/sncli/register.go b/cmd/sncli/register.go index 27854d4..5336ef0 100644 --- a/cmd/sncli/register.go +++ b/cmd/sncli/register.go @@ -26,17 +26,12 @@ func cmdRegister() *cli.Command { }, }, Action: func(c *cli.Context) error { - var opts configOptsOutput - opts, err := getOpts(c) - if err != nil { - return err - } - - // useStdOut = opts.useStdOut + opts := getOpts(c) + var err error if strings.TrimSpace(c.String("email")) == "" { - if cErr := cli.ShowCommandHelp(c, "register"); cErr != nil { - panic(cErr) + if err = cli.ShowCommandHelp(c, "register"); err != nil { + panic(err) } return errors.New("email required") @@ -53,10 +48,10 @@ func cmdRegister() *cli.Command { APIServer: opts.server, Debug: opts.debug, } - err = registerConfig.Run() - if err != nil { + if err = registerConfig.Run(); err != nil { return err } + fmt.Println(msgRegisterSuccess) return nil diff --git a/cmd/sncli/resync.go b/cmd/sncli/resync.go index de6e3e0..eddae01 100644 --- a/cmd/sncli/resync.go +++ b/cmd/sncli/resync.go @@ -2,6 +2,7 @@ package main import ( "github.com/jonhadfield/gosn-v2/cache" + "github.com/jonhadfield/gosn-v2/common" sncli "github.com/jonhadfield/sn-cli" "github.com/urfave/cli/v2" ) @@ -11,13 +12,9 @@ func cmdResync() *cli.Command { Name: "resync", Usage: "purge cache and resync content", Action: func(c *cli.Context) error { - var opts configOptsOutput - opts, err := getOpts(c) - if err != nil { - return err - } - var session cache.Session - session, _, err = cache.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug) + opts := getOpts(c) + + session, _, err := cache.GetSession(common.NewHTTPClient(), opts.useSession, opts.sessKey, opts.server, opts.debug) if err != nil { return err } diff --git a/cmd/sncli/session.go b/cmd/sncli/session.go index 471b991..a420b59 100644 --- a/cmd/sncli/session.go +++ b/cmd/sncli/session.go @@ -33,12 +33,7 @@ func cmdSession() *cli.Command { }, Hidden: false, Action: func(c *cli.Context) error { - var opts configOptsOutput - opts, err := getOpts(c) - if err != nil { - return err - } - // useStdOut = opts.useStdOut + opts := getOpts(c) return processSession(c, opts) }, @@ -67,7 +62,7 @@ func processSession(c *cli.Context, opts configOptsOutput) (err error) { if sAdd { var msg string - msg, err = session.AddSession(opts.server, sessKey, nil, opts.debug) + msg, err = session.AddSession(nil, opts.server, sessKey, nil, opts.debug) if err != nil { return err } @@ -117,7 +112,7 @@ func processSession(c *cli.Context, opts configOptsOutput) (err error) { // }, // Action: func(c *cli.Context) error { // var opts configOptsOutput -// opts, err := getOpts(c) +// opts := getOpts(c) // if err != nil { // return err // } diff --git a/cmd/sncli/stats.go b/cmd/sncli/stats.go index 8958a46..2b1cc63 100644 --- a/cmd/sncli/stats.go +++ b/cmd/sncli/stats.go @@ -2,6 +2,7 @@ package main import ( "github.com/jonhadfield/gosn-v2/cache" + "github.com/jonhadfield/gosn-v2/common" sncli "github.com/jonhadfield/sn-cli" "github.com/urfave/cli/v2" ) @@ -11,20 +12,18 @@ func cmdStats() *cli.Command { Name: "stats", Usage: "show statistics", 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 } + sess.CacheDBPath, err = cache.GenCacheDBPath(sess, opts.cacheDBDir, snAppName) if err != nil { return err } + statsConfig := sncli.StatsConfig{ Session: sess, } diff --git a/cmd/sncli/tag.go b/cmd/sncli/tag.go index e2f142f..bfb7772 100644 --- a/cmd/sncli/tag.go +++ b/cmd/sncli/tag.go @@ -5,13 +5,13 @@ import ( "encoding/json" "errors" "fmt" - "github.com/jonhadfield/gosn-v2/common" "os" "strings" "github.com/asdine/storm/v3/q" "github.com/gookit/color" "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" @@ -118,7 +118,7 @@ func processEditTag(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 } @@ -259,7 +259,7 @@ func processGetTags(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 } @@ -393,7 +393,7 @@ func processAddTags(c *cli.Context, opts configOptsOutput) (err error) { } // get session - session, _, err := cache.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug) + session, _, err := cache.GetSession(common.NewHTTPClient(), opts.useSession, opts.sessKey, opts.server, opts.debug) if err != nil { return err } @@ -426,6 +426,7 @@ func processAddTags(c *cli.Context, opts configOptsOutput) (err error) { // present results if len(ato.Added) > 0 { _, _ = fmt.Fprintf(c.App.Writer, color.Green.Sprint(msgTagAdded+": ", strings.Join(ato.Added, ", "))) + return err } @@ -449,7 +450,7 @@ func processTagItems(c *cli.Context, opts configOptsOutput) (err error) { findTag := c.String("find-tag") newTags := c.String("title") - 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 } @@ -498,7 +499,7 @@ func processDeleteTags(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 } @@ -572,15 +573,15 @@ func cmdTag() *cli.Command { }, }, Action: func(c *cli.Context) error { - opts, err := getOpts(c) - if err != nil { + opts := getOpts(c) + + if err := processTagItems(c, opts); err != nil { return err } - // useStdOut = opts.useStdOut - err = processTagItems(c, opts) + _, _ = fmt.Fprintf(c.App.Writer, color.Green.Sprintf("%s", msgTagSuccess)) - return err + return nil }, } } diff --git a/cmd/sncli/tag_test.go b/cmd/sncli/tag_test.go index 038b914..64609ca 100644 --- a/cmd/sncli/tag_test.go +++ b/cmd/sncli/tag_test.go @@ -12,38 +12,31 @@ import ( func TestGetMissingTag(t *testing.T) { time.Sleep(250 * time.Millisecond) var outputBuffer bytes.Buffer - app, err := appSetup() - require.NoError(t, err) + app := appSetup() app.Writer = &outputBuffer - osArgs := []string{"sncli", "get", "tag", "--title", "missing tag"} - err = app.Run(osArgs) + + require.NoError(t, app.Run([]string{"sncli", "get", "tag", "--title", "missing tag"})) stdout := outputBuffer.String() fmt.Println(stdout) - require.NoError(t, err) require.Contains(t, stdout, msgNoMatches) } func TestAddTag(t *testing.T) { time.Sleep(250 * time.Millisecond) var outputBuffer bytes.Buffer - app, err := appSetup() - require.NoError(t, err) + app := appSetup() app.Writer = &outputBuffer - osArgs := []string{"sncli", "add", "tag", "--title", "testAddOneTagGetCount"} - err = app.Run(osArgs) + require.NoError(t, app.Run([]string{"sncli", "add", "tag", "--title", "testAddOneTagGetCount"})) stdout := outputBuffer.String() fmt.Println(stdout) - require.NoError(t, err) require.Contains(t, stdout, msgAddSuccess) outputBuffer.Reset() - osArgs = []string{"sncli", "add", "tag", "--title", "testAddOneTagGetCount"} - err = app.Run(osArgs) + require.NoError(t, app.Run([]string{"sncli", "add", "tag", "--title", "testAddOneTagGetCount"})) stdout = outputBuffer.String() fmt.Println(stdout) - require.NoError(t, err) require.Contains(t, stdout, msgTagAlreadyExists) // err := startCLI([]string{"sncli", "--debug", "--no-stdout", "wipe", "--yes"}) @@ -59,7 +52,7 @@ func TestAddTag(t *testing.T) { func TestAddGetTag(t *testing.T) { time.Sleep(250 * time.Millisecond) var outputBuffer bytes.Buffer - app, _ := appSetup() + app := appSetup() app.Writer = &outputBuffer // wipe @@ -101,7 +94,7 @@ func TestAddGetTag(t *testing.T) { func TestAddDeleteTag(t *testing.T) { time.Sleep(250 * time.Millisecond) var outputBuffer bytes.Buffer - app, _ := appSetup() + app := appSetup() app.Writer = &outputBuffer // wipe diff --git a/cmd/sncli/wipe.go b/cmd/sncli/wipe.go index ead2444..e9aa62b 100644 --- a/cmd/sncli/wipe.go +++ b/cmd/sncli/wipe.go @@ -4,6 +4,7 @@ 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" ) @@ -23,19 +24,15 @@ func cmdWipe() *cli.Command { }, }, Action: func(c *cli.Context) error { - var opts configOptsOutput - opts, err := getOpts(c) - if err != nil { - return err - } + opts := getOpts(c) - var cacheSession cache.Session - cacheSession, _, err = cache.GetSession(opts.useSession, opts.sessKey, opts.server, opts.debug) + cacheSession, _, err := cache.GetSession(common.NewHTTPClient(), opts.useSession, opts.sessKey, opts.server, opts.debug) if err != nil { return err } var cacheDBPath string + cacheDBPath, err = cache.GenCacheDBPath(cacheSession, opts.cacheDBDir, snAppName) if err != nil { return err @@ -73,7 +70,7 @@ func cmdWipe() *cli.Command { return nil } - return err + return nil }, } } diff --git a/cmd/sncli/wipe_test.go b/cmd/sncli/wipe_test.go index f1b4523..e0c282c 100644 --- a/cmd/sncli/wipe_test.go +++ b/cmd/sncli/wipe_test.go @@ -11,7 +11,7 @@ import ( func TestWipe(t *testing.T) { time.Sleep(250 * time.Millisecond) var outputBuffer bytes.Buffer - app, _ := appSetup() + app := appSetup() app.Writer = &outputBuffer osArgs := []string{"sncli", "wipe", "--yes"} diff --git a/go.mod b/go.mod index 77a0bb3..c6bb20f 100644 --- a/go.mod +++ b/go.mod @@ -45,7 +45,7 @@ require ( github.com/divan/num2words v0.0.0-20170904212200-57dba452f942 github.com/dustin/go-humanize v1.0.1 github.com/gookit/color v1.5.4 - github.com/jonhadfield/gosn-v2 v0.0.0-20231220100241-fba7cf1f2185 + github.com/jonhadfield/gosn-v2 v0.0.0-20240106151742-c861fe8116e5 github.com/urfave/cli/v2 v2.26.0 ) @@ -57,6 +57,7 @@ require ( github.com/hashicorp/go-retryablehttp v0.7.5 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect github.com/pelletier/go-toml/v2 v2.1.1 // indirect + github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect github.com/rivo/uniseg v0.4.4 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect @@ -66,8 +67,12 @@ require ( github.com/sourcegraph/conc v0.3.0 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e // indirect + go.elara.ws/pcre v0.0.0-20230805032557-4ce849193f64 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 // indirect + modernc.org/libc v1.16.8 // indirect + modernc.org/mathutil v1.4.1 // indirect + modernc.org/memory v1.1.1 // indirect ) //replace github.com/jonhadfield/gosn-v2 => ../gosn-v2 diff --git a/go.sum b/go.sum index 1d6c384..bfcd1e9 100644 --- a/go.sum +++ b/go.sum @@ -22,6 +22,7 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1 github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/divan/num2words v0.0.0-20170904212200-57dba452f942 h1:fJ8/Lid8fF4i7Bwl7vWKvG2KeZzr3yU4qG6h/DPdXLU= github.com/divan/num2words v0.0.0-20170904212200-57dba452f942/go.mod h1:K88GQWK1aAiPMo9q2LZwyKBfEGnge7kmVVTUcZ61HSc= +github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= @@ -39,8 +40,10 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= @@ -56,10 +59,11 @@ github.com/hashicorp/go-retryablehttp v0.7.5 h1:bJj+Pj19UZMIweq/iie+1u5YCdGrnxCT github.com/hashicorp/go-retryablehttp v0.7.5/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/jonhadfield/gosn-v2 v0.0.0-20231220100241-fba7cf1f2185 h1:SZwVxLP1oIumWkpQOF4dpAi2sk5c6iw5efoYEC2akUA= -github.com/jonhadfield/gosn-v2 v0.0.0-20231220100241-fba7cf1f2185/go.mod h1:zaqVJQbjrhGWX0/UWY+Fp9oOJVBe7J/0DJmNmiuifs4= +github.com/jonhadfield/gosn-v2 v0.0.0-20240106151742-c861fe8116e5 h1:Q9wjzRxPFCpywQzdy2A4JIswIXAfDZ6nEUuIwFS+eFA= +github.com/jonhadfield/gosn-v2 v0.0.0-20240106151742-c861fe8116e5/go.mod h1:X1Is5S5KW0ZoS29YvRbZpUPhicmUAhxz0FlIglx+GMs= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -74,6 +78,7 @@ github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2 h1:JAEbJn3j/FrhdWA9jW8 github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2/go.mod h1:0KeJpeMD6o+O4hW7qJOT7vyQPKrWmj26uf5wMc/IiIs= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= @@ -89,6 +94,8 @@ github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdU github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk= +github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= @@ -139,24 +146,39 @@ github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavM github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e h1:+SOyEddqYF09QP7vr7CgJ1eti3pY9Fn3LHO1M1r/0sI= github.com/xrash/smetrics v0.0.0-20231213231151-1d8dd44e695e/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8= +github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/zalando/go-keyring v0.2.3 h1:v9CUu9phlABObO4LPWycf+zwMG7nlbb3t/B5wa97yms= github.com/zalando/go-keyring v0.2.3/go.mod h1:HL4k+OXQfJUWaMnqyuSOc0drfGPX2b51Du6K+MRgZMk= +go.elara.ws/pcre v0.0.0-20230805032557-4ce849193f64 h1:QixGnJE1jP08Hs1G3rS7tZGd8DeBRtz9RBpk08WlGh4= +go.elara.ws/pcre v0.0.0-20230805032557-4ce849193f64/go.mod h1:EF48C6VnP4wBayzFGk6lXqbiLucH7EfiaYOgiiCe5k4= go.etcd.io/bbolt v1.3.4/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= go.etcd.io/bbolt v1.3.8 h1:xs88BrvEv273UsB79e0hcVrlUWmS0a8upikMFhSyAtA= go.etcd.io/bbolt v1.3.8/go.mod h1:N9Mkw9X8x5fupy0IKsmuqVtoGDyxsaDlbk4Rd05IAQw= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611 h1:qCEDpW1G+vcj3Y7Fy52pEM1AWm3abj8WimGYejI3SC4= golang.org/x/exp v0.0.0-20231214170342-aacd6d4b4611/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= +golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191105084925-a882066a44e0/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= @@ -165,9 +187,16 @@ golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c= google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= @@ -184,3 +213,23 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +lukechampine.com/uint128 v1.1.1/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= +modernc.org/cc/v3 v3.36.0/go.mod h1:NFUHyPn4ekoC/JHeZFfZurN6ixxawE1BnVonP/oahEI= +modernc.org/ccgo/v3 v3.0.0-20220428102840-41399a37e894/go.mod h1:eI31LL8EwEBKPpNpA4bU1/i+sKOwOrQy8D87zWUcRZc= +modernc.org/ccgo/v3 v3.0.0-20220430103911-bc99d88307be/go.mod h1:bwdAnOoaIt8Ax9YdWGjxWsdkPcZyRPHqrOvJxaKAKGw= +modernc.org/ccgo/v3 v3.16.6/go.mod h1:tGtX0gE9Jn7hdZFeU88slbTh1UtCYKusWOoCJuvkWsQ= +modernc.org/ccorpus v1.11.6/go.mod h1:2gEUTrWqdpH2pXsmTM1ZkjeSrUWDpjMu2T6m29L/ErQ= +modernc.org/httpfs v1.0.6/go.mod h1:7dosgurJGp0sPaRanU53W4xZYKh14wfzX420oZADeHM= +modernc.org/libc v0.0.0-20220428101251-2d5f3daf273b/go.mod h1:p7Mg4+koNjc8jkqwcoFBJx7tXkpj00G77X7A72jXPXA= +modernc.org/libc v1.16.0/go.mod h1:N4LD6DBE9cf+Dzf9buBlzVJndKr/iJHG97vGLHYnb5A= +modernc.org/libc v1.16.1/go.mod h1:JjJE0eu4yeK7tab2n4S1w8tlWd9MxXLRzheaRnAKymU= +modernc.org/libc v1.16.8 h1:Ux98PaOMvolgoFX/YwusFOHBnanXdGRmWgI8ciI2z4o= +modernc.org/libc v1.16.8/go.mod h1:hYIV5VZczAmGZAnG15Vdngn5HSF5cSkbvfz2B7GRuVU= +modernc.org/mathutil v1.2.2/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= +modernc.org/mathutil v1.4.1 h1:ij3fYGe8zBF4Vu+g0oT7mB06r8sqGWKuJu1yXeR4by8= +modernc.org/mathutil v1.4.1/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= +modernc.org/memory v1.1.1 h1:bDOL0DIDLQv7bWhP3gMvIrnoFw+Eo6F7a2QK9HPDiFU= +modernc.org/memory v1.1.1/go.mod h1:/0wo5ibyrQiaoUoH7f9D8dnglAmILJ5/cxZlRECf+Nw= +modernc.org/opt v0.1.1/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= +modernc.org/strutil v1.1.1/go.mod h1:DE+MQQ/hjKBZS2zNInV5hhcipt5rLPWkmpbGeW5mmdw= +modernc.org/token v1.0.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/main.go b/main.go index 258ee46..d73a42c 100644 --- a/main.go +++ b/main.go @@ -1,13 +1,13 @@ package sncli import ( - "github.com/jonhadfield/gosn-v2/common" "os" "time" "github.com/briandowns/spinner" "github.com/gookit/color" "github.com/jonhadfield/gosn-v2/cache" + "github.com/jonhadfield/gosn-v2/common" "github.com/jonhadfield/gosn-v2/items" ) diff --git a/main_test.go b/main_test.go index 0ed109b..bdc2676 100644 --- a/main_test.go +++ b/main_test.go @@ -2,17 +2,18 @@ package sncli import ( "fmt" - "github.com/jonhadfield/gosn-v2/auth" - "github.com/jonhadfield/gosn-v2/cache" - "github.com/jonhadfield/gosn-v2/common" - "github.com/jonhadfield/gosn-v2/items" - "github.com/jonhadfield/gosn-v2/session" "log" "os" "strconv" "strings" "testing" "time" + + "github.com/jonhadfield/gosn-v2/auth" + "github.com/jonhadfield/gosn-v2/cache" + "github.com/jonhadfield/gosn-v2/common" + "github.com/jonhadfield/gosn-v2/items" + "github.com/jonhadfield/gosn-v2/session" ) var ( diff --git a/note.go b/note.go index f6f00b5..b6ca4fb 100644 --- a/note.go +++ b/note.go @@ -2,19 +2,19 @@ package sncli import ( "fmt" - "github.com/jonhadfield/gosn-v2/common" - "io/ioutil" + "io" "os" "path/filepath" "github.com/asdine/storm/v3/q" "github.com/jonhadfield/gosn-v2/cache" + "github.com/jonhadfield/gosn-v2/common" "github.com/jonhadfield/gosn-v2/items" ) -func (i *AddNoteInput) Run() (err error) { +func (i *AddNoteInput) Run() error { // get DB - var syncToken, newNoteUUID string + var syncToken string ani := addNoteInput{ noteTitle: i.Title, @@ -25,22 +25,24 @@ func (i *AddNoteInput) Run() (err error) { replace: i.Replace, } - newNoteUUID, err = addNote(ani) + newNoteUUID, err := addNote(ani) if err != nil { - return + return err } if len(ani.tagTitles) > 0 { - err = tagNotes(tagNotesInput{ + if err = tagNotes(tagNotesInput{ matchNoteUUIDs: []string{newNoteUUID}, syncToken: syncToken, session: i.Session, newTags: i.Tags, replace: i.Replace, - }) + }); err != nil { + return err + } } - return + return nil } type addNoteInput struct { @@ -52,16 +54,15 @@ type addNoteInput struct { replace bool } -func loadNoteContentFromFile(filePath string) (content string, err error) { +func loadNoteContentFromFile(filePath string) (string, error) { file, err := os.Open(filePath) if err != nil { - err = fmt.Errorf("%w failed to open: %s", err, filePath) - return + return "", fmt.Errorf("%w failed to open: %s", err, filePath) } - b, err := ioutil.ReadAll(file) + b, err := io.ReadAll(file) if err != nil { - return + return "", fmt.Errorf("%w failed to read: %s", err, filePath) } return string(b), nil @@ -90,7 +91,6 @@ func addNote(i addNoteInput) (noteUUID string, err error) { so, err = Sync(si, true) if err != nil { - return } @@ -111,12 +111,14 @@ func addNote(i addNoteInput) (noteUUID string, err error) { }, }, } + var gi items.Items + gi, err = gnc.Run() if err != nil { - return } + switch len(gi) { case 0: err = fmt.Errorf("failed to find existing note to replace") @@ -166,6 +168,7 @@ func addNote(i addNoteInput) (noteUUID string, err error) { pii := cache.SyncInput{ Session: i.session, + Close: false, } so, err = Sync(pii, true) @@ -200,10 +203,8 @@ func (i *DeleteItemConfig) Run() (noDeleted int, err error) { return noDeleted, err } -func (i *DeleteNoteConfig) Run() (noDeleted int, err error) { - noDeleted, err = deleteNotes(i.Session, i.NoteTitles, i.NoteText, i.NoteUUIDs, i.Regex) - - return noDeleted, err +func (i *DeleteNoteConfig) Run() (int, error) { + return deleteNotes(i.Session, i.NoteTitles, i.NoteText, i.NoteUUIDs, i.Regex) } func (i *GetNoteConfig) Run() (items items.Items, err error) { @@ -221,6 +222,7 @@ func (i *GetNoteConfig) Run() (items items.Items, err error) { err = so.DB.All(&allPersistedItems) if err != nil { err = fmt.Errorf("getting items from db: %w", err) + return } diff --git a/note_test.go b/note_test.go index f8205fe..a42f021 100644 --- a/note_test.go +++ b/note_test.go @@ -2,11 +2,12 @@ package sncli import ( "fmt" + "testing" + "github.com/jonhadfield/gosn-v2/cache" "github.com/jonhadfield/gosn-v2/common" "github.com/jonhadfield/gosn-v2/items" "github.com/stretchr/testify/require" - "testing" ) func TestAddDeleteNoteByUUID(t *testing.T) { @@ -215,9 +216,11 @@ func TestWipeWith50(t *testing.T) { } var deleted int + deleted, err = wipeConfig.Run() require.NoError(t, err) - require.True(t, deleted >= numNotes, fmt.Sprintf("notes created: %d items deleted: %d", numNotes, deleted)) + + require.GreaterOrEqual(t, deleted, numNotes, fmt.Sprintf("notes created: %d items deleted: %d", numNotes, deleted)) } func TestAddDeleteNoteByTitle(t *testing.T) { diff --git a/stats.go b/stats.go index 840b6bc..a0aa720 100644 --- a/stats.go +++ b/stats.go @@ -2,15 +2,16 @@ package sncli import ( "fmt" - "github.com/jonhadfield/gosn-v2/common" "sort" "time" "github.com/alexeyco/simpletable" "github.com/dustin/go-humanize" + // "github.com/fatih/color". "github.com/gookit/color" "github.com/jonhadfield/gosn-v2/cache" + "github.com/jonhadfield/gosn-v2/common" "github.com/jonhadfield/gosn-v2/items" "github.com/ryanuber/columnize" ) @@ -202,11 +203,11 @@ func showNoteHistory(data StatsData) { {Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("Time")}, }, } - table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{ - {Text: "Oldest"}, - {Text: fmt.Sprintf("%s", data.OldestNote.Content.Title)}, - {Text: fmt.Sprintf("%s", humanize.Time(time.UnixMicro(data.OldestNote.CreatedAtTimestamp)))}, - }) + + if data.OldestNote != nil { + data.OldestNote.Content = items.NoteContent{} + } + table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{ {Text: "Newest"}, {Text: fmt.Sprintf("%s", data.NewestNote.Content.Title)}, @@ -231,6 +232,7 @@ func showItemCounts(data StatsData) { {Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("Count")}, }, } + table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{ {Text: "Notes"}, {Text: fmt.Sprintf("%s", humanize.Comma(data.CoreTypeCounter.counts[common.SNItemTypeNote]))}, diff --git a/tag.go b/tag.go index 518be91..81e2218 100644 --- a/tag.go +++ b/tag.go @@ -30,10 +30,8 @@ func tagNotes(i tagNotesInput) (err error) { replace: i.replace, } - _, err = addTags(ati) - - if err != nil { - return + if _, err = addTags(ati); err != nil { + return err } // get notes and tags @@ -58,7 +56,7 @@ func tagNotes(i tagNotesInput) (err error) { so, err = Sync(syncInput, true) if err != nil { - return + return err } var allPersistedItems cache.Items @@ -129,7 +127,7 @@ func tagNotes(i tagNotesInput) (err error) { if len(tagsToPush) > 0 { if err = cache.SaveTags(so.DB, i.session, tagsToPush, true); err != nil { - return + return err } pii := cache.SyncInput{ @@ -138,29 +136,27 @@ func tagNotes(i tagNotesInput) (err error) { so, err = Sync(pii, true) if err != nil { - return + return err } if err = so.DB.Close(); err != nil { - return + return err } - return err + return nil } return nil } func (i *TagItemsConfig) Run() error { - tni := tagNotesInput{ + return tagNotes(tagNotesInput{ matchTitle: i.FindTitle, matchText: i.FindText, matchTags: []string{i.FindTag}, newTags: i.NewTags, session: i.Session, - } - - return tagNotes(tni) + }) } func (i *AddTagsInput) Run() (output AddTagsOutput, err error) { @@ -255,6 +251,7 @@ func (i *GetTagConfig) Run() (items items.Items, err error) { func (i *DeleteTagConfig) Run() (noDeleted int, err error) { noDeleted, err = deleteTags(i.Session, i.TagTitles, i.TagUUIDs) + return noDeleted, err } @@ -303,6 +300,7 @@ func deleteTags(session *cache.Session, tagTitles []string, tagUUIDs []string) ( tags = gItems tags.Filter(deleteFilter) + if len(tags) == 0 { return 0, nil } @@ -389,6 +387,7 @@ func addTags(ati addTagsInput) (ato addTagsOutput, err error) { } var so cache.SyncOutput + so, err = Sync(putItemsInput, true) if err != nil { return @@ -402,6 +401,7 @@ func addTags(ati addTagsInput) (ato addTagsOutput, err error) { } var gItems items.Items + gItems, err = allPersistedItems.ToItems(ati.session) if err != nil { return @@ -424,6 +424,7 @@ func addTags(ati addTagsInput) (ato addTagsOutput, err error) { if item.GetContentType() == common.SNItemTypeTag { tag := item.(*items.Tag) allTags = append(allTags, *tag) + if tag.Content.GetTitle() == ati.parent || tag.GetUUID() == ati.parentUUID { if parentRef != nil { return ato, errors.New("multiple parent tags found, specify by UUID") diff --git a/tag_test.go b/tag_test.go index d2921d2..4dbb156 100644 --- a/tag_test.go +++ b/tag_test.go @@ -1,10 +1,10 @@ package sncli import ( - "github.com/jonhadfield/gosn-v2/common" "testing" "github.com/jonhadfield/gosn-v2/cache" + "github.com/jonhadfield/gosn-v2/common" "github.com/jonhadfield/gosn-v2/items" "github.com/stretchr/testify/require" )