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