Skip to content

Commit

Permalink
build paths using filepath.Join for proper cross-platform paths
Browse files Browse the repository at this point in the history
Build all paths using `filepath.Join` so it actually uses the os path
separator and provides proper cross platform support. Also switches out
the remaining `path` library uses (for stuff like `path.Base`) to use
the `path/filepath` library equivalent, since that seems to be the
suggested library to use when working directly with files instead of
higher level objects.
  • Loading branch information
tjhop committed Apr 29, 2019
1 parent 9833012 commit b692ff6
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 29 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Release archive creation:
- [X] figure out how to post release binaries on github (never done it before ¯\\\_(ツ)_/¯)
- [X] figure out how to report version/commit info that I'm bothering to embed in the build
- [X] fix reading environment var for EDITOR so it actually overrides config defaults
- [ ] convert all manual filepath building to use `path.Join`
- [X] convert all manual filepath building to use `path.Join`

## 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.
7 changes: 3 additions & 4 deletions cmd/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"fmt"
"os"
"strings"
"path"
"path/filepath"

"github.com/spf13/cobra"
Expand All @@ -41,10 +40,10 @@ var copyCmd = &cobra.Command{
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"
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(path.Base(templateFilename), filepath.Ext(templateFilename)), err)
fmt.Printf("Failed to copy Clip template '%s' to clipboard: %v\n", strings.TrimSuffix(filepath.Base(templateFilename), filepath.Ext(templateFilename)), err)
}
},
}
Expand All @@ -56,7 +55,7 @@ func init() {
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)
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)
Expand Down
7 changes: 3 additions & 4 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"fmt"
"os"
"strings"
"path"
"path/filepath"
"io/ioutil"

Expand Down Expand Up @@ -62,7 +61,7 @@ var createCmd = &cobra.Command{
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
templateFilename := viper.GetString("templatedir") + "/" + os.Args[len(os.Args) - 1] + ".yml"
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)
Expand All @@ -82,9 +81,9 @@ func writeTemplateFile(filename string) error {
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)))
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(path.Base(filename), filepath.Ext(filename)))
fmt.Printf("A Clip template with the name '%s' already exists\n", strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)))
}

return nil
Expand Down
5 changes: 2 additions & 3 deletions cmd/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"os"
"os/exec"
"strings"
"path"
"path/filepath"

