Skip to content

Commit

Permalink
feat(normalise): add --quote-values option
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta committed Dec 18, 2024
1 parent f5e25bc commit e7fddcc
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
29 changes: 21 additions & 8 deletions cmd/cmd_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,35 @@ func TestRoot_testdata(t *testing.T) {

func TestNormalise_testdata(t *testing.T) {
for _, test := range []struct {
testPath string
transformsArgs []string
exitCode int
output string
testPath string
transformArgs []string
exitCode int
output string
}{
{
testPath: "../testdata/success_normalise_infotexts.md",
transformsArgs: []string{"-t", "filename=title"},
exitCode: 0,
testPath: "../testdata/success_normalise_infotexts.md",
transformArgs: []string{"-t", "filename=title"},
exitCode: 0,
output: `# Success: normalise info texts
The normalise command with ` + "`" + `-t filename=title` + "`" + ` transform argument should remove and ` + "`" + `no_value` + "`" + ` and ` + "`" + `key=value` + "`" + ` args and replace ` + "`" + `filename` + "`" + ` key with ` + "`" + `title` + "`" + `,
` + "```" + `sh title=true.sh
exit 0
` + "```" + `
`,
},
{
testPath: "../testdata/success_normalise_infotexts.md",
transformArgs: []string{"-t", "filename=title", "--quote-values=always"},
exitCode: 0,
output: `# Success: normalise info texts
The normalise command with ` + "`" + `-t filename=title` + "`" + ` transform argument should remove and ` + "`" + `no_value` + "`" + ` and ` + "`" + `key=value` + "`" + ` args and replace ` + "`" + `filename` + "`" + ` key with ` + "`" + `title` + "`" + `,
` + "```" + `sh title="true.sh"
exit 0
` + "```" + `
`,
},
} {
Expand All @@ -75,7 +88,7 @@ exit 0

var args []string
args = append(args, "normalise", "-o", dir)
args = append(args, test.transformsArgs...)
args = append(args, test.transformArgs...)
args = append(args, test.testPath)
rootCmd.SetArgs(args)

Expand Down
8 changes: 5 additions & 3 deletions cmd/normalise.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

var (
transforms []string
outputPath string
transforms []string
outputPath string
quoteValues string

normaliseCmd = &cobra.Command{
Aliases: []string{"normalize"},
Expand All @@ -21,6 +22,7 @@ var (
func init() {
rootCmd.AddCommand(normaliseCmd)
normaliseCmd.Flags().StringArrayVarP(&transforms, "transform", "t", nil, "transform info text key in `old=new` format, e.g., `-t filename=title` would transform `filename` info text to `title` info text")
normaliseCmd.Flags().StringVar(&quoteValues, "quote-values", "no", "`when` to quote info text values. `always` or `no`")
normaliseCmd.Flags().StringVarP(&outputPath, "output", "o", "", "`directory` where to save the normalised files")
_ = normaliseCmd.MarkFlagRequired("output")
normaliseCmd.RunE = func(cmd *cobra.Command, args []string) error {
Expand All @@ -31,6 +33,6 @@ func init() {
Transforms: transforms,
}

return utils.Normalize(args, params)
return utils.Normalize(args, params, quoteValues)
}
}
19 changes: 13 additions & 6 deletions utils/normalise.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type NormalizeParameters struct {
Transforms []string
}

func Normalize(rawPaths []string, params NormalizeParameters) error {
func Normalize(rawPaths []string, params NormalizeParameters, quoteValues string) error {
paths, warnings := ParseFilePaths(rawPaths, 1)

info, err := os.Stat(params.OutputPath)
Expand Down Expand Up @@ -55,7 +55,7 @@ func Normalize(rawPaths []string, params NormalizeParameters) error {
Message: fmt.Sprintf("Normalising %s", path),
Status: messages.MessageStatusStarted,
})
err := normalize(path, params.OutputPath, transformMap)
err := normalize(path, params.OutputPath, transformMap, quoteValues)
if err != nil {
_ = normLog.Push(messages.Update{
Key: path,
Expand All @@ -74,12 +74,19 @@ func Normalize(rawPaths []string, params NormalizeParameters) error {
return nil
}

func transformOptions(options, transforms map[string]string) string {
func quoteValue(value, quoteValues string) string {
if quoteValues == "always" {
return fmt.Sprintf(`"%s"`, value)
}
return value
}

func transformOptions(options, transforms map[string]string, quoteValues string) string {
output := ""
for key, value := range options {
if newKey := transforms[key]; newKey != "" {
if value != "" {
output += fmt.Sprintf(" %s=%s", newKey, value)
output += fmt.Sprintf(" %s=%s", newKey, quoteValue(value, quoteValues))
} else {
output += fmt.Sprintf(" %s", newKey)
}
Expand All @@ -89,7 +96,7 @@ func transformOptions(options, transforms map[string]string) string {
return output
}

func normalize(path, outputDir string, transforms map[string]string) error {
func normalize(path, outputDir string, transforms map[string]string, quoteValues string) error {
input, err := os.Open(path)
if err != nil {
return fmt.Errorf(`failed to open input file at "%s" (%w)`, path, err)
Expand All @@ -109,7 +116,7 @@ func normalize(path, outputDir string, transforms map[string]string) error {
info := line[3:]
if len(info) > 0 {
lang, options := ParseOptions(info)
line = fmt.Sprintf("```%s%s", lang, transformOptions(options, transforms))
line = fmt.Sprintf("```%s%s", lang, transformOptions(options, transforms, quoteValues))
}
}
_, err = output.WriteString(line + "\n")
Expand Down

0 comments on commit e7fddcc

Please sign in to comment.