@@ -3,43 +3,49 @@ package cli
3
3
import (
4
4
"fmt"
5
5
"io"
6
- "io/fs"
7
- "ladder/pkg/ruleset"
8
6
"os"
9
7
8
+ "ladder/pkg/ruleset"
9
+
10
10
"golang.org/x/term"
11
11
)
12
12
13
13
// HandleRulesetMerge merges a set of ruleset files, specified by the rulesetPath or RULESET env variable, into either YAML or Gzip format.
14
14
// Exits the program with an error message if the ruleset path is not provided or if loading the ruleset fails.
15
15
//
16
16
// Parameters:
17
- // - rulesetPath: A pointer to a string specifying the path to the ruleset file.
18
- // - mergeRulesets: A pointer to a boolean indicating if a merge operation should be performed.
19
- // - mergeRulesetsGzip: A pointer to a boolean indicating if the merge should be in Gzip format .
20
- // - mergeRulesetsOutput: A pointer to a string specifying the output file path . If empty, the output is printed to stdout .
17
+ // - rulesetPath: Specifies the path to the ruleset file.
18
+ // - mergeRulesets: Indicates if a merge operation should be performed.
19
+ // - useGzip: Indicates if the merged rulesets should be gzip-ped .
20
+ // - output: Specifies the output file. If nil, stdout will be used .
21
21
//
22
22
// Returns:
23
23
// - An error if the ruleset loading or merging process fails, otherwise nil.
24
- func HandleRulesetMerge (rulesetPath * string , mergeRulesets * bool , mergeRulesetsGzip * bool , mergeRulesetsOutput * string ) error {
25
- if * rulesetPath == "" {
26
- * rulesetPath = os . Getenv ( "RULESET" )
24
+ func HandleRulesetMerge (rulesetPath string , mergeRulesets bool , useGzip bool , output * os. File ) error {
25
+ if ! mergeRulesets {
26
+ return nil
27
27
}
28
- if * rulesetPath == "" {
29
- fmt .Println ("ERROR: no ruleset provided. Try again with --ruleset <ruleset.yaml>" )
28
+
29
+ if rulesetPath == "" {
30
+ rulesetPath = os .Getenv ("RULESET" )
31
+ }
32
+
33
+ if rulesetPath == "" {
34
+ fmt .Println ("error: no ruleset provided. Try again with --ruleset <ruleset.yaml>" )
30
35
os .Exit (1 )
31
36
}
32
37
33
- rs , err := ruleset .NewRuleset (* rulesetPath )
38
+ rs , err := ruleset .NewRuleset (rulesetPath )
34
39
if err != nil {
35
40
fmt .Println (err )
36
41
os .Exit (1 )
37
42
}
38
43
39
- if * mergeRulesetsGzip {
40
- return gzipMerge (rs , mergeRulesetsOutput )
44
+ if useGzip {
45
+ return gzipMerge (rs , output )
41
46
}
42
- return yamlMerge (rs , mergeRulesetsOutput )
47
+
48
+ return yamlMerge (rs , output )
43
49
}
44
50
45
51
// gzipMerge takes a RuleSet and an optional output file path pointer. It compresses the RuleSet into Gzip format.
@@ -48,33 +54,33 @@ func HandleRulesetMerge(rulesetPath *string, mergeRulesets *bool, mergeRulesetsG
48
54
//
49
55
// Parameters:
50
56
// - rs: The ruleset.RuleSet to be compressed.
51
- // - mergeRulesetsOutput: A pointer to a string specifying the output file path . If empty, the output is directed to stdout .
57
+ // - output: The output for the gzip data . If nil, stdout will be used .
52
58
//
53
59
// Returns:
54
60
// - An error if compression or file writing fails, otherwise nil.
55
- func gzipMerge (rs ruleset.RuleSet , mergeRulesetsOutput * string ) error {
61
+ func gzipMerge (rs ruleset.RuleSet , output io. Writer ) error {
56
62
gzip , err := rs .GzipYaml ()
57
63
if err != nil {
58
64
return err
59
65
}
60
66
61
- if * mergeRulesetsOutput != "" {
62
- out , err := os .Create (* mergeRulesetsOutput )
63
- defer out .Close ()
64
- _ , err = io .Copy (out , gzip )
67
+ if output != nil {
68
+ _ , err = io .Copy (output , gzip )
65
69
if err != nil {
66
70
return err
67
71
}
68
72
}
69
73
70
74
if term .IsTerminal (int (os .Stdout .Fd ())) {
71
- println ("WARNING : binary output can mess up your terminal. Use '--merge-rulesets-output <ruleset.gz>' or pipe it to a file." )
75
+ println ("warning : binary output can mess up your terminal. Use '--merge-rulesets-output <ruleset.gz>' or pipe it to a file." )
72
76
os .Exit (1 )
73
77
}
78
+
74
79
_ , err = io .Copy (os .Stdout , gzip )
75
80
if err != nil {
76
81
return err
77
82
}
83
+
78
84
return nil
79
85
}
80
86
@@ -83,23 +89,25 @@ func gzipMerge(rs ruleset.RuleSet, mergeRulesetsOutput *string) error {
83
89
//
84
90
// Parameters:
85
91
// - rs: The ruleset.RuleSet to be converted to YAML.
86
- // - mergeRulesetsOutput: A pointer to a string specifying the output file path . If empty, the output is printed to stdout .
92
+ // - output: The output for the merged data . If nil, stdout will be used .
87
93
//
88
94
// Returns:
89
95
// - An error if YAML conversion or file writing fails, otherwise nil.
90
- func yamlMerge (rs ruleset.RuleSet , mergeRulesetsOutput * string ) error {
96
+ func yamlMerge (rs ruleset.RuleSet , output io. Writer ) error {
91
97
yaml , err := rs .Yaml ()
92
98
if err != nil {
93
99
return err
94
100
}
95
- if * mergeRulesetsOutput == "" {
96
- fmt .Printf (yaml )
101
+
102
+ if output == nil {
103
+ fmt .Println (yaml )
97
104
os .Exit (0 )
98
105
}
99
106
100
- err = os . WriteFile ( * mergeRulesetsOutput , [] byte ( yaml ), fs . FileMode ( os . O_RDWR ) )
107
+ _ , err = io . WriteString ( output , yaml )
101
108
if err != nil {
102
- return fmt .Errorf ("ERROR: failed to write merged YAML ruleset to '%s' \n " , * mergeRulesetsOutput )
109
+ return fmt .Errorf ("failed to write merged YAML ruleset: %v " , err )
103
110
}
111
+
104
112
return nil
105
113
}
0 commit comments