Skip to content

Commit

Permalink
initial public release! \o/
Browse files Browse the repository at this point in the history
Basic functionality is there for pretty much all commands/aspects of
things, but it definitely could still use some polish. And I'm sure more
features/use cases/edge cases will come up along the way.
  • Loading branch information
tjhop committed Apr 27, 2019
0 parents commit d69eace
Show file tree
Hide file tree
Showing 16 changed files with 1,143 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Random swap and lock files from editors
*.swp
*.lock
*.vim
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2019 TJ Hoplock <[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.
106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Clip - A Templated Clipboard Manager

## Overview
Clip is a CLI tool to let you manage templates and copy them to your clipboard so you can easily paste them into other forms or software. Clip templates can be templated using [Go templates](https://golang.org/pkg/text/template/), and they can be assigned tags to organize and filter your templates. Variables to be used in the templates can be either embedded directly into the template or put into the `clip` config file so they're available to all templates. Examples of the [config file](#Configuration) and [template files](#Templates) can be found below.

## Usage
(To see all available subcommands, run `clip --help`)

## Configuration
Clip uses a single configuration file (by default located at `$HOME/.clip.yml`). A basic config file will be created for you when you first run the command, but there are currently only 3 required keys:
```
~ $ cat $HOME/.clip.yml
editor: nano
templatedir: /your/home/directory/clip
vars:
name: Clip User
```
You'll need to edit this config file directly to change these default values.

Template configuration can be done almost entirely through the `clip` CLI and it's subcommands (create, edit, remove, rename, list, etc)

## Templates
Clip templates are YAML files with [Golang templated](https://golang.org/pkg/text/template/) text snippets and variables to use for substitutions in the template. Templates exist in a directory managed by Clip. By default, the template directory is `$HOME/clip/`, but this can be changed by editing the `templatedir` setting in the config file or passing the `--templatedir` flag at runtime.

The base template that gets created is pretty simple:
```
# See README.md for detailed information and examples
tags: []
template:
vars: {}
text: |
```
What the keys do:

| Key | Description | Configuration |
| --- | ----------- | ------------- |
| `tags` | Metadata tags that you'd like to apply to this template (purely for your organizational needs) | List |
| `template:vars` | Variables that will be available to the Go templating system when it renders your clip template. These variables override any global variables with the same name you define in the Clip config file. | Accepts an arbitrary number of `key: value` pairs to define variables and their values |
| `template:text` | The text to be rendered through Go's template system and loaded onto your clipboard | Accepts a YAML multi-line string (be careful with indentation!) |

Example template:
```
~ $ clip show readme-template-example
# See README.md for detailed information and examples
tags:
- personal
- side-project
- golang
template:
vars:
name: "tjhop"
greeting: "Hello, Clip User!"
signature: "Have fun!"
text: |
{{ .greeting }}
This is the general format of a Clip template file. The tags assigned to this template can be used to filter output when using the `list` command, like so:
`clip list --tagsonly`
`clip list --tags personal,golang
More info can be found using the `--help` flag on any of the subcommands, too.
{{ .signature }}
{{ .name }}
```

After copying the template, we'll end up with the following content on our clipboard:
```
~ $ clip copy readme-template-example
~ $ clip paste
Hello, Clip User!
This is the general format of a Clip template file. The tags assigned to this template can be used to filter output when using the `list` command, like so:
`clip list --tagsonly`
`clip list --tags personal,golang
More info can be found using the `--help` flag on any of the subcommands, too.
Have fun!
tjhop
```

## Building
Builds are using `go1.12.1` using [gox](https://github.com/mitchellh/gox):

```
~/go/src/github.com/tjhop/clip -> COMMIT=$(git rev-parse --short HEAD | tr -d "[ \r\n\']");
TAG=$(git describe --always --tags --abbrev=0 | tr -d "[v\r\n]");
echo "commit: $COMMIT"; echo "tag: $TAG";
gox -ldflags="-X github.com/tjhop/clip/cmd/root.builddate=$(date +%Y-%m-%d)
-X github.com/tjhop/clip/cmd/root.version=$TAG
-X github.com/tjhop/clip/cmd/root.commit=$COMMIT" \
-osarch "linux/amd64" -output="$GOBIN/{{ .OS }}/{{ .Arch }}/clip" \
-osarch "darwin/amd64" -output="$GOBIN/{{ .OS }}/{{ .Arch }}/clip"
```

## TODO
- [ ] allow editing Clip config directly through `clip` commands like template files?
- [ ] allow using different config file locations (viper has the ability to search config paths, I just couldn't think of other places I'd want the config during development)
- [ ] figure out how to post release binaries on github (never done it before ¯\\\_(ツ)_/¯)
- [ ] figure out how to report version/commit info that I'm bothering to embed in the build

## Credits/Thanks
Clip is written using the [Cobra](https://github.com/spf13/cobra) and [Viper](https://github.com/spf13/viper) libraries, with the clipboard management provided by [atotto/clipboard library](https://github.com/atotto/clipboard). They made my life a heck of a life easier, so thanks to them <3.
73 changes: 73 additions & 0 deletions cmd/copy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright © 2019 TJ Hoplock <[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.

package cmd

import (
"fmt"
"os"
"strings"
"path"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/atotto/clipboard"

"github.com/tjhop/clip/helpers"
)

var copyCmd = &cobra.Command{
Use: "copy <Clip template>",
Aliases: []string{"load", "in"},
Short: "Copy a Clip template to your clipboard",
Long: `Copy a Clip template to your clipboard`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
templateFilename := viper.GetString("templatedir") + "/" + os.Args[len(os.Args) - 1] + ".yml"
err := writeClipTemplateToClipboard(templateFilename)
if err != nil {
fmt.Printf("Failed to copy Clip template '%s' to clipboard: %v\n", strings.TrimSuffix(path.Base(templateFilename), filepath.Ext(templateFilename)), err)
}
},
}

func init() {
rootCmd.AddCommand(copyCmd)
}

func writeClipTemplateToClipboard(filename string) error {
tmpl, err := helpers.LoadTemplateFile(filename)
if err != nil {
return fmt.Errorf("Couldn't load Clip template file '%s': %v\n", strings.TrimSuffix(path.Base(filename), filepath.Ext(filename)), err)
}

renderedTemplateString, err := helpers.ExecuteTemplate(tmpl)
if err != nil {
return fmt.Errorf("Failed to render Go Template: %v\n", err)
}

err = clipboard.WriteAll(renderedTemplateString)
if err != nil {
return fmt.Errorf("Failed to write Clip template to clipboard: %v\n", err)
}

return nil
}
79 changes: 79 additions & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright © 2019 TJ Hoplock <[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.

package cmd

import (
"fmt"
"os"
"strings"
"path"
"path/filepath"
"io/ioutil"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const baseTemplateFileString string =
`# See README.md for detailed information and examples
tags: []
template:
vars: {}
text: |
`

// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create <Clip template>",
Aliases: []string{"add", "make"},
Short: "Create a new Clip template",
Long: `Create a Clip template. Clip templates are YAML files with embedded Go templates and variables.
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
templateFilename := viper.GetString("templatedir") + "/" + os.Args[len(os.Args) - 1] + ".yml"
err := writeTemplateFile(templateFilename)
if err != nil {
fmt.Printf("Call to create template failed: %v\n", err)
}
},
}

func init() {
rootCmd.AddCommand(createCmd)
}

func writeTemplateFile(filename string) error {
// create template file if it doesn't exist
if _, err := os.Stat(filename); os.IsNotExist(err) {
err := ioutil.WriteFile(filename, []byte(baseTemplateFileString), 0644)
if err != nil {
return fmt.Errorf("Failed to create template file: %v\n", err)
}

fmt.Printf("Clip template '%s' created\n", strings.TrimSuffix(path.Base(filename), filepath.Ext(filename)))
} else {
fmt.Printf("A Clip template with the name '%s' already exists\n", strings.TrimSuffix(path.Base(filename), filepath.Ext(filename)))
}

return nil
}
Loading

0 comments on commit d69eace

Please sign in to comment.