forked from dianhsu-official/cf-tool
/
database-add.go
155 lines (145 loc) · 3.7 KB
/
database-add.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package cmd
import (
"database/sql"
"errors"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/Arapak/sio-tool/util"
"os"
"path/filepath"
"github.com/Arapak/sio-tool/config"
"github.com/Arapak/sio-tool/database_client"
"github.com/fatih/color"
_ "modernc.org/sqlite"
)
func DatabaseAdd() (err error) {
cfg := config.Instance
t := GetTaskFromArgs()
db, err := sql.Open("sqlite", cfg.DbPath)
if err != nil {
fmt.Printf("failed to open database connection: %v\n", err)
return
}
defer db.Close()
t = ReadTask(t)
err = database_client.AddTask(db, t)
if err == nil {
color.Green(`Task successfully added`)
}
return
}
func GetTaskFromArgs() database_client.Task {
return database_client.Task{Name: Args.Name, Source: Args.Source, Path: Args.Path,
Link: Args.Link, ShortName: Args.Shortname, ContestID: Args.Contest, ContestStageID: Args.Stage}
}
func checkPath(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
return errors.New("this path does not exist")
}
return nil
}
func ReadTask(task database_client.Task) database_client.Task {
if task.Source == "" {
if err := survey.AskOne(&survey.Input{Message: "source"}, &task.Source, survey.WithValidator(survey.Required)); err != nil {
color.Red(err.Error())
os.Exit(1)
}
}
props := getValuesProperties(task.Source)
return ExtendTaskInfo(task, props)
}
func ExtendTaskInfo(task database_client.Task, props valuesProperties) database_client.Task {
if task.Source == "" {
if err := survey.AskOne(&survey.Input{Message: "source"}, &task.Source); err != nil {
color.Red(err.Error())
os.Exit(1)
}
}
if task.Name == "" && props.Name.needed {
util.GetValue("name", &task.Name, props.Name.required)
}
if props.Path.needed {
for {
if task.Path == "" {
util.GetValue("path", &task.Path, props.Path.required)
}
if task.Path == "" && props.Path.required {
break
}
err := checkPath(task.Path)
if err == nil {
break
}
color.Red(err.Error())
task.Path = ""
}
if task.Path != "" {
task.Path, _ = filepath.Abs(task.Path)
}
}
if task.ShortName == "" && props.ShortName.needed {
util.GetValue("shortname", &task.ShortName, props.ShortName.required)
}
if task.Link == "" && props.Link.needed {
util.GetValue("link", &task.Link, props.Link.required)
}
if task.ContestID == "" && props.ContestID.needed {
util.GetValue("contest", &task.ContestID, props.ContestID.required)
}
if task.ContestStageID == "" && props.ContestStageID.needed {
util.GetValue("stage", &task.ContestStageID, props.ContestStageID.required)
}
return task
}
type properties struct {
needed bool
required bool
}
type valuesProperties struct {
Name properties
Path properties
ShortName properties
Link properties
ContestID properties
ContestStageID properties
}
func getValuesProperties(source string) valuesProperties {
switch source {
case "cf":
return valuesProperties{
properties{true, true},
properties{true, true},
properties{true, true},
properties{true, true},
properties{true, true},
properties{false, false},
}
case "sio", "sio2":
return valuesProperties{
properties{true, true},
properties{true, true},
properties{true, true},
properties{true, true},
properties{true, false},
properties{false, false},
}
case "oi":
return valuesProperties{
properties{true, true},
properties{true, true},
properties{true, true},
properties{true, true},
properties{true, true},
properties{true, true},
}
default:
return valuesProperties{
properties{true, true},
properties{true, true},
properties{true, false},
properties{true, false},
properties{true, false},
properties{true, false},
}
}
}