-
Notifications
You must be signed in to change notification settings - Fork 5
/
qa_configure.go
109 lines (95 loc) · 3.68 KB
/
qa_configure.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
/*
Copyright AppsCode Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmds
import (
"context"
"fmt"
"os"
"time"
"go.bytebuilders.dev/offline-license-server/pkg/server"
"github.com/pkg/errors"
"github.com/spf13/cobra"
csvtypes "gomodules.xyz/encoding/csv/types"
gdrive "gomodules.xyz/gdrive-utils"
"google.golang.org/api/option"
"google.golang.org/api/sheets/v4"
)
func NewCmdQATestConfigure() *cobra.Command {
cwd, _ := os.Getwd()
opts := struct {
GoogleCredentialDir string
ConfigDocId string
TestName string
QATemplateDocId string
StartDate string
TestDays int
Duration time.Duration
}{
GoogleCredentialDir: cwd,
StartDate: time.Now().UTC().Format(time.RFC3339),
TestDays: 3,
Duration: 90 * time.Minute,
}
cmd := &cobra.Command{
Use: "configure",
Short: "Configure Test",
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
if opts.TestName == "" {
return errors.New("missing test name.")
}
if opts.ConfigDocId == "" {
return errors.New("missing config doc id.")
}
if opts.QATemplateDocId == "" {
return errors.New("missing QA template doc id.")
}
startDate, err := time.Parse(time.RFC3339, opts.StartDate)
if err != nil {
return errors.Wrap(err, "failed to parse start date")
}
client, err := gdrive.DefaultClient(opts.GoogleCredentialDir)
if err != nil {
return errors.Wrap(err, "Error creating Google client")
}
svcSheets, err := sheets.NewService(context.TODO(), option.WithHTTPClient(client))
if err != nil {
return errors.Wrap(err, "Error creating Sheets client")
}
cfg := server.QuestionConfig{
ConfigType: server.ConfigTypeQuestion,
TestName: opts.TestName,
QuestionTemplateDocId: opts.QATemplateDocId,
StartDate: csvtypes.Date{Time: startDate},
EndDate: csvtypes.Date{Time: startDate.Add(time.Duration(opts.TestDays) * 24 * time.Hour)},
Duration: csvtypes.Duration{Duration: opts.Duration},
}
err = server.SaveConfig(svcSheets, opts.ConfigDocId, cfg)
if err != nil {
return errors.Wrap(err, "failed to save config")
}
fmt.Println()
fmt.Println("Email the following link to candidates:")
fmt.Printf("https://x.appscode.com/_/qa/%s/\n", opts.ConfigDocId)
return nil
},
}
cmd.Flags().StringVar(&opts.GoogleCredentialDir, "google.credential-dir", opts.GoogleCredentialDir, "Directory used to store Google credential")
cmd.Flags().StringVar(&opts.ConfigDocId, "test.config-doc-id", opts.ConfigDocId, "Google sheets id for config spread sheet")
cmd.Flags().StringVar(&opts.TestName, "test.name", opts.TestName, "Name of the test")
cmd.Flags().StringVar(&opts.QATemplateDocId, "test.qa-template-doc-id", opts.QATemplateDocId, "Google docs id for QA template")
cmd.Flags().StringVar(&opts.StartDate, "test.start-date", opts.StartDate, "Start date for test")
cmd.Flags().IntVar(&opts.TestDays, "test.days-to-take-test", opts.TestDays, "Number of days available to take the test")
cmd.Flags().DurationVar(&opts.Duration, "test.duration", opts.Duration, "Test duration")
return cmd
}