~gjabell/bmk

701a631803003e1d4422300f46af1bb8364f1c2d — Galen Abell 1 year, 4 months ago e5f19e5
Refactor remove to parse indices in args

* Extract common code between remove and open
4 files changed, 70 insertions(+), 39 deletions(-)

M cmd/open.go
M cmd/remove.go
M cmd/root.go
A cmd/util.go
M cmd/open.go => cmd/open.go +3 -14
@@ 19,14 19,11 @@ import (
	"errors"
	"fmt"
	"os/exec"
	"strconv"

	"git.sr.ht/~gjabell/bmk/bookmark"
	"github.com/spf13/cobra"
)

var indices []int

var openCmd = &cobra.Command{
	Use:     "open",
	Aliases: []string{"o"},


@@ 44,18 41,10 @@ file.`,
		}

		// parse indices and ensure they are valid
		if len(args) != 0 {
			for _, i := range args {
				index, err := strconv.Atoi(i)
				if err != nil || index < 0 {
					return fmt.Errorf("invalid index: %s", i)
				}
		var err error
		indices, err = argsToIndices(args)

				indices = append(indices, index)
			}
		}

		return nil
		return err
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		filter := bookmark.Filter{Tags: tags, Indices: indices, Desc: ""}

M cmd/remove.go => cmd/remove.go +28 -25
@@ 17,9 17,9 @@ package cmd

import (
	"bufio"
	"errors"
	"fmt"
	"os"
	"strconv"
	"strings"

	"git.sr.ht/~gjabell/bmk/bookmark"


@@ 34,37 34,40 @@ var removeCmd = &cobra.Command{
	Short:   "Remove a bookmark",
	Long: `Remove a bookmark from the manager. Takes a list of numbers referencing the
index values from the list command.`,
	Args: cobra.MinimumNArgs(1),
	RunE: func(cmd *cobra.Command, args []string) error {
		var indices []int
		reader := bufio.NewReader(os.Stdin)
		for _, i := range args {
			index, err := strconv.Atoi(i)
			if err != nil {
				fmt.Printf("Skipping invalid index %s\n", i)
				continue
			}
	Args: func(cmd *cobra.Command, args []string) error {
		if len(args) == 0 {
			return errors.New("you must specify at least one index")
		}

			if noConfirm {
				indices = append(indices, index)
				continue
			}
		// parse indices and ensure they are valid
		var err error
		indices, err = argsToIndices(args)

			fmt.Printf("Delete %d? (y/n) ", index)
		return err
	},
	RunE: func(cmd *cobra.Command, args []string) error {
		var toDelete []int
		if noConfirm {
			toDelete = indices
		} else {
			reader := bufio.NewReader(os.Stdin)
			for _, i := range indices {
				fmt.Printf("Delete %d? (y/n) ", i)

			confirm, err := reader.ReadString('\n')
			if err != nil {
				return err
			}
				confirm, err := reader.ReadString('\n')
				if err != nil {
					return err
				}

			if strings.ToLower(string(confirm[0])) == "y" {
				indices = append(indices, index)
			} else {
				fmt.Printf("Skipping %d\n", index)
				if strings.ToLower(string(confirm[0])) == "y" {
					toDelete = append(toDelete, i)
				} else {
					fmt.Printf("Skipping %d\n", i)
				}
			}
		}

		removed, err := bookmark.Remove(config.BookmarkPath, indices)
		removed, err := bookmark.Remove(config.BookmarkPath, toDelete)
		if err != nil {
			return err
		}

M cmd/root.go => cmd/root.go +1 -0
@@ 29,6 29,7 @@ import (
var config *cfg.Config
var cfgFile string
var tags []string
var indices []int

var rootCmd = &cobra.Command{
	Use:   "bmk",

A cmd/util.go => cmd/util.go +38 -0
@@ 0,0 1,38 @@
// Copyright © 2019 Galen Abell <galen@galenabell.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package cmd

import (
	"fmt"
	"strconv"
)

func argsToIndices(args []string) ([]int, error) {
	var indices []int

	if len(args) != 0 {
		for _, i := range args {
			index, err := strconv.Atoi(i)
			if err != nil || index < 0 {
				return nil, fmt.Errorf("invalid index: %s", i)
			}

			indices = append(indices, index)
		}
	}

	return indices, nil
}