Skip to content

Commit

Permalink
add support for using Project Snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwood committed Jan 18, 2022
1 parent 467b6df commit 5c19bf0
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 50 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ work:
gitlab_url: https://url.of.work.server/
token: USERTOKENWORK
clipboard_name: glsnip
project_id: 12345678
...
```

If you set the project_id in a server profile, a project-based Gitlab Snippet
API will be used.

You may also specify an alternative location for the configuration file with the
`--config` flag.

Expand Down
105 changes: 74 additions & 31 deletions cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ func Copy(cmd *cobra.Command, args []string) {

if (stat.Mode() & os.ModeCharDevice) == 0 { // we were piped into
reader := bufio.NewReader(os.Stdin)
copy(args, git, viper.GetString("clipboard_name"), visibility, reader)
copy(args, git, viper.GetString("clipboard_name"), viper.GetInt("project_id"), visibility, reader)
} else { // invoked without a pipe or redirect
println("ERROR: Please pipe something into STDIN")
os.Exit(1)
}
}

func copy(args []string, git gitlab.Client, clipboardName string, visibility string, reader RuneReader) {
func copy(args []string, git gitlab.Client, clipboardName string, projectID int, visibility string, reader RuneReader) {

// read stdin
var output []rune
Expand All @@ -63,47 +63,90 @@ func copy(args []string, git gitlab.Client, clipboardName string, visibility str
BailOnError(errors.New("Bad visibility"), "Bad visibility. Must be `private`, `internal or `public`")
}

