Skip to content

Commit ca44dca

Browse files
committed
use opid or name value as prefix
1 parent a5eff08 commit ca44dca

File tree

2 files changed

+18
-15
lines changed

2 files changed

+18
-15
lines changed

cmd/pbm/diagnostic.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func handleDiagnostic(
3232
return nil, errors.New("--opid or --name must be provided")
3333
}
3434

35+
prefix := opts.opid
3536
if opts.opid == "" {
3637
cid, err := sdk.FindCommandIDByName(ctx, pbm, opts.name)
3738
if err != nil {
@@ -46,6 +47,7 @@ func handleDiagnostic(
4647
return nil, errors.Wrap(err, "find opid by name")
4748
}
4849
opts.opid = string(cid)
50+
prefix = opts.name
4951
}
5052

5153
report, err := sdk.Diagnostic(ctx, pbm, sdk.CommandID(opts.opid))
@@ -65,10 +67,10 @@ func handleDiagnostic(
6567
return nil, errors.Errorf("%s is not a dir", opts.path)
6668
}
6769

68-
err = writeToFile(opts.path, opts.opid+".report.json", report)
70+
err = writeToFile(opts.path, prefix+".report.json", report)
6971
if err != nil {
7072
return nil, errors.Wrapf(err,
71-
"failed to save %s", filepath.Join(opts.path, opts.opid+".report.json"))
73+
"failed to save %s", filepath.Join(opts.path, prefix+".report.json"))
7274
}
7375

7476
switch report.Command.Cmd {
@@ -80,10 +82,10 @@ func handleDiagnostic(
8082
}
8183
} else {
8284
meta.Store = backup.Storage{}
83-
err = writeToFile(opts.path, opts.opid+".backup.json", meta)
85+
err = writeToFile(opts.path, prefix+".backup.json", meta)
8486
if err != nil {
8587
return nil, errors.Wrapf(err,
86-
"failed to save %s", filepath.Join(opts.path, opts.opid+".backup.json"))
88+
"failed to save %s", filepath.Join(opts.path, prefix+".backup.json"))
8789
}
8890
}
8991
case sdk.CmdRestore:
@@ -93,21 +95,21 @@ func handleDiagnostic(
9395
return nil, errors.Wrap(err, "get restore meta")
9496
}
9597
} else {
96-
err = writeToFile(opts.path, opts.opid+".restore.json", meta)
98+
err = writeToFile(opts.path, prefix+".restore.json", meta)
9799
if err != nil {
98100
return nil, errors.Wrapf(err,
99-
"failed to save %s", filepath.Join(opts.path, opts.opid+".restore.json"))
101+
"failed to save %s", filepath.Join(opts.path, prefix+".restore.json"))
100102
}
101103
}
102104
}
103105

104-
err = writeLogToFile(ctx, pbm, opts)
106+
err = writeLogToFile(ctx, pbm, opts.path, prefix, opts.opid)
105107
if err != nil {
106108
return nil, errors.Wrap(err, "failed to save command log")
107109
}
108110

109111
if opts.archive {
110-
err = createArchive(ctx, opts)
112+
err = createArchive(opts.path, prefix)
111113
if err != nil {
112114
return nil, errors.Wrap(err, "create archive")
113115
}
@@ -117,8 +119,8 @@ func handleDiagnostic(
117119
}
118120

119121
//nolint:nonamedreturns
120-
func writeLogToFile(ctx context.Context, pbm *sdk.Client, opts diagnosticOptions) (err error) {
121-
filename := filepath.Join(opts.path, opts.opid+".log")
122+
func writeLogToFile(ctx context.Context, pbm *sdk.Client, path, prefix, opid string) (err error) {
123+
filename := filepath.Join(path, prefix+".log")
122124
file, err := os.Create(filename)
123125
if err != nil {
124126
return err
@@ -130,7 +132,7 @@ func writeLogToFile(ctx context.Context, pbm *sdk.Client, opts diagnosticOptions
130132
}
131133
}()
132134

133-
cur, err := sdk.CommandLogCursor(ctx, pbm, sdk.CommandID(opts.opid))
135+
cur, err := sdk.CommandLogCursor(ctx, pbm, sdk.CommandID(opid))
134136
if err != nil {
135137
return errors.Wrap(err, "open log cursor")
136138
}
@@ -201,7 +203,7 @@ func writeToFile(dirname, name string, val any) error {
201203
return nil
202204
}
203205

204-
func createArchive(_ context.Context, opts diagnosticOptions) error {
206+
func createArchive(path, prefix string) error {
205207
file, err := os.CreateTemp("", "")
206208
if err != nil {
207209
return errors.Wrap(err, "create tmp file")
@@ -214,7 +216,7 @@ func createArchive(_ context.Context, opts diagnosticOptions) error {
214216
}()
215217

216218
archive := zip.NewWriter(file)
217-
err = archive.AddFS(os.DirFS(opts.path))
219+
err = archive.AddFS(os.DirFS(path))
218220
if err != nil {
219221
return err
220222
}
@@ -229,7 +231,7 @@ func createArchive(_ context.Context, opts diagnosticOptions) error {
229231
return errors.Wrap(err, "close file")
230232
}
231233

232-
err = os.Rename(file.Name(), filepath.Join(opts.path, opts.opid+".zip"))
234+
err = os.Rename(file.Name(), filepath.Join(path, prefix+".zip"))
233235
if err != nil {
234236
return err
235237
}

sdk/util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func FindCommandIDByName(ctx context.Context, c *Client, name string) (CommandID
112112
}
113113

114114
type DiagnosticReport struct {
115+
OPID string `json:"opid" bson:"opid"`
115116
ClusterTime primitive.Timestamp `json:"cluster_time" bson:"cluster_time"`
116117
ServerVersion string `json:"server_version" bson:"server_version"`
117118
FCV string `json:"fcv" bson:"fcv"`
@@ -124,7 +125,7 @@ type DiagnosticReport struct {
124125

125126
func Diagnostic(ctx context.Context, c *Client, cid CommandID) (*DiagnosticReport, error) {
126127
var err error
127-
rv := &DiagnosticReport{}
128+
rv := &DiagnosticReport{OPID: string(cid)}
128129

129130
rv.ClusterTime, err = topo.GetClusterTime(ctx, c.conn)
130131
if err != nil {

0 commit comments

Comments
 (0)