-
Notifications
You must be signed in to change notification settings - Fork 56
/
snapshot_summary.go
110 lines (96 loc) · 2.54 KB
/
snapshot_summary.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"os"
"github.com/sourcegraph/sourcegraph/lib/errors"
"github.com/sourcegraph/sourcegraph/lib/output"
"github.com/sourcegraph/src-cli/internal/api"
)
func init() {
usage := fmt.Sprintf(`'src snapshot summary' generates summary data for acceptance testing of a restored Sourcegraph instance with 'src snapshot test'.
USAGE
src login # site-admin authentication required
src [-v] snapshot summary
SUMMARY DATA
Use '-dump-requests' to see what data is requested, or open the generated summary at %q.
`, srcSnapshotSummaryPath)
flagSet := flag.NewFlagSet("summary", flag.ExitOnError)
apiFlags := api.NewFlags(flagSet)
snapshotCommands = append(snapshotCommands, &command{
flagSet: flagSet,
handler: func(args []string) error {
if err := flagSet.Parse(args); err != nil {
return err
}
out := output.NewOutput(flagSet.Output(), output.OutputOpts{Verbose: *verbose})
client := cfg.apiClient(apiFlags, flagSet.Output())
snapshotResult, err := fetchSnapshotSummary(context.Background(), client)
if err != nil {
return err
}
_ = os.MkdirAll(srcSnapshotDir, os.ModePerm)
f, err := os.OpenFile(srcSnapshotSummaryPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
if err != nil {
return errors.Wrap(err, "open snapshot file")
}
enc := json.NewEncoder(f)
enc.SetIndent("", "\t")
if err := enc.Encode(snapshotResult); err != nil {
return errors.Wrap(err, "write snapshot file")
}
out.WriteLine(output.Emojif(output.EmojiSuccess, "Summary snapshot data generated in %q!", srcSnapshotSummaryPath))
return nil
},
usageFunc: func() { fmt.Fprint(flag.CommandLine.Output(), usage) },
})
}
type snapshotSummary struct {
ExternalServices struct {
TotalCount int
Nodes []struct {
Kind string
ID string
}
}
Site struct {
AuthProviders struct {
TotalCount int
Nodes []struct {
ServiceType string
ServiceID string
}
}
}
}
func fetchSnapshotSummary(ctx context.Context, client api.Client) (*snapshotSummary, error) {
var snapshotResult snapshotSummary
ok, err := client.NewQuery(`
query GenerateSnapshotAcceptanceData {
externalServices {
totalCount
nodes {
kind
id
}
}
site {
authProviders {
totalCount
nodes {
serviceType
serviceID
}
}
}
}
`).Do(ctx, &snapshotResult)
if err != nil {
return nil, errors.Wrap(err, "generate snapshot")
} else if !ok {
return nil, errors.New("received no data")
}
return &snapshotResult, nil
}