/
output.go
61 lines (51 loc) · 1.73 KB
/
output.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
package analytic
import (
"context"
"fmt"
"log"
"os"
"github.com/authgear/authgear-server/pkg/lib/analytic"
)
type OutputReportOptions struct {
OutputType string
CSVOutputFilePath string
GoogleOAuthClientCredentialsJSONFilePath string
GoogleOAuthTokenFilePath string
SpreadsheetOutputMode analytic.OutputGoogleSpreadsheetMode
SpreadsheetID string
SpreadsheetRange string
}
func OutputReport(ctx context.Context, options *OutputReportOptions, data *analytic.ReportData) error {
switch options.OutputType {
case ReportOutputTypeCSV:
f, err := os.Create(options.CSVOutputFilePath)
defer f.Close()
if err != nil {
return fmt.Errorf("Unable to create csv file: %w", err)
}
outputCSV := analytic.OutputCSV{
Writer: f,
}
err = outputCSV.OutputReport(ctx, data)
if err != nil {
return fmt.Errorf("failed to output csv: %w", err)
}
log.Println("Done")
case ReportOutputTypeGoogleSheets:
outputGoogle := analytic.OutputGoogleSpreadsheet{
GoogleOAuthClientCredentialsJSONFilePath: options.GoogleOAuthClientCredentialsJSONFilePath,
GoogleOAuthTokenFilePath: options.GoogleOAuthTokenFilePath,
SpreadsheetOutputMode: options.SpreadsheetOutputMode,
SpreadsheetID: options.SpreadsheetID,
SpreadsheetRange: options.SpreadsheetRange,
}
err := outputGoogle.OutputReport(ctx, data)
if err != nil {
return fmt.Errorf("failed to output google spreadsheet: %w", err)
}
log.Println("Done")
default:
return fmt.Errorf("unsupported output type: %v", options.OutputType)
}
return nil
}