Skip to content

Commit a5eff08

Browse files
committed
add --archive option to diagnostic command
1 parent cb16a2e commit a5eff08

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

cmd/pbm/diagnostic.go

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"archive/zip"
45
"context"
56
"fmt"
67
"io"
@@ -16,9 +17,10 @@ import (
1617
)
1718

1819
type diagnosticOptions struct {
19-
path string
20-
opid string
21-
name string
20+
path string
21+
opid string
22+
name string
23+
archive bool
2224
}
2325

2426
func handleDiagnostic(
@@ -104,6 +106,13 @@ func handleDiagnostic(
104106
return nil, errors.Wrap(err, "failed to save command log")
105107
}
106108

109+
if opts.archive {
110+
err = createArchive(ctx, opts)
111+
if err != nil {
112+
return nil, errors.Wrap(err, "create archive")
113+
}
114+
}
115+
107116
return outMsg{"Report is successfully created"}, nil
108117
}
109118

@@ -191,3 +200,40 @@ func writeToFile(dirname, name string, val any) error {
191200

192201
return nil
193202
}
203+
204+
func createArchive(_ context.Context, opts diagnosticOptions) error {
205+
file, err := os.CreateTemp("", "")
206+
if err != nil {
207+
return errors.Wrap(err, "create tmp file")
208+
}
209+
defer func() {
210+
if file != nil {
211+
file.Close()
212+
os.Remove(file.Name())
213+
}
214+
}()
215+
216+
archive := zip.NewWriter(file)
217+
err = archive.AddFS(os.DirFS(opts.path))
218+
if err != nil {
219+
return err
220+
}
221+
222+
err = archive.Close()
223+
if err != nil {
224+
return errors.Wrap(err, "close zip")
225+
}
226+
227+
err = file.Close()
228+
if err != nil {
229+
return errors.Wrap(err, "close file")
230+
}
231+
232+
err = os.Rename(file.Name(), filepath.Join(opts.path, opts.opid+".zip"))
233+
if err != nil {
234+
return err
235+
}
236+
237+
file = nil
238+
return nil
239+
}

cmd/pbm/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ func main() {
445445
diagnosticCmd.Flag("path", "Path where files will be saved").
446446
Required().
447447
StringVar(&diagnosticOpts.path)
448+
diagnosticCmd.Flag("archive", "create zip file").
449+
BoolVar(&diagnosticOpts.archive)
448450
diagnosticCmd.Flag("opid", "OPID/Command ID").
449451
StringVar(&diagnosticOpts.opid)
450452
diagnosticCmd.Flag("name", "Backup or Restore name").

0 commit comments

Comments
 (0)