-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
768 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,5 @@ _testmain.go | |
*.exe | ||
*.test | ||
*.prof | ||
out | ||
pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
# pet | ||
pet | ||
# pet : CLI Snippet Manager | ||
|
||
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](https://github.com/knqyf263/pet/blob/master/LICENSE) | ||
|
||
|
||
Simple command-line snippet manager, written in Go | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/knqyf263/pet/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// configureCmd represents the configure command | ||
var configureCmd = &cobra.Command{ | ||
Use: "configure", | ||
Short: "Edit config file", | ||
Long: `Edit config file (default: opened by vim)`, | ||
RunE: configure, | ||
} | ||
|
||
func configure(cmd *cobra.Command, args []string) (err error) { | ||
editor := config.Conf.General.Editor | ||
return editFile(editor, configFile) | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(configureCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/knqyf263/pet/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// editCmd represents the edit command | ||
var editCmd = &cobra.Command{ | ||
Use: "edit", | ||
Short: "Edit snippet file", | ||
Long: `Edit snippet file (default: opened by vim)`, | ||
RunE: edit, | ||
} | ||
|
||
func edit(cmd *cobra.Command, args []string) (err error) { | ||
editor := config.Conf.General.Editor | ||
snippetFile := config.Conf.General.SnippetFile | ||
|
||
return editFile(editor, snippetFile) | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(editCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/knqyf263/pet/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// execCmd represents the exec command | ||
var execCmd = &cobra.Command{ | ||
Use: "exec", | ||
Short: "Run the selected commands", | ||
Long: `Run the selected commands directly`, | ||
RunE: execute, | ||
} | ||
|
||
func execute(cmd *cobra.Command, args []string) (err error) { | ||
var options []string | ||
commands, err := filter(options) | ||
if err != nil { | ||
return err | ||
} | ||
command := strings.Join(commands, "; ") | ||
if config.Flag.Debug { | ||
fmt.Printf("Command: %s\n", command) | ||
} | ||
return run(command, os.Stdin, os.Stdout) | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(execCmd) | ||
execCmd.Flags().StringVarP(&config.Flag.Query, "query", "q", "", | ||
`Initial value for query`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/fatih/color" | ||
"github.com/knqyf263/pet/config" | ||
"github.com/knqyf263/pet/snippet" | ||
runewidth "github.com/mattn/go-runewidth" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
column = 40 | ||
) | ||
|
||
// listCmd represents the list command | ||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "Show all snippets", | ||
Long: `Show all snippets`, | ||
RunE: list, | ||
} | ||
|
||
func list(cmd *cobra.Command, args []string) error { | ||
var snippets snippet.Snippets | ||
if err := snippets.Load(); err != nil { | ||
return err | ||
} | ||
|
||
col := config.Conf.General.Column | ||
if col == 0 { | ||
col = column | ||
} | ||
|
||
for _, snippet := range snippets.Snippets { | ||
description := runewidth.FillRight(runewidth.Truncate(snippet.Description, col, "..."), col) | ||
command := runewidth.Truncate(snippet.Command, 100-4-col, "...") | ||
|
||
fmt.Fprintf(color.Output, "%s : %s\n", | ||
color.GreenString(description), color.YellowString(command)) | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(listCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/knqyf263/pet/snippet" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// newCmd represents the new command | ||
var newCmd = &cobra.Command{ | ||
Use: "new COMMAND", | ||
Short: "Create a new snippet", | ||
Long: `Create a new snippet (default: $HOME/.config/pet/snippet.toml)`, | ||
RunE: new, | ||
} | ||
|
||
func scan(message string) (string, error) { | ||
fmt.Print(message) | ||
scanner := bufio.NewScanner(os.Stdin) | ||
if !scanner.Scan() { | ||
return "", errors.New("canceled") | ||
} | ||
if scanner.Err() != nil { | ||
return "", scanner.Err() | ||
} | ||
return scanner.Text(), nil | ||
} | ||
|
||
func new(cmd *cobra.Command, args []string) (err error) { | ||
var command string | ||
var description string | ||
|
||
var snippets snippet.Snippets | ||
if err := snippets.Load(); err != nil { | ||
return err | ||
} | ||
|
||
if len(args) > 0 { | ||
command = strings.Join(args, " ") | ||
fmt.Printf("%s %s\n", color.YellowString("Command:"), command) | ||
} else { | ||
command, err = scan(color.YellowString("Command: ")) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
description, err = scan(color.GreenString("Description: ")) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, s := range snippets.Snippets { | ||
if s.Description == description { | ||
return fmt.Errorf("Snippet [%s] already exists", description) | ||
} | ||
} | ||
|
||
newSnippet := snippet.SnippetInfo{ | ||
Description: description, | ||
Command: command, | ||
} | ||
snippets.Snippets = append(snippets.Snippets, newSnippet) | ||
if err = snippets.Save(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(newCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright © 2017 Teppei Fukuda | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/knqyf263/pet/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
version = "0.0.1" | ||
) | ||
|
||
var configFile string | ||
|
||
// RootCmd represents the base command when called without any subcommands | ||
var RootCmd = &cobra.Command{ | ||
Use: "pet", | ||
Short: "Simple command-line snippet manager.", | ||
Long: `pet - Simple command-line snippet manager.`, | ||
} | ||
|
||
// Execute adds all child commands to the root command sets flags appropriately. | ||
func Execute() { | ||
if err := RootCmd.Execute(); err != nil { | ||
fmt.Println(err) | ||
os.Exit(-1) | ||
} | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
RootCmd.AddCommand(versionCmd) | ||
|
||
RootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is $HOME/.config/pet/config.toml)") | ||
RootCmd.PersistentFlags().BoolVarP(&config.Flag.Debug, "debug", "", false, "debug mode") | ||
} | ||
|
||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Print the version number", | ||
Long: `Print the version number`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Printf("pet version %s\n", version) | ||
}, | ||
} | ||
|
||
// initConfig reads in config file and ENV variables if set. | ||
func initConfig() { | ||
if configFile == "" { | ||
dir, err := config.GetDefaultConfigDir() | ||
if err != nil { | ||
fmt.Fprintf(os.Stderr, "%v", err) | ||
os.Exit(1) | ||
} | ||
configFile = filepath.Join(dir, "config.toml") | ||
} | ||
|
||
if err := config.Conf.Load(configFile); err != nil { | ||
fmt.Fprintf(os.Stderr, "%v", err) | ||
os.Exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/knqyf263/pet/config" | ||
"github.com/spf13/cobra" | ||
"golang.org/x/crypto/ssh/terminal" | ||
) | ||
|
||
var delimiter string | ||
|
||
// searchCmd represents the search command | ||
var searchCmd = &cobra.Command{ | ||
Use: "search", | ||
Short: "Search snippets", | ||
Long: `Search snippets interactively (default filtering tool: peco)`, | ||
RunE: search, | ||
} | ||
|
||
func search(cmd *cobra.Command, args []string) (err error) { | ||
flag := config.Flag | ||
|
||
var options []string | ||
if flag.Query != "" { | ||
options = append(options, fmt.Sprintf("--query %s", flag.Query)) | ||
} | ||
commands, err := filter(options) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Print(strings.Join(commands, flag.Delimiter)) | ||
if terminal.IsTerminal(1) { | ||
fmt.Print("\n") | ||
} | ||
return nil | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(searchCmd) | ||
searchCmd.Flags().StringVarP(&config.Flag.Query, "query", "q", "", | ||
`Initial value for query`) | ||
searchCmd.Flags().StringVarP(&config.Flag.Delimiter, "delimiter", "d", "; ", | ||
`Use delim as the command delimiter character`) | ||
} |
Oops, something went wrong.