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 f728a57
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 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"},
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
12 changes: 9 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 bool

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().BoolVar(&quoteValues, "quote-values", false, "wrap info text values in double quotes")
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,10 @@ func init() {
Transforms: transforms,
}

return utils.Normalize(args, params)
quote := ""
if quoteValues {
quote = `"`
}
return utils.Normalize(args, params, quote)
}
}
13 changes: 8 additions & 5 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, quote 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, quote)
if err != nil {
_ = normLog.Push(messages.Update{
Key: path,
Expand All @@ -74,11 +74,14 @@ func Normalize(rawPaths []string, params NormalizeParameters) error {
return nil
}

func transformOptions(options, transforms map[string]string) string {
func transformOptions(options, transforms map[string]string, quote string) string {
output := ""
for key, value := range options {
if newKey := transforms[key]; newKey != "" {
if value != "" {
if quote != "" {
value = fmt.Sprintf("%s%s%s", quote, value, quote)
}
output += fmt.Sprintf(" %s=%s", newKey, value)
} else {
output += fmt.Sprintf(" %s", newKey)
Expand All @@ -89,7 +92,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, quote 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 +112,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, quote))
}
}
_, err = output.WriteString(line + "\n")
Expand Down

0 comments on commit f728a57

Please sign in to comment.