Skip to content

Commit a6b8e22

Browse files
committed
Support export size detail message
With this update, the support export command now reports the resulting archive file's size along with usage instructions. In cases where the file is larger than 25MiB, an additional note is displayed with further instructions due to common email attachment size limits. Issue: [sc-18005]
1 parent 2fc94f4 commit a6b8e22

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

internal/cmd/export.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ import (
5050
"github.com/crunchydata/postgres-operator-client/internal/util"
5151
)
5252

53+
const (
54+
// define one mebibyte in float64
55+
// - https://mathworld.wolfram.com/Mebibyte.html
56+
mebibyte float64 = (1 << 20)
57+
58+
// Default support export message
59+
msg1 = "\n" + `Archive file size: %.2f MiB
60+
Email the support export archive to [email protected]
61+
or attach as a email reply to your existing Support Ticket` + "\n"
62+
63+
// Additional support export message. Shown when size is greater than 25 MiB.
64+
msg2 = `Archive file (%.2f MiB) may be too big to email.
65+
Please request file share link by emailing [email protected]` + "\n"
66+
)
67+
5368
var namespacedResources = []schema.GroupVersionResource{{
5469
Group: appsv1.SchemeGroupVersion.Group,
5570
Version: appsv1.SchemeGroupVersion.Version,
@@ -313,12 +328,35 @@ kubectl pgo support export daisy --monitoring-namespace another-namespace --outp
313328
return logErr
314329
}
315330

331+
// Print final message
332+
if err == nil {
333+
info, err := os.Stat(outputDir + "/" + outputFile)
334+
335+
if err == nil {
336+
fmt.Print(exportSizeReport(float64(info.Size())))
337+
}
338+
}
339+
316340
return err
317341
}
318342

319343
return cmd
320344
}
321345

346+
// exportSizeReport defines the message displayed when a support export archive
347+
// is created. If the size of the archive file is greater than 25MiB, an additional
348+
// message is displayed.
349+
func exportSizeReport(size float64) string {
350+
finalMsg := fmt.Sprintf(msg1, size/mebibyte)
351+
352+
// if file size is > 25 MiB, print additional message
353+
if size > mebibyte*25 {
354+
finalMsg = finalMsg + fmt.Sprintf(msg2, size/mebibyte)
355+
}
356+
357+
return finalMsg
358+
}
359+
322360
// gatherKubeServerVersion collects the server version from the Kubernetes cluster
323361
func gatherKubeServerVersion(_ context.Context,
324362
client *discovery.DiscoveryClient,

internal/cmd/export_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2021 - 2023 Crunchy Data Solutions, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"fmt"
19+
"testing"
20+
21+
"gotest.tools/v3/assert"
22+
)
23+
24+
func TestFileSizeReport(t *testing.T) {
25+
testsCases := []struct {
26+
desc string
27+
bytes float64
28+
output string
29+
}{
30+
{"Zero value", 0, fmt.Sprintf(msg1, 0/mebibyte)},
31+
{"Less than 25 MiB", 10000, fmt.Sprintf(msg1, 10000/mebibyte)},
32+
{"25 MiB", 26214400, fmt.Sprintf(msg1, 26214400/mebibyte)},
33+
{"25 MiB + 1 byte", 26214401,
34+
fmt.Sprintf(msg1, 26214401/mebibyte) + fmt.Sprintf(msg2, 26214400/mebibyte)},
35+
{"3 GiB", 3221225472,
36+
fmt.Sprintf(msg1, 3221225472/mebibyte) + fmt.Sprintf(msg2, 3221225472/mebibyte)},
37+
{"Something went wrong...", -1, fmt.Sprintf(msg1, -1/mebibyte)},
38+
}
39+
40+
for _, tc := range testsCases {
41+
t.Run(tc.desc, func(t *testing.T) {
42+
assert.Equal(t, exportSizeReport(tc.bytes), tc.output)
43+
})
44+
}
45+
}

0 commit comments

Comments
 (0)