Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Apr 19, 2017
0 parents commit de67a9a
Show file tree
Hide file tree
Showing 15 changed files with 1,335 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
gm
bin
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
The MIT License (MIT)
Copyright © 2017 Masaki Ishiyama <[email protected]>

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.
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
TEST?=./...
DEPS = $(shell go list -f '{{range .TestImports}}{{.}} {{end}}' ./...)

all: build

build: deps
mkdir -p bin
go build -o bin/gist

install: build
install -m 755 ./bin/gist ~/bin/gist

deps:
go get -d -v ./...
echo $(DEPS) | xargs -n1 go get -d

test: deps
go test $(TEST) $(TESTARGS) -timeout=3s -parallel=4
go vet $(TEST)
go test $(TEST) -race

.PHONY: all build deps test
92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<img src="https://raw.githubusercontent.com/b4b4r07/i/master/gist/logo.png" width="200">

> A simple gist editor for CLI
>
> <img src="https://octodex.github.com/images/megacat-2.png" width="200">
## Pros

- Simple and intuitive
- Just select and edit gist you want
- Can use any editors what you want
- Work with [peco](https://github.com/peco/peco) and [fzf](https://github.com/junegunn/fzf)
- Automatically synchronized after editing
- Customizable
- A few options and small TOML
- Easy to install
- Go! single binary

***DEMO***

<img src="https://raw.githubusercontent.com/b4b4r07/i/master/gist/demo.gif" width="500">

## Usage

Currently gist supports the following commands:

```console
$ gist help
gist - A simple gist editor for CLI

Usage:
gist [flags]
gist [command]

Available Commands:
config Config the setting file
delete Delete gist files
edit Edit the gist file and sync after
new Create a new gist
open Open user's gist

Flags:
-v, --version show the version and exit

Use "gist [command] --help" for more information about a command.
```

### Configurations

Well-specified options and user-specific settings can be described in a toml file. It can be changed with the `gist set` command.

```toml
[Core]
Editor = "vim"
selectcmd = "fzf-tmux --multi:fzf:peco:percol"
tomlfile = "/Users/b4b4r07/.config/gist/config.toml"
user = "b4b4r07"

[Gist]
token = "your_github_token"
dir = "/Users/b4b4r07/.config/gist/files"

[Flag]
open_url = false
private = false
verbose = true
show_spinner = true
```

This behavior was heavily inspired by [mattn/memo](https://github.com/mattn/memo), thanks!

## Installation

```console
$ go get github.com/b4b4r07/gist
```

## Versus

There are many other implements as the gist client (called "gister") such as the following that works on command-line:

- <https://github.com/defunkt/gist>
- <https://github.com/jdowner/gist>
- ...

## License

MIT

## Author

b4b4r07
30 changes: 30 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"path/filepath"

"github.com/b4b4r07/gist/config"
"github.com/b4b4r07/gist/util"
"github.com/spf13/cobra"
)

var confCmd = &cobra.Command{
Use: "config",
Short: "Config the setting file",
Long: "Config the setting file with your editor (default: vim)",
RunE: conf,
}

func conf(cmd *cobra.Command, args []string) error {
editor := config.Conf.Core.Editor
tomlfile := config.Conf.Core.TomlFile
if tomlfile == "" {
dir, _ := config.GetDefaultDir()
tomlfile = filepath.Join(dir, "config.toml")
}
return util.RunCommand(editor, tomlfile)
}

func init() {
RootCmd.AddCommand(confCmd)
}
62 changes: 62 additions & 0 deletions cmd/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package cmd

import (
"log"

"github.com/b4b4r07/gist/config"
"github.com/b4b4r07/gist/gist"
"github.com/b4b4r07/gist/util"
"github.com/spf13/cobra"
)

var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete gist files",
Long: "Delete gist files on the remote",
RunE: delete,
}

func delete(cmd *cobra.Command, args []string) error {
var err error

gist, err := gist.New(config.Conf.Gist.Token)
if err != nil {
return err
}

gfs, err := gist.GetRemoteFiles()
if err != nil {
return err
}

selectedLines, err := util.Filter(gfs.Text)
if err != nil {
return err
}

var ids []string
for _, line := range selectedLines {
if line == "" {
continue
}
parsedLine, err := util.ParseLine(line)
if err != nil {
continue
}
ids = append(ids, gfs.ExtendID(parsedLine.ID))
}

ids = util.UniqueArray(ids)
for _, id := range ids {
err = gist.Delete(id)
if err != nil {
log.Printf("[ERROR] %v", err)
}
}

return nil
}

func init() {
RootCmd.AddCommand(deleteCmd)
}
65 changes: 65 additions & 0 deletions cmd/edit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package cmd

import (
"path/filepath"

"github.com/b4b4r07/gist/config"
"github.com/b4b4r07/gist/gist"
"github.com/b4b4r07/gist/util"
"github.com/spf13/cobra"
)

var editCmd = &cobra.Command{
Use: "edit",
Short: "Edit the gist file and sync after",
Long: "Edit the gist file and sync after",
RunE: edit,
}

func edit(cmd *cobra.Command, args []string) error {
var err error

gist, err := gist.New(config.Conf.Gist.Token)
if err != nil {
return err
}

gfs, err := gist.GetRemoteFiles()
if err != nil {
return err
}

selectedLines, err := util.Filter(gfs.Text)
if err != nil {
return err
}

for _, line := range selectedLines {
if line == "" {
continue
}
var url string
parsedLine, err := util.ParseLine(line)
if err != nil {
continue
}

file := filepath.Join(config.Conf.Gist.Dir, gfs.ExtendID(parsedLine.ID), parsedLine.Filename)
err = gist.Edit(file)
if err != nil {
return err
}

// TODO: FIXME: gist.Edit
if config.Conf.Flag.OpenURL {
util.Open(url)
}
}

return nil
}

func init() {
RootCmd.AddCommand(editCmd)
editCmd.Flags().BoolVarP(&config.Conf.Flag.OpenURL, "open", "o", false, "Open with the default browser")
}
Loading

0 comments on commit de67a9a

Please sign in to comment.