Skip to content

Commit

Permalink
style: go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
tjhop committed May 20, 2024
1 parent 2daa2f7 commit 8406683
Show file tree
Hide file tree
Showing 14 changed files with 526 additions and 527 deletions.
142 changes: 71 additions & 71 deletions cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,89 +21,89 @@
package cmd

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

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

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

var copyCmd = &cobra.Command{
Use: "copy <Clip template>",
Aliases: []string{"load", "in"},
Short: "Copy a Clip template/Stdin to your clipboard (default if just running `clip $arg`)",
Long: `Copy a Clip template or command output from Stdin to your clipboard`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
err := writeStdinToClipboard()
if err != nil {
fmt.Printf("Failed to copy from stdin: %v\n", err)
}
} else if len(args) == 1 {
templateFilename := filepath.Join(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(filepath.Base(templateFilename), filepath.Ext(templateFilename)), err)
}
}
},
Use: "copy <Clip template>",
Aliases: []string{"load", "in"},
Short: "Copy a Clip template/Stdin to your clipboard (default if just running `clip $arg`)",
Long: `Copy a Clip template or command output from Stdin to your clipboard`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
err := writeStdinToClipboard()
if err != nil {
fmt.Printf("Failed to copy from stdin: %v\n", err)
}
} else if len(args) == 1 {
templateFilename := filepath.Join(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(filepath.Base(templateFilename), filepath.Ext(templateFilename)), err)
}
}
},
}

func init() {
rootCmd.AddCommand(copyCmd)
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(filepath.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
tmpl, err := helpers.LoadTemplateFile(filename)
if err != nil {
return fmt.Errorf("Couldn't load Clip template file '%s': %v\n", strings.TrimSuffix(filepath.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
}

func writeStdinToClipboard() error {
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}

if info.Mode()&os.ModeCharDevice == os.ModeCharDevice {
fmt.Println("Invalid input device for stdin")
} else {
scanner := bufio.NewScanner(os.Stdin)
var output []string

for scanner.Scan() {
output = append(output, scanner.Text())
}

if scanner.Err() != nil {
return fmt.Errorf("Failed to read data from stdin: %v\n", err)
}

err := clipboard.WriteAll(strings.Join(output, "\n"))
if err != nil {
return fmt.Errorf("Failed to write data from stdin to clipboard: %v\n", err)
}
}

return nil
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}

if info.Mode()&os.ModeCharDevice == os.ModeCharDevice {
fmt.Println("Invalid input device for stdin")
} else {
scanner := bufio.NewScanner(os.Stdin)
var output []string

for scanner.Scan() {
output = append(output, scanner.Text())
}

if scanner.Err() != nil {
return fmt.Errorf("Failed to read data from stdin: %v\n", err)
}

err := clipboard.WriteAll(strings.Join(output, "\n"))
if err != nil {
return fmt.Errorf("Failed to write data from stdin to clipboard: %v\n", err)
}
}

return nil
}
65 changes: 32 additions & 33 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,17 @@
package cmd

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

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

const baseTemplateFileString string =
`# See README.md for detailed information
const baseTemplateFileString string = `# See README.md for detailed information
#
# Example template:
#
Expand All @@ -54,37 +53,37 @@ template:

// 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.
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 := filepath.Join(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)
}
},
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
templateFilename := filepath.Join(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)
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)
}
// 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(filepath.Base(filename), filepath.Ext(filename)))
} else {
fmt.Printf("A Clip template with the name '%s' already exists\n", strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)))
}
fmt.Printf("Clip template '%s' created\n", strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)))
} else {
fmt.Printf("A Clip template with the name '%s' already exists\n", strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)))
}

return nil
return nil
}
102 changes: 51 additions & 51 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,81 +21,81 @@
package cmd

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

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

var (
editor string
editor string
)

var editCmd = &cobra.Command{
Use: "edit <Clip template>",
Short: "Open Clip template in text editor",
Long: `Open Clip template in text editor.
Use: "edit <Clip template>",
Short: "Open Clip template in text editor",
Long: `Open Clip template in text editor.
Clip will check the following locations for the editor to use:
Clip config file
Command line flag (--editor)
$EDITOR environment variable
Default (nano)
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
templateFilename := filepath.Join(viper.GetString("templatedir"), os.Args[len(os.Args) - 1] + ".yml")
err := openClipTemplateInEditor(templateFilename)
if err != nil {
fmt.Printf("Failed to open Clip template for editing: %v\n", err)
}
},
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
templateFilename := filepath.Join(viper.GetString("templatedir"), os.Args[len(os.Args)-1]+".yml")
err := openClipTemplateInEditor(templateFilename)
if err != nil {
fmt.Printf("Failed to open Clip template for editing: %v\n", err)
}
},
}

func init() {
rootCmd.AddCommand(editCmd)
rootCmd.AddCommand(editCmd)

// command Line flags
editCmd.Flags().StringVarP(&editor, "editor", "e", "", "location of template directory (default is $HOME/clip)")
// command Line flags
editCmd.Flags().StringVarP(&editor, "editor", "e", "", "location of template directory (default is $HOME/clip)")

// use viper to bind config to CLI flags
viper.BindPFlag("editor", editCmd.Flags().Lookup("editor"))
// use viper to bind config to CLI flags
viper.BindPFlag("editor", editCmd.Flags().Lookup("editor"))
}

func openClipTemplateInEditor(filename string) error {
var editor string
var editor string

// check if clip template exists yet. if it doesn't, make it
if _, err := os.Stat(filename); os.IsNotExist(err) {
err = writeTemplateFile(filename)
if err != nil {
return fmt.Errorf("Call to create Clip template file failed: %v\n", err)
}
}
// check if clip template exists yet. if it doesn't, make it
if _, err := os.Stat(filename); os.IsNotExist(err) {
err = writeTemplateFile(filename)
if err != nil {
return fmt.Errorf("Call to create Clip template file failed: %v\n", err)
}
}

if viper.GetString("editor") != "" {
editor = viper.GetString("editor")
_, err := exec.LookPath(editor)
if err != nil {
return fmt.Errorf("Could not find an editor named '%s' in your PATH: %v\n", editor, err)
}
} else {
return fmt.Errorf("No editor defined!")
}
if viper.GetString("editor") != "" {
editor = viper.GetString("editor")
_, err := exec.LookPath(editor)
if err != nil {
return fmt.Errorf("Could not find an editor named '%s' in your PATH: %v\n", editor, err)
}
} else {
return fmt.Errorf("No editor defined!")
}

// build command to run
cmd := exec.Command(editor, filename)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("Failed to open Clip template '%s' in %s: %v\n", strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)), editor, err)
}
// build command to run
cmd := exec.Command(editor, filename)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return fmt.Errorf("Failed to open Clip template '%s' in %s: %v\n", strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)), editor, err)
}

return nil
return nil
}
Loading

0 comments on commit 8406683

Please sign in to comment.