Skip to content

Commit

Permalink
Merge pull request #14 from dbalan/abstracting_ids
Browse files Browse the repository at this point in the history
Abstracting ids
  • Loading branch information
dbalan committed Mar 21, 2018
2 parents 67aa556 + 5009d39 commit 38fe29c
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 113 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ Usage:
pipet [command]

Available Commands:
delete Remove snippet from storage (this is irreversible!)
edit edit snippet data
help Help about any command
init Configure pipet
list list all snippets
new Creates a new snippet and opens editor to edit content
search Search through snippets
show display the snippet
show Show snippet
delete Remove snippet from storage (this is irreversible!)
edit Edit snippet data
help Help about any command
list List all snippets

Flags:
--config string config file (default is $HOME/.pipet.yaml)
Expand All @@ -65,7 +64,7 @@ Flags:
- [x] binary downloads
- [x] make public
- [ ] Ability to search full text, with a flag to search command
- [ ] Try to abstract snippet id from operations, one way to do this is to move id's optional for commands and jump to a search interface in case IDs are not specified.
- [x] Try to abstract snippet id from operations, one way to do this is to move id's optional for commands and jump to a search interface in case IDs are not specified.
- [ ] Tests, would like more tests.
- [ ] Add an archive flag for delete, the data is not deleted, but is not exposed unless user turns on another flag.

Expand Down
17 changes: 13 additions & 4 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,28 @@ import (

// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete uid",
Use: "delete [uid]",
Short: "Remove snippet from storage (this is irreversible!)",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
PreRunE: ensureConfig,
Run: func(cmd *cobra.Command, args []string) {
sid := ""
if len(args) == 0 {
s, err := searchFullSnippet()
errorGuard(err, "")
sid = s
} else {
sid = args[0]
}

dataStore := getDataStore()
snip, err := dataStore.Read(args[0])
snip, err := dataStore.Read(sid)
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")
errorGuard(dataStore.Delete(sid), "program failed to delete")
fmt.Println("deleted!")
}
},
Expand Down
15 changes: 12 additions & 3 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ import (

// editCmd represents the edit command
var editCmd = &cobra.Command{
Use: "edit",
Use: "edit [uid]",
Short: "edit snippet data",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
sid := ""
if len(args) == 0 {
s, err := searchFullSnippet()
errorGuard(err, "")
sid = s
} else {
sid = args[0]
}

dataStore := getDataStore()
fn := dataStore.Fullpath(args[0])
fn := dataStore.Fullpath(sid)
editSnippet(fn)
},
}
Expand Down
15 changes: 1 addition & 14 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ package cmd

import (
"fmt"
"strings"

"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
)

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

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))
output = append(output, out)
}

rendered := columnize.SimpleFormat(output)
rendered := renderSnippetList(sns, true)
fmt.Println(rendered)
},
}

func init() {
rootCmd.AddCommand(listCmd)

// listCmd.Flags().BoolVarP(&tags, "tags", "t", false, "Enable list tags associated with the snippet.")
// listCmd.Flags().BoolVarP(&full, "full", "f", false, "Print the full snippet")
}
80 changes: 0 additions & 80 deletions cmd/search.go

This file was deleted.

15 changes: 12 additions & 3 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,22 @@ var body bool

// showCmd represents the show command
var showCmd = &cobra.Command{
Use: "show uid",
Use: "show [uid]",
Short: "display the snippet",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
PreRunE: ensureConfig,
Run: func(cmd *cobra.Command, args []string) {
sid := ""
if len(args) == 0 {
s, err := searchFullSnippet()
errorGuard(err, "")
sid = s
} else {
sid = args[0]
}

dataStore := getDataStore()
snip, err := dataStore.Read(args[0])
snip, err := dataStore.Read(sid)
errorGuard(err, "reading snippet failed")
if body {
fmt.Printf(snip.Data)
Expand Down
51 changes: 49 additions & 2 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/fatih/color"
homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/ryanuber/columnize"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -121,10 +122,18 @@ func editSnippet(fn string) error {
func parseOutput(out string) (string, error) {
out = strings.TrimSuffix(out, "\n")
oli := strings.Split(out, " ")
if len(oli) < 2 {

coli := []string{}
// compact the output
for _, v := range oli {
if v != "" {
coli = append(coli, v)
}
}
if len(coli) < 2 {
return "", errors.New("bad data")
}
return oli[0], nil
return coli[len(coli)-1], nil
}

func which(c string) (string, error) {
Expand Down Expand Up @@ -184,6 +193,44 @@ func readLine() string {
return strings.TrimSuffix(text, "\n")
}

func renderSnippetList(sns []*pipetdata.Snippet, header bool) string {
output := []string{}
if header {
output = append(output, "Title | Tags | UID")
}

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

return columnize.SimpleFormat(output)
}

func searchFullSnippet() (sid string, e error) {
dataStore := getDataStore()

sns, err := dataStore.List()
if err != nil {
e = errors.Wrap(err, "listing dataStore failed")
return
}

rendered := renderSnippetList(sns, false)
sid, err = fuzzyWrapper(rendered)
if err != nil {
e = errors.Wrap(err, "searching failed")
return
}
return sid, err
}

// console colors
var Red = color.New(color.FgRed).SprintFunc()
var Green = color.New(color.FgGreen).SprintFunc()
Expand Down

0 comments on commit 38fe29c

Please sign in to comment.