@@ -15,136 +15,156 @@ import (
15
15
"github.com/spf13/cobra"
16
16
)
17
17
18
+ var (
19
+ chartOnly bool
20
+ )
21
+
18
22
func ChartDebugSubCommand (base64ChartTarball string ) * cobra.Command {
19
23
chartDebug := & cobra.Command {
20
24
Use : "debug-chart" ,
21
25
Short : "This command helps debug the internal chart files" ,
22
- Run : func (cmd * cobra.Command , _ []string ) {
23
- chartOnly , _ := cmd .Flags ().GetBool ("chart-only" )
26
+ RunE : func (_ * cobra.Command , _ []string ) error {
24
27
if chartOnly {
25
28
logrus .Info ("Only the embedded chart's `Chart.yaml` will be exported." )
26
29
} else {
27
30
logrus .Info ("The entire embedded chart wil be exported." )
28
31
}
29
32
30
- chartTarballData , err := base64 . StdEncoding . DecodeString (base64ChartTarball )
33
+ tarReader , err := readEmbeddedHelmChart (base64ChartTarball )
31
34
if err != nil {
32
- fmt .Println ("error:" , err )
33
- return
35
+ return err
34
36
}
35
37
36
- gzipReader , err := gzip .NewReader (bytes .NewReader (chartTarballData ))
37
- if err != nil {
38
- fmt .Println ("Error creating gzip reader:" , err )
39
- return
40
- }
41
- defer gzipReader .Close ()
42
-
43
- // Create a tar reader
44
- tarReader := tar .NewReader (gzipReader )
45
-
46
38
// Extract files to the current working directory
47
39
cwd , err := os .Getwd ()
48
40
if err != nil {
49
- fmt .Println ("Error getting current working directory:" , err )
50
- return
41
+ return fmt .Errorf ("error getting current working directory: %v" , err )
51
42
}
52
43
debugDir := filepath .Join (cwd , ".debug" )
53
44
// Ensure the .debug directory exists
54
45
if err := os .MkdirAll (debugDir , 0755 ); err != nil {
55
- fmt .Println ("Error creating .debug directory:" , err )
56
- return
46
+ return fmt .Errorf ("error creating .debug directory: %v" , err )
57
47
}
58
48
59
- // Extract files to the .debug directory
60
- for {
61
- header , err := tarReader .Next ()
62
- if err == io .EOF {
63
- // End of archive
64
- break
65
- }
66
- if err != nil {
67
- fmt .Println ("Error reading tarball:" , err )
68
- return
69
- }
70
-
71
- // Determine the path to extract the file to
72
- filePath := filepath .Join (debugDir , header .Name )
73
-
74
- if chartOnly {
75
- switch header .Typeflag {
76
- case tar .TypeReg :
77
- if header .Name == "rancher-project-monitoring/Chart.yaml" {
78
- logrus .Info ("Found a `Chart.yaml` file to export." )
79
- // Ensure the parent directory exists
80
- parentDir := filepath .Dir (filePath )
81
- if err := os .MkdirAll (parentDir , 0755 ); err != nil {
82
- logrus .Error ("Error creating parent directory:" , err )
83
- return
84
- }
85
-
86
- // Create regular file
87
- outFile , err := os .Create (filePath )
88
- if err != nil {
89
- logrus .Error ("Error creating file:" , err )
90
- return
91
- }
92
-
93
- // Copy file content
94
- if _ , err := io .Copy (outFile , tarReader ); err != nil {
95
- logrus .Error ("Error writing file content:" , err )
96
- outFile .Close ()
97
- return
98
- }
99
- logrus .Info ("The `Chart.yaml` file was exported" )
100
- outFile .Close ()
101
- } else {
102
- logrus .Debugf ("Skipping file: %s\n " , header .Name )
103
- }
104
- default :
105
- logrus .Debugf ("Skipping file: %s\n " , header .Name )
106
- }
107
-
108
- return
109
- }
110
-
111
- switch header .Typeflag {
112
- case tar .TypeDir :
113
- // Create directory
114
- if err := os .MkdirAll (filePath , os .FileMode (header .Mode )); err != nil {
115
- fmt .Println ("Error creating directory:" , err )
116
- return
117
- }
118
- case tar .TypeReg :
119
- // Ensure the parent directory exists
120
- parentDir := filepath .Dir (filePath )
121
- if err := os .MkdirAll (parentDir , 0755 ); err != nil {
122
- fmt .Println ("Error creating parent directory:" , err )
123
- return
124
- }
125
-
126
- // Create regular file
127
- outFile , err := os .Create (filePath )
128
- if err != nil {
129
- fmt .Println ("Error creating file:" , err )
130
- return
131
- }
132
-
133
- // Copy file content
134
- if _ , err := io .Copy (outFile , tarReader ); err != nil {
135
- fmt .Println ("Error writing file content:" , err )
136
- outFile .Close ()
137
- return
138
- }
139
- outFile .Close ()
140
- default :
141
- fmt .Printf ("Skipping unsupported file type: %s\n " , header .Name )
142
- }
49
+ err = extractChartData (tarReader , debugDir , chartOnly )
50
+ if err != nil {
51
+ return err
143
52
}
144
53
145
- fmt .Println ("Chart files successfully extracted to" , debugDir )
54
+ logrus .Infof ("Chart files successfully extracted to: %s" , debugDir )
55
+ return nil
146
56
},
147
57
}
148
- chartDebug .PersistentFlags ().BoolP ( "chart-only" , "C" , false , "When set, only the `Chart.yaml` will be exported." )
58
+ chartDebug .Flags ().BoolVarP ( & chartOnly , "chart-only" , "C" , false , "When set, only the `Chart.yaml` will be exported." )
149
59
return chartDebug
150
60
}
61
+
62
+ func readEmbeddedHelmChart (base64ChartTarball string ) (* tar.Reader , error ) {
63
+ chartTarballData , err := base64 .StdEncoding .DecodeString (base64ChartTarball )
64
+ if err != nil {
65
+ return nil , fmt .Errorf ("error reading embedded chart data from base64: %v" , err )
66
+ }
67
+
68
+ gzipReader , err := gzip .NewReader (bytes .NewReader (chartTarballData ))
69
+ if err != nil {
70
+ return nil , fmt .Errorf ("error creating gzip reader: %v" , err )
71
+ }
72
+ defer gzipReader .Close ()
73
+
74
+ // Create a tar reader
75
+ tarReader := tar .NewReader (gzipReader )
76
+ return tarReader , nil
77
+ }
78
+
79
+ func extractChartData (tarReader * tar.Reader , debugDir string , chartOnly bool ) error {
80
+ // Extract files to the .debug directory
81
+ for {
82
+ header , err := tarReader .Next ()
83
+ if err == io .EOF {
84
+ // End of archive
85
+ break
86
+ }
87
+ if err != nil {
88
+ return fmt .Errorf ("error reading tarball: %v" , err )
89
+ }
90
+
91
+ // Determine the path to extract the file to
92
+ filePath := filepath .Join (debugDir , header .Name )
93
+
94
+ if chartOnly {
95
+ err = extractChartYamlFile (tarReader , header , filePath )
96
+ } else {
97
+ err = extractAllChartData (tarReader , header , filePath )
98
+ }
99
+
100
+ if err != nil {
101
+ return err
102
+ }
103
+ }
104
+ return nil
105
+ }
106
+
107
+ func extractChartYamlFile (tarReader * tar.Reader , header * tar.Header , filePath string ) error {
108
+ switch header .Typeflag {
109
+ case tar .TypeReg :
110
+ if header .Name == "rancher-project-monitoring/Chart.yaml" {
111
+ logrus .Info ("Found a `Chart.yaml` file to export." )
112
+ // Ensure the parent directory exists
113
+ parentDir := filepath .Dir (filePath )
114
+ if err := os .MkdirAll (parentDir , 0755 ); err != nil {
115
+ return fmt .Errorf ("error creating parent directory: %v" , err )
116
+ }
117
+
118
+ // Create regular file
119
+ outFile , err := os .Create (filePath )
120
+ if err != nil {
121
+ return fmt .Errorf ("error creating file: %v" , err )
122
+ }
123
+
124
+ // Copy file content
125
+ if _ , err := io .Copy (outFile , tarReader ); err != nil {
126
+ outFile .Close ()
127
+ return fmt .Errorf ("error writing file content: %v" , err )
128
+ }
129
+ logrus .Info ("The `Chart.yaml` file was exported" )
130
+ outFile .Close ()
131
+ } else {
132
+ logrus .Debugf ("Skipping file: %s\n " , header .Name )
133
+ }
134
+ default :
135
+ logrus .Debugf ("Skipping file: %s\n " , header .Name )
136
+ }
137
+ return nil
138
+ }
139
+
140
+ func extractAllChartData (tarReader * tar.Reader , header * tar.Header , filePath string ) error {
141
+ switch header .Typeflag {
142
+ case tar .TypeDir :
143
+ // Create directory
144
+ if err := os .MkdirAll (filePath , os .FileMode (header .Mode )); err != nil {
145
+ return fmt .Errorf ("error creating directory: %v" , err )
146
+ }
147
+ case tar .TypeReg :
148
+ // Ensure the parent directory exists
149
+ parentDir := filepath .Dir (filePath )
150
+ if err := os .MkdirAll (parentDir , 0755 ); err != nil {
151
+ return fmt .Errorf ("error creating parent directory: %v" , err )
152
+ }
153
+
154
+ // Create regular file
155
+ outFile , err := os .Create (filePath )
156
+ if err != nil {
157
+ return fmt .Errorf ("error creating file: %v" , err )
158
+ }
159
+
160
+ // Copy file content
161
+ if _ , err := io .Copy (outFile , tarReader ); err != nil {
162
+ outFile .Close ()
163
+ return fmt .Errorf ("error writing file content: %v" , err )
164
+ }
165
+ outFile .Close ()
166
+ default :
167
+ logrus .Debugf ("Skipping unsupported file type: %s\n " , header .Name )
168
+ }
169
+ return nil
170
+ }
0 commit comments