-
Notifications
You must be signed in to change notification settings - Fork 1
/
format.go
47 lines (38 loc) · 971 Bytes
/
format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package utils
import (
"fmt"
"go/format"
"os"
"os/exec"
)
var (
terraformCommand func(string) error = func(folder string) error {
_, err := exec.Command("terraform", "fmt", folder).Output()
return err
}
osReadFile = os.ReadFile
formatSource = format.Source
osWriteFile = os.WriteFile
)
func TerraformFormat(folder string) error {
err := terraformCommand(folder)
if err != nil {
return fmt.Errorf("please consider to install terraform. Terraform format fails: %w", err)
}
return nil
}
func GoFormat(filename string) error {
inputFileContent, err := osReadFile(filename)
if err != nil {
return fmt.Errorf("error opening input file: %w", err)
}
formattedContent, err := formatSource(inputFileContent)
if err != nil {
return fmt.Errorf("error formatting source code: %w", err)
}
err = osWriteFile(filename, formattedContent, os.ModePerm)
if err != nil {
return fmt.Errorf("error writing to output file: %w", err)
}
return nil
}