Skip to content

Commit

Permalink
Merge pull request #12 from dbalan/delete_command
Browse files Browse the repository at this point in the history
Delete command
  • Loading branch information
dbalan committed Mar 15, 2018
2 parents 1d8417b + 5f6f040 commit a240a23
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 29 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ editor_binary: "absolute path to editor you want to use" # default is $EDITOR en
- pipet search : search through current snippets (only titles and tags for now)
- pipet edit id : edit a snippet by id
- pipet show id : show a snippet
- pipet echo id : like show, but only prints the snippet data.
- pipet list : list all snippets
- pipet configure: TBD
- pipet init: inital config.
- pipet delete: delete snippets.
## TODO
- [x] finish configure command
Expand All @@ -53,6 +53,7 @@ editor_binary: "absolute path to editor you want to use" # default is $EDITOR en
- [ ] Search full text, with a flag to search command
- [ ] Try to abstract snippet id from operations.
- [ ] Tests, would like more tests.
- [ ] Add an archive flag for search, the data is not deleted, but is not exposed unless user turns on another flag.
## Hacking
0. Uses [dep](https://golang.github.io/dep/) for dependency management.
Expand Down
62 changes: 62 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright © 2018 Dhananjay Balan <mail@dbalan.in>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete uid",
Short: "Remove snippet from storage (this is irreversible!)",
Args: cobra.ExactArgs(1),
PreRunE: ensureConfig,
Run: func(cmd *cobra.Command, args []string) {
dataStore := getDataStore()
snip, err := dataStore.Read(args[0])
errorGuard(err, "querying snippet failed")

fmt.Printf("Are your you want to %s '%s' [y/n]: ", Red("DELETE"), Green(snip.Meta.Title))
confirm := readLine()
if confirm == "y" || confirm == "yes" {
errorGuard(dataStore.Delete(args[0]), "program failed to delete")
fmt.Println("deleted!")
}
},
}

func init() {
rootCmd.AddCommand(deleteCmd)

// FIXME: add an archive
}
11 changes: 4 additions & 7 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ package cmd

import (
"fmt"
"io/ioutil"
"os"

"github.com/fatih/color"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
)

// initCmd represents the init command
Expand All @@ -45,8 +44,6 @@ var initCmd = &cobra.Command{
Short: "Configure pipet",
Long: `Creates the config files for pipet if not present, usually you only need at the first use`,
Run: func(cmd *cobra.Command, args []string) {
green := color.New(color.FgGreen).SprintFunc()

configFile := expandHome("~/.pipet.yaml")
_, err := os.Stat(configFile)
if err == nil {
Expand Down Expand Up @@ -89,7 +86,7 @@ var initCmd = &cobra.Command{

// expanded path
if path != eBin {
fmt.Printf("Using %s as the absoulte path to editor\n", green(path))
fmt.Printf("Using %s as the absoulte path to editor\n", Green(path))
}
config.EBin = path
} else {
Expand All @@ -111,7 +108,7 @@ var initCmd = &cobra.Command{
fmt.Printf(`pipet is now ready to use
snippets are stored in: %s
config is stored in %s
`, green(snipDir), green(configFile))
`, Green(snipDir), Green(configFile))
},
}

Expand Down
10 changes: 3 additions & 7 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ package cmd

import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
"strings"
)

var (
Expand All @@ -55,14 +54,11 @@ var listCmd = &cobra.Command{
sns, err := dataStore.List()
errorGuard(err, "listing store failed")

blue := color.New(color.FgBlue).SprintFunc()
green := color.New(color.FgGreen).SprintFunc()

output := []string{"UID | Title | Tags"}
for _, snip := range sns {
tags := strings.Join(snip.Meta.Tags, " ")
out := fmt.Sprintf("%s | %s | %s", green(snip.Meta.UID),
snip.Meta.Title, blue(tags))
out := fmt.Sprintf("%s | %s | %s", Green(snip.Meta.UID),
snip.Meta.Title, Blue(tags))
output = append(output, out)
}

Expand Down
13 changes: 5 additions & 8 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ package cmd
import (
"fmt"

"github.com/dbalan/pipet/pipetdata"

"github.com/fatih/color"
"github.com/spf13/cobra"

"github.com/dbalan/pipet/pipetdata"
)

var body bool
Expand Down Expand Up @@ -64,13 +63,11 @@ func init() {
}

func fancySnippet(s *pipetdata.Snippet) string {
green := color.New(color.FgGreen).SprintFunc()
blue := color.New(color.FgBlue).SprintFunc()
sep := green("---\n")
sep := Green("---\n")

text := sep + green("Title: ") + fmt.Sprint(s.Meta.Title) + green("\nTags:\n")
text := sep + Green("Title: ") + fmt.Sprint(s.Meta.Title) + Green("\nTags:\n")
for _, t := range s.Meta.Tags {
text += green("- ") + blue(t) + "\n"
text += Green("- ") + Blue(t) + "\n"
}
text += sep
text += s.Data
Expand Down
14 changes: 9 additions & 5 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,26 @@
package cmd

import (
"bufio"
"bytes"
"fmt"
"github.com/spf13/cobra"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/fatih/color"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"bufio"
"github.com/dbalan/pipet/pipetdata"
"github.com/fatih/color"
)

func errorGuard(err error, msg string) {
red := color.New(color.FgRed).SprintFunc()
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", red(msg), err)
fmt.Fprintf(os.Stderr, "%s: %v\n", Red(msg), err)
os.Exit(-1)
}
}
Expand Down Expand Up @@ -184,3 +183,8 @@ func readLine() string {

return strings.TrimSuffix(text, "\n")
}

// console colors
var Red = color.New(color.FgRed).SprintFunc()
var Green = color.New(color.FgGreen).SprintFunc()
var Blue = color.New(color.FgBlue).SprintFunc()
15 changes: 15 additions & 0 deletions pipetdata/pipetdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,18 @@ func (d *DataStore) List() (sns []*Snippet, err error) {
}
return
}

func (d *DataStore) Delete(id string) error {
if !d.Exist(id) {
return errors.New("no such document")
}

filename := d.Fullpath(id)

err := os.Remove(filename)
if err != nil {
return errors.Wrap(err, "delete failed")
}

return nil
}
12 changes: 12 additions & 0 deletions pipetdata/pipetdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ func TestDataStore(t *testing.T) {
}

assert.Equal(t, expected.Meta, sn.Meta, "snippet metadata should match")

// test delete
err = ds.Delete(uid)
assert.Nil(t, err, "should have deleted properly")

// try random file
err = ds.Delete("probably.txt")
assert.NotNil(t, err, "should return no file")

_, err = ds.Read(uid)
assert.NotNil(t, err, "no such snippet should exist.")

}

func TestDataStoreList(t *testing.T) {
Expand Down

0 comments on commit a240a23

Please sign in to comment.