Skip to content

Commit 5a1f61d

Browse files
authored
Merge pull request #341 from jumppad-labs/erik/fmt
Add `fmt` command to format config
2 parents 073162b + 0c2a298 commit 5a1f61d

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@
5959
"${input:blueprint}",
6060
],
6161
},
62+
{
63+
"name": "Debug - Format",
64+
"type": "go",
65+
"request": "launch",
66+
"mode": "debug",
67+
"program": "${workspaceFolder}",
68+
"env": {
69+
"LOG_LEVEL": "debug"
70+
},
71+
"args": [
72+
"fmt",
73+
"${input:blueprint}",
74+
],
75+
},
6276
{
6377
"name": "Debug - Up",
6478
"type": "go",

cmd/format.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
"github.com/hashicorp/hcl/v2"
10+
"github.com/hashicorp/hcl/v2/hclwrite"
11+
"github.com/jumppad-labs/jumppad/pkg/utils"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
func newFormatCmd() *cobra.Command {
16+
formatCmd := &cobra.Command{
17+
Use: "fmt [file] | [directory]",
18+
Short: "fmt the configuration at the given path",
19+
Long: `fmt the configuration at the given path`,
20+
Example: `
21+
# fmt configuration in .hcl files in the current folder
22+
jumppad fmt
23+
24+
# format configuration in a specific file
25+
jumppad fmt my-stack/network.hcl
26+
27+
# format configuration in a specific directory
28+
jumppad fmt ./my-stack
29+
`,
30+
Args: cobra.ArbitraryArgs,
31+
RunE: newFormatCmdFunc(),
32+
SilenceUsage: true,
33+
}
34+
35+
return formatCmd
36+
}
37+
38+
func newFormatCmdFunc() func(cmd *cobra.Command, args []string) error {
39+
return func(cmd *cobra.Command, args []string) error {
40+
dst := ""
41+
if len(args) == 1 {
42+
dst = args[0]
43+
} else {
44+
dst = "./"
45+
}
46+
47+
if dst == "." {
48+
dst = "./"
49+
}
50+
51+
if dst != "" {
52+
if utils.IsHCLFile(dst) {
53+
err := format(dst)
54+
if err != nil {
55+
return err
56+
}
57+
} else if utils.IsLocalFolder(dst) {
58+
err := filepath.Walk(dst, func(path string, info os.FileInfo, err error) error {
59+
if err != nil {
60+
return err
61+
}
62+
63+
if !info.IsDir() && strings.HasSuffix(path, ".hcl") {
64+
err := format(path)
65+
if err != nil {
66+
return err
67+
}
68+
}
69+
70+
return nil
71+
})
72+
if err != nil {
73+
return err
74+
}
75+
} else {
76+
return fmt.Errorf("error: can only format local files and directories")
77+
}
78+
}
79+
80+
return nil
81+
}
82+
}
83+
84+
func format(path string) error {
85+
data, err := os.ReadFile(path)
86+
if err != nil {
87+
return err
88+
}
89+
90+
file, diags := hclwrite.ParseConfig(data, path, hcl.InitialPos)
91+
if diags.HasErrors() {
92+
return fmt.Errorf("errors: %v", diags)
93+
}
94+
95+
err = os.WriteFile(path, file.Bytes(), 0644)
96+
if err != nil {
97+
return err
98+
}
99+
100+
return nil
101+
}

cmd/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ func Execute(v, c, d string) error {
8989
// add the validate command
9090
rootCmd.AddCommand(newValidateCmd(engine, engineClients.Getter))
9191

92+
// add the fmt command
93+
rootCmd.AddCommand(newFormatCmd())
94+
9295
rootCmd.SilenceErrors = true
9396

9497
// set a pre run function to show the changelog

0 commit comments

Comments
 (0)