// search snippets for a clipboard with the correct name to update
snippets, _, err := git.Snippets.ListSnippets(&gitlab.ListSnippetsOptions{})
if projectID > 0 { // Project Snippet
// search snippets for a clipboard with the correct name to update
snippets, _, err := git.ProjectSnippets.ListSnippets(projectID, &gitlab.ListProjectSnippetsOptions{})
BailOnError(err, "Could not read Project Snippets")
var clipboardFound bool = false
var clipboardID int

BailOnError(err, "Could not read Snippets")
for _, item := range snippets {

var clipboardFound bool = false
var clipboardID int
if item.Title == clipboardName {
clipboardFound = true
clipboardID = item.ID
break
}
}

for _, item := range snippets {
// create a new snippet
if !clipboardFound {
snippetoptions := &gitlab.CreateProjectSnippetOptions{
Title: gitlab.String(clipboardName),
FileName: gitlab.String(clipboardName),
Content: gitlab.String(string(output)),
Visibility: gitlab.Visibility(gitlab.VisibilityValue(visibility)),
}

if item.Title == clipboardName {
clipboardFound = true
clipboardID = item.ID
break
}
}
_, _, err = git.ProjectSnippets.CreateSnippet(projectID, snippetoptions)

BailOnError(err, "Could not create Snippet")

} else { // update existing snippet
snippetoptions := &gitlab.UpdateProjectSnippetOptions{
Title: gitlab.String(clipboardName),
FileName: gitlab.String(clipboardName),
Content: gitlab.String(string(output)),
Visibility: gitlab.Visibility(gitlab.VisibilityValue(visibility)),
}

_, _, err = git.ProjectSnippets.UpdateSnippet(projectID, clipboardID, snippetoptions)

// create a new snippet
if !clipboardFound {
snippetoptions := &gitlab.CreateSnippetOptions{
Title: gitlab.String(clipboardName),
FileName: gitlab.String(clipboardName),
Content: gitlab.String(string(output)),
Visibility: gitlab.Visibility(gitlab.VisibilityValue(visibility)),
BailOnError(err, "Could not update snippet")
}

_, _, err = git.Snippets.CreateSnippet(snippetoptions)
} else { // Personal Snippet
// search snippets for a clipboard with the correct name to update
snippets, _, err := git.Snippets.ListSnippets(&gitlab.ListSnippetsOptions{})
BailOnError(err, "Could not read Personal Snippets")
var clipboardFound bool = false
var clipboardID int

BailOnError(err, "Could not create Snippet")
for _, item := range snippets {

} else { // update existing snippet
snippetoptions := &gitlab.UpdateSnippetOptions{
Title: gitlab.String(clipboardName),
FileName: gitlab.String(clipboardName),
Content: gitlab.String(string(output)),
Visibility: gitlab.Visibility(gitlab.VisibilityValue(visibility)),
if item.Title == clipboardName {
clipboardFound = true
clipboardID = item.ID
break
}
}

_, _, err = git.Snippets.UpdateSnippet(clipboardID, snippetoptions)
// create a new snippet
if !clipboardFound {
snippetoptions := &gitlab.CreateSnippetOptions{
Title: gitlab.String(clipboardName),
FileName: gitlab.String(clipboardName),
Content: gitlab.String(string(output)),
Visibility: gitlab.Visibility(gitlab.VisibilityValue(visibility)),
}

_, _, err = git.Snippets.CreateSnippet(snippetoptions)

BailOnError(err, "Could not create Snippet")

} else { // update existing snippet
snippetoptions := &gitlab.UpdateSnippetOptions{
Title: gitlab.String(clipboardName),
FileName: gitlab.String(clipboardName),
Content: gitlab.String(string(output)),
Visibility: gitlab.Visibility(gitlab.VisibilityValue(visibility)),
}

_, _, err = git.Snippets.UpdateSnippet(clipboardID, snippetoptions)

BailOnError(err, "Could not update snippet")
}

BailOnError(err, "Could not update snippet")
}

}
4 changes: 2 additions & 2 deletions cmd/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestCopyUpdate(t *testing.T) {

var emptyStringArray []string

copy(emptyStringArray, *client, "glsnip", "internal", bytes.NewBufferString("std in"))
copy(emptyStringArray, *client, "glsnip", -1, "internal", bytes.NewBufferString("std in"))

}

Expand Down Expand Up @@ -102,6 +102,6 @@ func TestCopyCreate(t *testing.T) {

var emptyStringArray []string

copy(emptyStringArray, *client, "glsnip", "private", bytes.NewBufferString("std in"))
copy(emptyStringArray, *client, "glsnip", -1, "private", bytes.NewBufferString("std in"))

}
44 changes: 29 additions & 15 deletions cmd/paste.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,41 @@ func init() {
// Paste implements the paste command
func Paste(cmd *cobra.Command, args []string) {
git := GetGitlabClient()
output := paste(args, git, viper.GetString("clipboard_name"))
output := paste(args, git, viper.GetString("clipboard_name"), viper.GetInt("project_id"))
fmt.Print(output)
}

func paste(args []string, git gitlab.Client, clipboardName string) string {
func paste(args []string, git gitlab.Client, clipboardName string, projectID int) string {

var output string

snippets, _, err := git.Snippets.ListSnippets(&gitlab.ListSnippetsOptions{})

BailOnError(err, "Could not read Snippets")

for _, item := range snippets {

if item.Title == clipboardName {
snip, _, err := git.Snippets.SnippetContent(item.ID)
BailOnError(err, "Could not read Snippet contents")
output = string(snip)
break
if projectID > 0 { // Project Snippet
snippets, _, err := git.ProjectSnippets.ListSnippets(projectID, &gitlab.ListProjectSnippetsOptions{})
BailOnError(err, "Could not read Project Snippets")
for _, item := range snippets {

if item.Title == clipboardName {
snip, _, err := git.Snippets.SnippetContent(item.ID)
BailOnError(err, "Could not read Snippet contents")
output = string(snip)
break
}
}
}
return output

} else { // Personal Snippet
snippets, _, err := git.Snippets.ListSnippets(&gitlab.ListSnippetsOptions{})
BailOnError(err, "Could not read Personal Snippets")
for _, item := range snippets {

if item.Title == clipboardName {
snip, _, err := git.Snippets.SnippetContent(item.ID)
BailOnError(err, "Could not read Snippet contents")
output = string(snip)
break
}
}
return output

return output
}
}
2 changes: 1 addition & 1 deletion cmd/paste_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestPaste(t *testing.T) {

var emptyStringArray []string

output := paste(emptyStringArray, *client, "glsnip")
output := paste(emptyStringArray, *client, "glsnip", -1)

assert.Equal(t, "PASTED_DATA", output)
}
7 changes: 6 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var cfgFile, profile string
var cfgFileFound bool = true

var rootCmd = &cobra.Command{
Version: "0.2.0",
Version: "0.3.0",
Use: "glsnip",
Short: "Copy and paste using GitLab Snippets",
Long: `This app behaves like pbcopy(1) and pbpaste(1) on a Mac, or like xclip(1) on
Expand All @@ -41,7 +41,11 @@ Configuration:
gitlab_url: https://url.of.work.server/
token: USERTOKENWORK
clipboard_name: glsnip
project_id: 12345678
...
If you set the project_id in a server profile, a project-based Gitlab Snippet
API will be used.
Environment variables:
Instead of using a configuration file, you may set environment variables by
Expand Down Expand Up @@ -86,6 +90,7 @@ func initConfig() {

// set defaults on top-level viper settings
viper.SetDefault("clipboard_name", "glsnip")
viper.SetDefault("project_id", -1)

// try read in config file
if err := viper.ReadInConfig(); err != nil {
Expand Down

0 comments on commit 5c19bf0

Please sign in to comment.