"github.com/spf13/cobra"
Expand All @@ -49,7 +48,7 @@ Clip will check the following locations for the editor to use:
`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
templateFilename := viper.GetString("templatedir") + "/" + os.Args[len(os.Args) - 1] + ".yml"
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)
Expand Down Expand Up @@ -95,7 +94,7 @@ func openClipTemplateInEditor(filename string) error {
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(path.Base(filename), filepath.Ext(filename)), editor, err)
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
Expand Down
4 changes: 2 additions & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func listTemplates(dir string) error {
// if `--tags` filter was provided, check if file contains one of the provided tags
if len(tags) > 0 {
for _, tag := range tags {
tmpl, err := helpers.LoadTemplateFile(dir + "/" + info.Name())
tmpl, err := helpers.LoadTemplateFile(filepath.Join(dir, info.Name()))
if err != nil {
return fmt.Errorf("Couldn't load Clip template '%s' to check for tags: %v\n", filenameWithoutExtension, err)
}
Expand Down Expand Up @@ -121,7 +121,7 @@ func listTemplateTags(dir string) error {
// walk the template directory and get the files
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() && (filepath.Ext(path) == ".yaml" || filepath.Ext(path) == ".yml") {
tmpl, err := helpers.LoadTemplateFile(dir + "/" + info.Name())
tmpl, err := helpers.LoadTemplateFile(filepath.Join(dir, info.Name()))
if err != nil {
return fmt.Errorf("Couldn't load Clip template to check for tags: %v\n", err)
}
Expand Down
7 changes: 3 additions & 4 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package cmd
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -39,7 +38,7 @@ var removeCmd = &cobra.Command{
Long: `Delete a Clip template from your template folder`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
templateFilename := viper.GetString("templatedir") + "/" + os.Args[len(os.Args) - 1] + ".yml"
templateFilename := filepath.Join(viper.GetString("templatedir"), os.Args[len(os.Args) - 1] + ".yml")
err := removeTemplateFile(templateFilename)
if err != nil {
fmt.Printf("Call to remove Clip template failed: %v\n", err)
Expand All @@ -54,14 +53,14 @@ func init() {
func removeTemplateFile(filename string) error {
// check if template even exists
if _, err := os.Stat(filename); os.IsNotExist(err) {
fmt.Printf("Couldn't find a Clip template with the name: '%s'\n", strings.TrimSuffix(path.Base(filename), filepath.Ext(filename)))
fmt.Printf("Couldn't find a Clip template with the name: '%s'\n", strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)))
} else {
err := os.Remove(filename)
if err != nil {
return fmt.Errorf("Failed to remove Clip template file: %v\n", err)
}

fmt.Printf("Clip template '%s' removed\n", strings.TrimSuffix(path.Base(filename), filepath.Ext(filename)))
fmt.Printf("Clip template '%s' removed\n", strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)))
}

return nil
Expand Down
9 changes: 4 additions & 5 deletions cmd/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package cmd
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -40,8 +39,8 @@ var renameCmd = &cobra.Command{
and the new name must be available.`,
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
sourceTemplateFilename := viper.GetString("templatedir") + "/" + os.Args[len(os.Args) - 2] + ".yml"
destinationTemplateFilename := viper.GetString("templatedir") + "/" + os.Args[len(os.Args) - 1] + ".yml"
sourceTemplateFilename := filepath.Join(viper.GetString("templatedir"), os.Args[len(os.Args) - 2] + ".yml")
destinationTemplateFilename := filepath.Join(viper.GetString("templatedir"), os.Args[len(os.Args) - 1] + ".yml")
err := renameTemplateFile(sourceTemplateFilename, destinationTemplateFilename)
if err != nil {
fmt.Printf("Call to rename template failed: %v\n", err)
Expand All @@ -56,12 +55,12 @@ func init() {
func renameTemplateFile(sourceFilename, destinationFilename string) error {
// check to ensure source template exists
if _, err := os.Stat(sourceFilename); os.IsNotExist(err) {
fmt.Printf("Couldn't find a Clip template with the name: '%s'\n", strings.TrimSuffix(path.Base(sourceFilename), filepath.Ext(sourceFilename)))
fmt.Printf("Couldn't find a Clip template with the name: '%s'\n", strings.TrimSuffix(filepath.Base(sourceFilename), filepath.Ext(sourceFilename)))
}

// check to ensure destination template does not exist
if _, err := os.Stat(destinationFilename); err == nil {
fmt.Printf("No action taken: Found an existing Clip template with the name: '%s'\n", strings.TrimSuffix(path.Base(destinationFilename), filepath.Ext(destinationFilename)))
fmt.Printf("No action taken: Found an existing Clip template with the name: '%s'\n", strings.TrimSuffix(filepath.Base(destinationFilename), filepath.Ext(destinationFilename)))
}

err := os.Rename(sourceFilename, destinationFilename)
Expand Down
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ package cmd
import (
"fmt"
"os"
"path"
"path/filepath"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -100,15 +100,15 @@ func initClip() {
viper.SetConfigFile(cfgFile)
} else {
// Set config file to home directory + name ".clip.yml"
cfgFile = path.Join(home, "." + cfgName) + ".yml"
cfgFile = filepath.Join(home, "." + cfgName + ".yml")
viper.SetConfigType("yaml")
viper.SetConfigFile(cfgFile)
}

if viper.GetString("templatedir") != "" {
viper.Set("templatedir", templateDir)
} else {
viper.Set("templatedir", home + "/" + cfgName)
viper.Set("templatedir", filepath.Join(home, cfgName))
}

viper.AutomaticEnv() // read in environment variables that match
Expand Down
5 changes: 2 additions & 3 deletions cmd/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"fmt"
"os"
"strings"
"path"
"path/filepath"
"io/ioutil"

Expand All @@ -39,7 +38,7 @@ var showCmd = &cobra.Command{
Long: `Show the output of the raw clip template file (pretty much just cat the file)`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
templateFilename := viper.GetString("templatedir") + "/" + os.Args[len(os.Args) - 1] + ".yml"
templateFilename := filepath.Join(viper.GetString("templatedir"), os.Args[len(os.Args) - 1] + ".yml")
err := showClipTemplate(templateFilename)
if err != nil {
fmt.Printf("Call to show template failed: %v\n", err)
Expand All @@ -54,7 +53,7 @@ func init() {
func showClipTemplate(filename string) error {
// check if template file exists
if _, err := os.Stat(filename); os.IsNotExist(err) {
fmt.Printf("Couldn't find a clip template with the name: '%s'\n", strings.TrimSuffix(path.Base(filename), filepath.Ext(filename)))
fmt.Printf("Couldn't find a clip template with the name: '%s'\n", strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)))
} else {
buf, err := ioutil.ReadFile(filename)
if err != nil {
Expand Down

0 comments on commit b692ff6

Please sign in to comment.