Skip to content

Commit

Permalink
Merge pull request #328 from csessh/bug-filename-serialization
Browse files Browse the repository at this point in the history
Omit Filename attribute when saving snippets to file
  • Loading branch information
RamiAwar authored Dec 3, 2024
2 parents 97f9f9d + 756906a commit acc929d
Show file tree
Hide file tree
Showing 5 changed files with 343 additions and 29 deletions.
12 changes: 8 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ require (
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
)

require (
github.com/kennygrant/sanitize v1.2.4
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61
)
require gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61

require (
github.com/awesome-gocui/gocui v1.1.0
Expand All @@ -34,6 +31,12 @@ require (
golang.org/x/term v0.15.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
github.com/alessio/shellescape v1.4.1 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
Expand All @@ -44,6 +47,7 @@ require (
github.com/hashicorp/go-retryablehttp v0.6.8 // indirect
github.com/lucasb-eyer/go-colorful v1.0.3 // indirect
github.com/rivo/uniseg v0.1.0 // indirect
github.com/stretchr/testify v1.10.0
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
9 changes: 5 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ github.com/hashicorp/go-retryablehttp v0.6.8 h1:92lWxgpa+fF3FozM4B3UZtHZMJX8T5XT
github.com/hashicorp/go-retryablehttp v0.6.8/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/kennygrant/sanitize v1.2.4 h1:gN25/otpP5vAsO2djbMhF/LQX6R7+O1TB4yv8NzpJ3o=
github.com/kennygrant/sanitize v1.2.4/go.mod h1:LGsjYYtgxbetdg5owWB2mpgUL6e2nfw2eObZ0u0qvak=
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
Expand Down Expand Up @@ -68,8 +66,9 @@ github.com/spf13/pflag v1.0.1 h1:aCvUg6QPl3ibpQUxyLkrEkCHtPqYJL4x9AuhqVqFis4=
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/xanzy/go-gitlab v0.50.3 h1:M7ncgNhCN4jaFNyXxarJhCLa9Qi6fdmCxFFhMTQPZiY=
github.com/xanzy/go-gitlab v0.50.3/go.mod h1:Q+hQhV508bDPoBijv7YjK/Lvlb4PhVhJdKqXVQrUoAE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down Expand Up @@ -108,6 +107,8 @@ google.golang.org/appengine v1.3.0 h1:FBSsiFRMz3LBeXIomRnVzrQwSDj4ibvcRexLG0LZGQ
google.golang.org/appengine v1.3.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61 h1:8ajkpB4hXVftY5ko905id+dOnmorcS2CHNxxHLLDcFM=
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61/go.mod h1:IfMagxm39Ys4ybJrDb7W3Ob8RwxftP0Yy+or/NVz1O8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
43 changes: 24 additions & 19 deletions snippet/snippet.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"fmt"
"os"
"sort"
"strings"

"github.com/kennygrant/sanitize"
"github.com/knqyf263/pet/config"
"github.com/knqyf263/pet/path"
"github.com/pelletier/go-toml"
Expand All @@ -18,7 +16,7 @@ type Snippets struct {
}

type SnippetInfo struct {
Filename string
Filename string `toml:"-"`
Description string
Command string `toml:"command,multiline"`
Tag []string
Expand Down Expand Up @@ -97,31 +95,38 @@ if you only want to provide snippetdirs instead`,

// Save saves the snippets to toml file.
func (snippets *Snippets) Save() error {
var snippetFile string
var newSnippets Snippets
snippetFiles := make(map[string][]SnippetInfo)

// Need to construct a bunch of snippet files if we have multiple snippet files and then save them all
for _, snippet := range snippets.Snippets {
if snippet.Filename == "" {
snippetFile = config.Conf.General.SnippetDirs[0] + fmt.Sprintf("%s.toml", strings.ToLower(sanitize.BaseName(snippet.Description)))
newSnippets.Snippets = append(newSnippets.Snippets, snippet)
} else if snippet.Filename == config.Conf.General.SnippetFile {
snippetFile = config.Conf.General.SnippetFile
newSnippets.Snippets = append(newSnippets.Snippets, snippet)
// No filename => just save to main snippet file
snippet.Filename = config.Conf.General.SnippetFile
}
snippetFiles[snippet.Filename] = append(snippetFiles[snippet.Filename], snippet)
}

absFile, err := path.NewAbsolutePath(snippetFile)
if err != nil {
return err
}
// Save all snippet files
for file, snippets := range snippetFiles {
absFilePath, err := path.NewAbsolutePath(file)
if err != nil {
return fmt.Errorf("failed to save snippet file. err: %s", err)
}

f, err := os.Create(absFile.Get())
if err != nil {
return fmt.Errorf("failed to save snippet file. err: %s", err)
// Overwrite snippet file with snippets
f, err := os.Create(absFilePath.Get())
if err != nil {
return fmt.Errorf("failed to save snippet file. err: %s", err)
}
defer f.Close()

err = toml.NewEncoder(f).Encode(Snippets{Snippets: snippets})
if err != nil {
return fmt.Errorf("failed to encode snippets while saving snippet file. err: %s", err)
}
}

defer f.Close()
return toml.NewEncoder(f).Encode(snippets)
return nil
}

// ToString returns the contents of toml file.
Expand Down
Loading

0 comments on commit acc929d

Please sign in to comment.