Skip to content

Commit

Permalink
Create a linter for filenames when using ouput-dir
Browse files Browse the repository at this point in the history
  • Loading branch information
neilnaveen committed Jul 19, 2024
1 parent a9bf2e2 commit 88d0a97
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 1 addition & 2 deletions cmd/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/bit-bom/bitbom/pkg"
Expand Down Expand Up @@ -52,7 +51,7 @@ func (o *options) Run(_ *cobra.Command, args []string) error {
return err
}

filePath := filepath.Join(o.outputdir, strconv.Itoa(int(node.Id))+".json")
filePath := filepath.Join(o.outputdir, pkg.SanitizeFilename(node.Name)+".json")
file, err := os.Create(filePath)
if err != nil {
return err
Expand Down
15 changes: 15 additions & 0 deletions pkg/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pkg

import "strings"

// SanitizeFilename replaces characters that are not allowed in filenames with underscores.
func SanitizeFilename(filename string) string {
// Define a set of characters that are not allowed in filenames.
invalidChars := []string{"/", "\\", ":", "*", "?", "\"", "<", ">", "|"}

// Replace each invalid character with an underscore.
for _, char := range invalidChars {
filename = strings.ReplaceAll(filename, char, "_")
}
return filename
}

0 comments on commit 88d0a97

Please sign in to comment.