@@ -17,6 +17,7 @@ limitations under the License.
17
17
package cmd
18
18
19
19
import (
20
+ "errors"
20
21
"fmt"
21
22
"io"
22
23
"os"
@@ -27,7 +28,7 @@ import (
27
28
"strings"
28
29
29
30
"github.com/olekukonko/tablewriter"
30
- "github.com/pkg/errors"
31
+ pkgerrors "github.com/pkg/errors"
31
32
"github.com/spf13/cobra"
32
33
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
33
34
"k8s.io/utils/exec"
@@ -118,11 +119,11 @@ func runTopologyPlan() error {
118
119
for _ , f := range tp .files {
119
120
raw , err := os .ReadFile (f ) //nolint:gosec
120
121
if err != nil {
121
- return errors .Wrapf (err , "failed to read input file %q" , f )
122
+ return pkgerrors .Wrapf (err , "failed to read input file %q" , f )
122
123
}
123
124
objects , err := utilyaml .ToUnstructured (raw )
124
125
if err != nil {
125
- return errors .Wrapf (err , "failed to convert file %q to list of objects" , f )
126
+ return pkgerrors .Wrapf (err , "failed to convert file %q to list of objects" , f )
126
127
}
127
128
objs = append (objs , objects ... )
128
129
}
@@ -151,7 +152,7 @@ func printTopologyPlanOutput(out *cluster.TopologyPlanOutput, outdir string) err
151
152
} else {
152
153
printChangeSummary (out )
153
154
if err := writeOutputFiles (out , outdir ); err != nil {
154
- return errors .Wrap (err , "failed to write output files of target cluster changes" )
155
+ return pkgerrors .Wrap (err , "failed to write output files of target cluster changes" )
155
156
}
156
157
}
157
158
fmt .Printf ("\n " )
@@ -230,17 +231,17 @@ func writeOutputFiles(out *cluster.TopologyPlanOutput, outDir string) error {
230
231
// Write created files
231
232
createdDir := path .Join (outDir , "created" )
232
233
if err := os .MkdirAll (createdDir , 0750 ); err != nil {
233
- return errors .Wrapf (err , "failed to create %q directory" , createdDir )
234
+ return pkgerrors .Wrapf (err , "failed to create %q directory" , createdDir )
234
235
}
235
236
for _ , c := range out .Created {
236
237
yaml , err := utilyaml .FromUnstructured ([]unstructured.Unstructured {* c })
237
238
if err != nil {
238
- return errors .Wrap (err , "failed to convert object to yaml" )
239
+ return pkgerrors .Wrap (err , "failed to convert object to yaml" )
239
240
}
240
241
fileName := fmt .Sprintf ("%s_%s_%s.yaml" , c .GetKind (), c .GetNamespace (), c .GetName ())
241
242
filePath := path .Join (createdDir , fileName )
242
243
if err := os .WriteFile (filePath , yaml , 0600 ); err != nil {
243
- return errors .Wrapf (err , "failed to write yaml to file %q" , filePath )
244
+ return pkgerrors .Wrapf (err , "failed to write yaml to file %q" , filePath )
244
245
}
245
246
}
246
247
if len (out .Created ) != 0 {
@@ -250,44 +251,44 @@ func writeOutputFiles(out *cluster.TopologyPlanOutput, outDir string) error {
250
251
// Write modified files
251
252
modifiedDir := path .Join (outDir , "modified" )
252
253
if err := os .MkdirAll (modifiedDir , 0750 ); err != nil {
253
- return errors .Wrapf (err , "failed to create %q directory" , modifiedDir )
254
+ return pkgerrors .Wrapf (err , "failed to create %q directory" , modifiedDir )
254
255
}
255
256
for _ , m := range out .Modified {
256
257
// Write the modified object to file.
257
258
fileNameModified := fmt .Sprintf ("%s_%s_%s.modified.yaml" , m .After .GetKind (), m .After .GetNamespace (), m .After .GetName ())
258
259
filePathModified := path .Join (modifiedDir , fileNameModified )
259
260
if err := writeObjectToFile (filePathModified , m .After ); err != nil {
260
- return errors .Wrap (err , "failed to write modified object to file" )
261
+ return pkgerrors .Wrap (err , "failed to write modified object to file" )
261
262
}
262
263
263
264
// Write the original object to file.
264
265
fileNameOriginal := fmt .Sprintf ("%s_%s_%s.original.yaml" , m .Before .GetKind (), m .Before .GetNamespace (), m .Before .GetName ())
265
266
filePathOriginal := path .Join (modifiedDir , fileNameOriginal )
266
267
if err := writeObjectToFile (filePathOriginal , m .Before ); err != nil {
267
- return errors .Wrap (err , "failed to write original object to file" )
268
+ return pkgerrors .Wrap (err , "failed to write original object to file" )
268
269
}
269
270
270
271
// Calculate the jsonpatch and write to a file.
271
272
patch := crclient .MergeFrom (m .Before )
272
273
jsonPatch , err := patch .Data (m .After )
273
274
if err != nil {
274
- return errors .Wrapf (err , "failed to calculate jsonpatch of modified object %s/%s" , m .After .GetNamespace (), m .After .GetName ())
275
+ return pkgerrors .Wrapf (err , "failed to calculate jsonpatch of modified object %s/%s" , m .After .GetNamespace (), m .After .GetName ())
275
276
}
276
277
patchFileName := fmt .Sprintf ("%s_%s_%s.jsonpatch" , m .After .GetKind (), m .After .GetNamespace (), m .After .GetName ())
277
278
patchFilePath := path .Join (modifiedDir , patchFileName )
278
279
if err := os .WriteFile (patchFilePath , jsonPatch , 0600 ); err != nil {
279
- return errors .Wrapf (err , "failed to write jsonpatch to file %q" , patchFilePath )
280
+ return pkgerrors .Wrapf (err , "failed to write jsonpatch to file %q" , patchFilePath )
280
281
}
281
282
282
283
// Calculate the diff and write to a file.
283
284
diffFileName := fmt .Sprintf ("%s_%s_%s.diff" , m .After .GetKind (), m .After .GetNamespace (), m .After .GetName ())
284
285
diffFilePath := path .Join (modifiedDir , diffFileName )
285
286
diffFile , err := os .OpenFile (filepath .Clean (diffFilePath ), os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0600 )
286
287
if err != nil {
287
- return errors .Wrapf (err , "unable to open file %q" , diffFilePath )
288
+ return pkgerrors .Wrapf (err , "unable to open file %q" , diffFilePath )
288
289
}
289
290
if err := writeDiffToFile (filePathOriginal , filePathModified , diffFile ); err != nil {
290
- return errors .Wrapf (err , "failed to write diff to file %q" , diffFilePath )
291
+ return pkgerrors .Wrapf (err , "failed to write diff to file %q" , diffFilePath )
291
292
}
292
293
}
293
294
if len (out .Modified ) != 0 {
@@ -300,10 +301,10 @@ func writeOutputFiles(out *cluster.TopologyPlanOutput, outDir string) error {
300
301
func writeObjectToFile (filePath string , obj * unstructured.Unstructured ) error {
301
302
yaml , err := utilyaml .FromUnstructured ([]unstructured.Unstructured {* obj })
302
303
if err != nil {
303
- return errors .Wrap (err , "failed to convert object to yaml" )
304
+ return pkgerrors .Wrap (err , "failed to convert object to yaml" )
304
305
}
305
306
if err := os .WriteFile (filePath , yaml , 0600 ); err != nil {
306
- return errors .Wrapf (err , "failed to write yaml to file %q" , filePath )
307
+ return pkgerrors .Wrapf (err , "failed to write yaml to file %q" , filePath )
307
308
}
308
309
return nil
309
310
}
@@ -345,7 +346,7 @@ func writeDiffToFile(from, to string, out io.Writer) error {
345
346
cmd .SetStdout (out )
346
347
347
348
if err := cmd .Run (); err != nil && ! isDiffError (err ) {
348
- return errors .Wrapf (err , "failed to run %q" , diff )
349
+ return pkgerrors .Wrapf (err , "failed to run %q" , diff )
349
350
}
350
351
return nil
351
352
}
@@ -379,7 +380,8 @@ func getDiffCommand(args ...string) (string, exec.Cmd) {
379
380
// This makes use of the exit code of diff programs which is 0 for no diff, 1 for
380
381
// modified and 2 for other errors.
381
382
func isDiffError (err error ) bool {
382
- if err , ok := err .(exec.ExitError ); ok && err .ExitStatus () <= 1 {
383
+ var exitErr exec.ExitError
384
+ if errors .As (err , & exitErr ) && exitErr .ExitStatus () <= 1 {
383
385
return true
384
386
}
385
387
return false
0 commit comments