Expand FormatOutput to accept optional paths for format inference
This commit is contained in:
+2
-15
@@ -45,15 +45,10 @@ See https://bkl.gopatchy.io/#bkld for detailed documentation.`
|
||||
}
|
||||
|
||||
format := ""
|
||||
|
||||
if opts.OutputFormat != nil {
|
||||
format = *opts.OutputFormat
|
||||
}
|
||||
|
||||
if format == "" && opts.OutputPath != nil {
|
||||
format = bkl.Ext(string(*opts.OutputPath))
|
||||
}
|
||||
|
||||
// Prepare paths from current working directory
|
||||
paths := []string{string(opts.Positional.BasePath), string(opts.Positional.TargetPath)}
|
||||
preparedPaths, err := bkl.PreparePathsFromCwd(paths, "/")
|
||||
@@ -68,16 +63,8 @@ See https://bkl.gopatchy.io/#bkld for detailed documentation.`
|
||||
fatal(err)
|
||||
}
|
||||
|
||||
// Get format from first file if not specified
|
||||
if format == "" {
|
||||
_, f, err := bkl.FileMatch(fsys, preparedPaths[0])
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
format = f
|
||||
}
|
||||
|
||||
enc, err := bkl.FormatOutput(doc, format)
|
||||
// Pass output path and input path - FormatOutput will use their extensions if format is empty
|
||||
enc, err := bkl.FormatOutput(doc, format, (*string)(opts.OutputPath), &preparedPaths[0])
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
|
||||
+2
-15
@@ -44,15 +44,10 @@ See https://bkl.gopatchy.io/#bkli for detailed documentation.`
|
||||
}
|
||||
|
||||
format := ""
|
||||
|
||||
if opts.OutputFormat != nil {
|
||||
format = *opts.OutputFormat
|
||||
}
|
||||
|
||||
if format == "" && opts.OutputPath != nil {
|
||||
format = bkl.Ext(string(*opts.OutputPath))
|
||||
}
|
||||
|
||||
// Convert paths to strings
|
||||
paths := make([]string, len(opts.Positional.InputPaths))
|
||||
for i, path := range opts.Positional.InputPaths {
|
||||
@@ -72,16 +67,8 @@ See https://bkl.gopatchy.io/#bkli for detailed documentation.`
|
||||
fatal(err)
|
||||
}
|
||||
|
||||
// Get format from first file if not specified
|
||||
if format == "" {
|
||||
_, f, err := bkl.FileMatch(fsys, preparedPaths[0])
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
format = f
|
||||
}
|
||||
|
||||
enc, err := bkl.FormatOutput(doc, format)
|
||||
// Pass output path and input path - FormatOutput will use their extensions if format is empty
|
||||
enc, err := bkl.FormatOutput(doc, format, (*string)(opts.OutputPath), &preparedPaths[0])
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
|
||||
+2
-13
@@ -58,23 +58,12 @@ See https://bkl.gopatchy.io/#bklr for detailed documentation.`
|
||||
|
||||
// Get format from file if not specified
|
||||
format := ""
|
||||
if opts.OutputPath != nil {
|
||||
format = bkl.Ext(string(*opts.OutputPath))
|
||||
}
|
||||
|
||||
if opts.OutputFormat != nil {
|
||||
format = *opts.OutputFormat
|
||||
}
|
||||
|
||||
if format == "" {
|
||||
_, f, err := bkl.FileMatch(fsys, preparedPaths[0])
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
format = f
|
||||
}
|
||||
|
||||
enc, err := bkl.FormatOutput(out, format)
|
||||
// Pass output path and input path - FormatOutput will use their extensions if format is empty
|
||||
enc, err := bkl.FormatOutput(out, format, (*string)(opts.OutputPath), &preparedPaths[0])
|
||||
if err != nil {
|
||||
fatal(err)
|
||||
}
|
||||
|
||||
@@ -351,8 +351,20 @@ func getOSEnv() map[string]string {
|
||||
}
|
||||
|
||||
// FormatOutput marshals the given data to the specified format.
|
||||
// If format is empty, it looks at the provided paths (as string pointers) and uses
|
||||
// the file extension of the first non-nil path as the format.
|
||||
// Returns the marshaled bytes or an error if the format is unknown or marshaling fails.
|
||||
func FormatOutput(data any, format string) ([]byte, error) {
|
||||
func FormatOutput(data any, format string, paths ...*string) ([]byte, error) {
|
||||
// If format is empty, try to infer from paths
|
||||
if format == "" {
|
||||
for _, path := range paths {
|
||||
if path != nil && *path != "" {
|
||||
format = Ext(*path)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
f, found := formatByExtension[format]
|
||||
if !found {
|
||||
return nil, fmt.Errorf("%s: %w", format, ErrUnknownFormat)
|
||||
|
||||
Reference in New Issue
Block a user