Skip to content

Commit

Permalink
Merge pull request #19 from DrakeW/develop
Browse files Browse the repository at this point in the history
v0.2.2 release - develop to master
  • Loading branch information
junyu-w committed Jun 9, 2018
2 parents 7dfdc63 + 84b3865 commit 7a1a11e
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 30 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

Corgi is a command-line tool that helps with your repetitive command usages by organizing them into reusable snippet. See usage by simply running `corgi` or `corgi --help`

Current version: **v0.2.1**
Current version: **v0.2.2**
## Examples

Create a new snippet to automate the commands you run repetitively
Expand Down Expand Up @@ -145,9 +145,9 @@ corgi remove [<title of the snippet>]
```

### Configure `corgi`
Currently the only editable option is your text editor choice (default is `vim`), to configure the corgi CLI, run
You can configure your text editor choice (default is `vim`) for snippet editing, and filter tool for interactive snippet selection. To configure the corgi CLI, run
```
corgi config --editor <editor of your choice>
corgi config [--editor <editor of your choice>] [--filter-cmd <fzf, peco or something else>]
```

## Roadmap
Expand Down
12 changes: 11 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import "github.com/spf13/cobra"
import (
"github.com/spf13/cobra"
)

var configCmd = &cobra.Command{
Use: "config",
Expand All @@ -9,6 +11,7 @@ var configCmd = &cobra.Command{
}

var editor string
var filterCmd string

func configure(cmd *cobra.Command, args []string) error {
conf, _, err := loadConfigAndSnippetsMeta()
Expand All @@ -21,10 +24,17 @@ func configure(cmd *cobra.Command, args []string) error {
return err
}
}
if filterCmd != "" {
conf.FilterCmd = filterCmd
if err := conf.Save(); err != nil {
return err
}
}
return nil
}

func init() {
configCmd.Flags().StringVar(&filterCmd, "filter-cmd", "", "Select the text editor you would like to use to edit snippet")
configCmd.Flags().StringVar(&editor, "editor", "", "Select the text editor you would like to use to edit snippet")
rootCmd.AddCommand(configCmd)
}
2 changes: 1 addition & 1 deletion cmd/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import "github.com/spf13/cobra"

var describeCmd = &cobra.Command{
Use: "describe",
Use: "describe [title]",
Short: "Describe a snippet",
Args: cobra.MaximumNArgs(1),
RunE: describe,
Expand Down
13 changes: 9 additions & 4 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,42 @@ import (
)

var editCmd = &cobra.Command{
Use: "edit",
Use: "edit [title]",
Short: "Edit a snippet",
Args: cobra.MaximumNArgs(1),
RunE: edit,
}

func edit(cmd *cobra.Command, args []string) error {
// load config & snippets
conf, snippets, err := loadConfigAndSnippetsMeta()
conf, snippetsMeta, err := loadConfigAndSnippetsMeta()
if err != nil {
return err
}
// find snippet title
var title string
if len(args) == 0 {
title, err = filterSnippetTitle(conf.FilterCmd, snippets.GetSnippetTitles())
title, err = filterSnippetTitle(conf.FilterCmd, snippetsMeta.GetSnippetTitles())
if err != nil {
return err
}
} else {
title = args[0]
}
// find snippet
s, err := snippets.FindSnippet(title)
s, err := snippetsMeta.FindSnippet(title)
if err != nil {
return err
}
command := fmt.Sprintf("%s %s", conf.Editor, s.GetFilePath())
if err := util.Execute(command, os.Stdin, os.Stdout); err != nil {
return err
}
// mark snippetsMeta dirty
snippetsMeta.IsMetaDirty = true
if err = snippetsMeta.Save(); err != nil {
return err
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

var execCmd = &cobra.Command{
Use: "exec",
Use: "exec [title]",
Short: "Execute a snippet",
Args: cobra.MaximumNArgs(1),
RunE: execute,
Expand Down
2 changes: 1 addition & 1 deletion cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var exportCmd = &cobra.Command{
Use: "export",
Use: "export [title]",
Short: "Export a snippet to json file",
Args: cobra.MaximumNArgs(1),
RunE: export,
Expand Down
4 changes: 2 additions & 2 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

var importCmd = &cobra.Command{
Use: "import",
Short: "Import a snippet from json file",
Use: "import [file1] [file2...]",
Short: "Import a snippet from one or multiple json files",
Args: cobra.MinimumNArgs(1),
RunE: importSnippet,
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ var listCmd = &cobra.Command{

func list(cmd *cobra.Command, args []string) error {
// load config & snippets
_, snippets, err := loadConfigAndSnippetsMeta()
_, snippetsMeta, err := loadConfigAndSnippetsMeta()
if err != nil {
return err
}
// display
fmt.Println("Here is the list of corgi snippets saved on your system:")
for _, s := range snippets.Snippets {
for _, s := range snippetsMeta.Snippets {
color.Yellow("- %s", s.Title)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func create(cmd *cobra.Command, args []string) error {
}
defer snippet.RemoveHistFile()
// load config and snippets
conf, snippets, err := loadConfigAndSnippetsMeta()
conf, snippetsMeta, err := loadConfigAndSnippetsMeta()
if err != nil {
return err
}
Expand All @@ -40,7 +40,7 @@ func create(cmd *cobra.Command, args []string) error {
return err
}
// add new sninppet to snippets meta and save
if err = snippets.SaveNewSnippet(newSnippet, conf.SnippetsDir); err != nil {
if err = snippetsMeta.SaveNewSnippet(newSnippet, conf.SnippetsDir); err != nil {
return err
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import "github.com/spf13/cobra"

var removeCmd = &cobra.Command{
Use: "remove",
Use: "remove [title]",
Short: "Remove a snippet",
Args: cobra.MaximumNArgs(1),
RunE: remove,
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"os"
)

var appVersion = "v0.2.1"
var appVersion = "v0.2.2"

var rootCmd = &cobra.Command{
Use: "corgi",
Short: "Corgi is a smart dog that helps you organize your command flow for future usage",
Short: "Corgi is a smart dog that helps you manage your CLI workflow",
Version: appVersion,
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
Expand Down
8 changes: 6 additions & 2 deletions snippet/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,13 @@ func (snippet *Snippet) AskQuestion(options ...interface{}) error {
return nil
}

func getSnippetFileName(title string) string {
return fmt.Sprintf("%s.json", strings.Replace(title, " ", "_", -1))
}

func (snippet *Snippet) Save(snippetsDir string) error {
fmt.Printf("Saving snippet %s... ", snippet.Title)
filePath := fmt.Sprintf("%s/%s.json", snippetsDir, strings.Replace(snippet.Title, " ", "_", -1))
fmt.Printf("Saving snippet \"%s\"... ", snippet.Title)
filePath := fmt.Sprintf("%s/%s", snippetsDir, getSnippetFileName(snippet.Title))
snippet.fileLoc = filePath
if err := snippet.writeToFile(filePath); err != nil {
color.Red("Failure")
Expand Down
64 changes: 57 additions & 7 deletions snippet/snippets.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,62 @@ import (
"github.com/fatih/color"
"io/ioutil"
"os"
"path"
"strconv"
"time"
)

type SnippetsMeta struct {
Snippets []*jsonSnippet `json:"snippets"`
fileLoc string
Snippets []*jsonSnippet `json:"snippets"`
IsMetaDirty bool `json:"is_meta_dirty"`
fileLoc string
}

type jsonSnippet struct {
FileLoc string `json:"file_loc"`
Title string `json:"title"`
}

func (sm *SnippetsMeta) syncWithSnippets() error {
for _, s := range sm.Snippets {
snippet, err := sm.FindSnippet(s.Title)
if err != nil {
return err
}
if s.Title != snippet.Title {
s.Title = snippet.Title
newFileName := getSnippetFileName(s.Title)
newFilePath := fmt.Sprintf("%s/%s", path.Dir(s.FileLoc), newFileName)
if err = os.Rename(s.FileLoc, newFilePath); err != nil {
return err
}
s.FileLoc = newFilePath
}
}
sm.IsMetaDirty = false
if err := sm.Save(); err != nil {
return err
}
return nil
}

func LoadSnippetsMeta(filePath string) (*SnippetsMeta, error) {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil, err
}
snippets := &SnippetsMeta{}
if err := util.LoadJsonDataFromFile(filePath, snippets); err != nil {
snippetsMeta := &SnippetsMeta{}
if err := util.LoadJsonDataFromFile(filePath, snippetsMeta); err != nil {
return nil, err
}
if snippets.fileLoc == "" {
snippets.fileLoc = filePath
if snippetsMeta.fileLoc == "" {
snippetsMeta.fileLoc = filePath
}
if snippetsMeta.IsMetaDirty {
if err := snippetsMeta.syncWithSnippets(); err != nil {
return nil, err
}
}
return snippets, nil
return snippetsMeta, nil
}

func (sm *SnippetsMeta) Save() error {
Expand All @@ -49,9 +81,18 @@ func (sm *SnippetsMeta) Save() error {

// Save new snippet into snippetsDir and update snippets meta file
func (sm *SnippetsMeta) SaveNewSnippet(snippet *Snippet, snippetsDir string) error {
// check for duplicate
if sm.isDuplicate(snippet.Title) {
t := strconv.FormatInt(time.Now().Unix(), 10)
newTitle := fmt.Sprintf("%s-%s", snippet.Title, t)
color.Red("Snippet with title \"%s\" already existed - saving as \"%s\"", snippet.Title, newTitle)
snippet.Title = newTitle
}
// save snippet file
if err := snippet.Save(snippetsDir); err != nil {
return err
}
// save to snippets meta file
jsonSnippet := &jsonSnippet{
Title: snippet.Title,
FileLoc: snippet.fileLoc,
Expand All @@ -63,6 +104,15 @@ func (sm *SnippetsMeta) SaveNewSnippet(snippet *Snippet, snippetsDir string) err
return nil
}

func (sm *SnippetsMeta) isDuplicate(title string) bool {
for _, s := range sm.Snippets {
if s.Title == title {
return true
}
}
return false
}

func (sm *SnippetsMeta) DeleteSnippet(title string) error {
idx, err := sm.findJsonSnippetIndex(title)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion snippet/step.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
type StepInfo struct {
Command string `json:"command"`
Description string `json:"description,omitempty"`
ExecuteConcurrent bool `json:"execute_concurrent"`
}

var TemplateParamsRegex = `<([^(<>|\s)]+)>`
Expand Down

0 comments on commit 7a1a11e

Please sign in to comment.