-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for formatting jsonnet with substation cli (#250)
- Loading branch information
1 parent
7e9fe1e
commit 6cae3cd
Showing
1 changed file
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/google/go-jsonnet/formatter" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(fmtCmd) | ||
fmtCmd.PersistentFlags().BoolP("write", "w", false, "write result to (source) file instead of stdout") | ||
fmtCmd.PersistentFlags().BoolP("recursive", "R", false, "recursively format all files") | ||
} | ||
|
||
var fmtCmd = &cobra.Command{ | ||
Use: "fmt [path]", | ||
Short: "format configs", | ||
Long: `'substation fmt' formats Jsonnet files. | ||
It prints the formatted output to stdout by default. | ||
Use the --write flag to update the files in-place. | ||
The command can format a single file or a directory. | ||
Use the --recursive flag to format all files in a directory and its subdirectories. | ||
Supported file extensions: .jsonnet, .libsonnet`, | ||
Example: ` substation fmt config.jsonnet | ||
substation fmt -w config.jsonnet | ||
substation fmt -R /path/to/configs | ||
substation fmt -w -R /path/to/configs`, | ||
Args: cobra.MaximumNArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
// Default to current directory if no path is provided | ||
path := "." | ||
if len(args) > 0 { | ||
path = args[0] | ||
} | ||
|
||
write, err := cmd.Flags().GetBool("write") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
recursive, err := cmd.Flags().GetBool("recursive") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return formatPath(path, write, recursive) | ||
}, | ||
} | ||
|
||
func formatPath(path string, write, recursive bool) error { | ||
fi, err := os.Stat(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !fi.IsDir() { | ||
return formatFile(path, write) | ||
} | ||
|
||
return filepath.Walk(path, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
if !recursive && path != "." { | ||
return filepath.SkipDir | ||
} | ||
return nil | ||
} | ||
|
||
ext := filepath.Ext(path) | ||
if ext != ".jsonnet" && ext != ".libsonnet" { | ||
return nil | ||
} | ||
return formatFile(path, write) | ||
}) | ||
} | ||
|
||
func formatFile(path string, write bool) error { | ||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
formatted, err := formatter.Format(path, string(content), formatter.DefaultOptions()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if write { | ||
err = os.WriteFile(path, []byte(formatted), 0o644) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(path) | ||
} else { | ||
fmt.Println(formatted) | ||
} | ||
|
||
return nil | ||
} |