This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.go
189 lines (172 loc) · 6.66 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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// nolint:gochecknoglobals
package cmd
import (
"errors"
"fmt"
"log"
"os"
"strings"
"github.com/Defacto2/df2/pkg/cmd/internal/arg"
"github.com/Defacto2/df2/pkg/cmd/internal/run"
"github.com/Defacto2/df2/pkg/database"
"github.com/Defacto2/df2/pkg/groups"
"github.com/Defacto2/df2/pkg/logs"
"github.com/Defacto2/df2/pkg/people"
"github.com/Defacto2/df2/pkg/recent"
"github.com/Defacto2/df2/pkg/sitemap"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var ErrNoOutput = errors.New("no output command used")
const notUsed = "\n\nThis document is not in use on the website."
var (
dbf database.Flags
gpf arg.Group
ppf arg.People
rcf arg.Recent
)
// outputCmd represents the output command.
var outputCmd = &cobra.Command{
Use: "output",
Short: "Generators for JSON, HTML, SQL and sitemap documents.",
Aliases: []string{"o"},
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
logs.Println(ErrNoOutput)
logs.Println()
if err := cmd.Usage(); err != nil {
logs.Fatal(err)
}
os.Exit(0)
}
if err := cmd.Usage(); err != nil {
logs.Fatal(err)
}
logs.Danger(fmt.Errorf("output cmd %q: %w", args[0], ErrCmd))
},
}
func init() { // nolint:gochecknoinits
const fifteen = 15
rootCmd.AddCommand(outputCmd)
outputCmd.AddCommand(dataCmd)
dataCmd.Flags().BoolVarP(&dbf.CronJob, "cronjob", "j", false,
"data backup for the cron time-based job scheduler\nall other flags are ignored")
dataCmd.Flags().BoolVarP(&dbf.Compress, "compress", "c", false,
fmt.Sprintf("save and compress the SQL using bzip2\n%s/d2-sql-create.bz2", viper.Get("directory.sql")))
dataCmd.Flags().UintVarP(&dbf.Limit, "limit", "l", 1,
"limit the number of rows returned (no limit 0)")
dataCmd.Flags().BoolVarP(&dbf.Parallel, "parallel", "p", true,
"run --table=all queries in parallel")
dataCmd.Flags().BoolVarP(&dbf.Save, "save", "s", false,
fmt.Sprintf("save the SQL\n%s/d2-sql-update.sql", viper.Get("directory.sql")))
dataCmd.Flags().StringVarP(&dbf.Tables, "table", "t", "files",
fmt.Sprintf("database table to use\noptions: all, %s", database.Tbls()))
dataCmd.Flags().StringVarP(&dbf.Type, "type", "y", "update",
"database export type\noptions: create or update")
if err := dataCmd.Flags().MarkHidden("parallel"); err != nil {
logs.Fatal(err)
}
outputCmd.AddCommand(groupCmd)
groupCmd.Flags().StringVarP(&gpf.Filter, "filter", "f", "",
"filter groups (default all)\noptions: "+strings.Join(groups.Wheres(), ","))
groupCmd.Flags().BoolVarP(&gpf.Counts, "count", "c", false,
"display the file totals for each group (SLOW)")
groupCmd.Flags().BoolVarP(&gpf.Progress, "progress", "p", true,
"show a progress indicator while fetching a large number of records")
groupCmd.Flags().BoolVarP(&gpf.Cronjob, "cronjob", "j", false,
"run in cronjob automated mode, ignores all other arguments")
groupCmd.Flags().BoolVar(&gpf.Forcejob, "forcejob", false,
"force the running of the cronjob automated mode")
groupCmd.Flags().StringVarP(&gpf.Format, "format", "t", "",
"output format (default html)\noptions: datalist,html,text")
groupCmd.Flags().BoolVarP(&gpf.Init, "initialism", "i", false,
"display the acronyms and initialisms for groups (SLOW)")
outputCmd.AddCommand(peopleCmd)
peopleCmd.Flags().StringVarP(&ppf.Filter, "filter", "f", "",
"filter people (default all)\noptions: "+people.Roles())
peopleCmd.Flags().StringVarP(&ppf.Format, "format", "t", "",
"output format (default html)\noptions: datalist,html,text")
peopleCmd.Flags().BoolVarP(&ppf.Cronjob, "cronjob", "j", false,
"run in cronjob automated mode, ignores all other arguments")
peopleCmd.Flags().BoolVar(&ppf.Forcejob, "forcejob", false,
"force the running of the cronjob automated mode")
outputCmd.AddCommand(recentCmd)
recentCmd.Flags().BoolVarP(&rcf.Compress, "compress", "c", false,
"remove insignificant whitespace characters")
recentCmd.Flags().UintVarP(&rcf.Limit, "limit", "l", fifteen,
"limit the number of rows returned")
outputCmd.AddCommand(sitemapCmd)
}
var dataCmd = &cobra.Command{
Use: "data",
Aliases: []string{"d", "sql"},
Short: "Generate SQL data dump export files.",
Long: `Generate a logical backup of the MySQL database. It produces
SQL statements that can be used to recreate the database objects and data.
The dumps can be used with mysqldump or Adminer to manage content in the
MySQL databases. `,
Run: func(cmd *cobra.Command, args []string) {
if err := run.Data(dbf); err != nil {
log.Print(err)
}
},
}
// groupCmd represents the organisations command.
var groupCmd = &cobra.Command{
Use: "groups",
Aliases: []string{"g", "group"},
Short: "HTML snippet generator to list groups.",
Long: `A HTML snippet generate to list groups. Each group will be wrapped with
a heading 2 element containing a relative anchor link to the group's page
and the name of the group.`,
Run: func(cmd *cobra.Command, args []string) {
if err := run.Groups(gpf); err != nil {
log.Print(err)
}
},
}
// peopleCmd represents the authors command.
var peopleCmd = &cobra.Command{
Use: "people",
Aliases: []string{"p", "ppl"},
Short: "HTML snippet generator to list people.",
Long: `A HTML snippet generate to list people. Each person will be wrapped with
a heading 2 element containing a relative anchor link to the person's page
and their name.` + notUsed,
Run: func(cmd *cobra.Command, args []string) {
if err := run.People(ppf); err != nil {
log.Print(err)
}
},
}
var recentCmd = &cobra.Command{
Use: "recent",
Aliases: []string{"r"},
Short: "JSON snippet generator to list recent additions.",
Long: `JSON snippet generator to list recent additions.` + notUsed,
Run: func(cmd *cobra.Command, args []string) {
if err := recent.List(rcf.Limit, rcf.Compress); err != nil {
log.Print(err)
}
},
}
var sitemapCmd = &cobra.Command{
Use: "sitemap",
Aliases: []string{"m", "s", "map"},
Short: "Sitemap generator.",
Long: `A sitemap generator to help search engines index the website.
"A sitemap is a file where you provide information about the pages, videos,
and other files on your site, and the relationships between them. Search
engines like Google read this file to crawl your site more efficiently.
A sitemap tells Google which pages and files you think are important in
your site, and also provides valuable information about these files."
"If your site's pages are properly linked, Google can usually discover most
of your site. Proper linking means that all pages that you deem important
can be reached through some form of navigation."
See: https://developers.google.com/search/docs/advanced/sitemaps/overview`,
Run: func(cmd *cobra.Command, args []string) {
if err := sitemap.Create(); err != nil {
log.Print(err)
}
},
}