From e67d03f6ffe7986b544bbc86b8472baa0cd3acda Mon Sep 17 00:00:00 2001 From: brittonhayes Date: Thu, 3 Oct 2024 14:48:05 -0700 Subject: [PATCH 1/3] feat: add support for formatting jsonnet with substation cli --- cmd/substation/fmt.go | 100 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 cmd/substation/fmt.go diff --git a/cmd/substation/fmt.go b/cmd/substation/fmt.go new file mode 100644 index 00000000..ae696b1f --- /dev/null +++ b/cmd/substation/fmt.go @@ -0,0 +1,100 @@ +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 Jsonnet files", + 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, _ := cmd.Flags().GetBool("write") + recursive, _ := cmd.Flags().GetBool("recursive") + + 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 +} From db1f3ae977ecfb1054e602f087c95489ae7dc9dd Mon Sep 17 00:00:00 2001 From: brittonhayes Date: Thu, 3 Oct 2024 17:01:13 -0700 Subject: [PATCH 2/3] fix: change short help message to match other commands style --- cmd/substation/fmt.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/substation/fmt.go b/cmd/substation/fmt.go index ae696b1f..f0d59fb4 100644 --- a/cmd/substation/fmt.go +++ b/cmd/substation/fmt.go @@ -17,7 +17,7 @@ func init() { var fmtCmd = &cobra.Command{ Use: "fmt [path]", - Short: "Format Jsonnet files", + 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. From 898d43ce4947f74b018f9986d5635d7510bbb646 Mon Sep 17 00:00:00 2001 From: brittonhayes Date: Thu, 3 Oct 2024 17:04:19 -0700 Subject: [PATCH 3/3] fix: added missing error handling for write and recursive flag checks --- cmd/substation/fmt.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cmd/substation/fmt.go b/cmd/substation/fmt.go index f0d59fb4..3bbf69a3 100644 --- a/cmd/substation/fmt.go +++ b/cmd/substation/fmt.go @@ -38,8 +38,15 @@ Supported file extensions: .jsonnet, .libsonnet`, path = args[0] } - write, _ := cmd.Flags().GetBool("write") - recursive, _ := cmd.Flags().GetBool("recursive") + 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) },