-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
193 lines (163 loc) · 6.21 KB
/
main.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
190
191
192
193
package main
import (
"fmt"
"os"
"strings"
"github.com/charmbracelet/log"
"github.com/joshbeard/web-indexer/internal/webindexer"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// version is set at build time using -ldflags
var version = "dev"
var (
cfg webindexer.Config
exiter Exiter = DefaultExiter{}
)
var rootCmd = &cobra.Command{
Use: "web-indexer [flags]",
Version: version,
Short: "Generate index files for a directory or S3 bucket",
Long: "web-indexer is a tool to generate index files for a directory or S3 bucket.\n\n" +
"See https://github.com/joshbeard/web-indexer for more information.\n\n" +
"The source and target can be specified using their flags or as " +
"the\nfirst and second arguments.\n\nA custom configuration file can be " +
"specified using the --config flag.\nBy default, web-indexer will look " +
"for a .web-indexer.yml or .web-indexer.yaml\nfile in the current directory.",
Example: strings.Join([]string{
" Index a local directory and write the index file to the same directory",
" web-indexer --source /path/to/directory --target /path/to/directory",
" Index a local directory and write the index file to a different directory",
" web-indexer --source /path/to/directory --target /foo/bar",
" Index a local directory and upload the index file to an S3 bucket",
" web-indexer --source /path/to/directory --target s3://bucket/path",
" Index an S3 bucket and write the index file to a local directory",
" web-indexer --source s3://bucket/path --target /path/to/directory",
" Index an S3 bucket and upload the index file to the same bucket and path",
" web-indexer --source s3://bucket/path --target s3://bucket/path",
"",
" Run with a custom configuration file",
" web-indexer -c custom.yml /path/to/source /path/to/target",
"",
" Set a title for the index pages",
" web-indexer \\",
" --source /path/to/directory \\",
" --target /path/to/directory \\",
" --title 'Index of {relativePath}'",
}, "\n"),
Run: func(cmd *cobra.Command, args []string) {
if err := run(args); err != nil {
fmt.Println("FATAL:", err)
exiter.Exit(1)
}
},
}
func main() {
err := rootCmd.Execute()
cobra.CheckErr(err)
}
func run(args []string) error {
err := viper.Unmarshal(&cfg)
if err != nil {
return fmt.Errorf("unable to unmarshal config: %w", err)
}
// If 2 arguments are passed, the first is the source and the
// second is the target
if len(args) == 2 {
cfg.Source = args[0]
cfg.Target = args[1]
} else if len(args) > 0 {
return fmt.Errorf("unknown arguments: %v", args)
}
if err := setupLogger(cfg); err != nil {
return fmt.Errorf("unable to setup logger: %w", err)
}
indexer, err := webindexer.New(cfg)
if err != nil {
return fmt.Errorf("unable to create indexer: %w", err)
}
log.Infof("Generating index for %s", cfg.Source)
err = indexer.Generate(indexer.Cfg.BasePath)
if err != nil {
return fmt.Errorf("unable to generate index: %w", err)
}
return nil
}
func initConfig(cfgFile *string) func() {
return func() {
if *cfgFile != "" {
viper.SetConfigFile(*cfgFile)
} else {
// Look for a .web-indexer.yml or .web-indexer.yaml file in the
// current directory
for _, name := range []string{".web-indexer.yml", ".web-indexer.yaml"} {
if _, err := os.Stat(name); err == nil {
viper.SetConfigFile(name)
break
}
}
}
// Environment variables
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err == nil {
log.Debugf("Using config file: %s", viper.ConfigFileUsed())
}
}
}
func init() {
cobra.OnInitialize(initConfig(&cfg.CfgFile))
rootCmd.PersistentFlags().StringVarP(&cfg.CfgFile, "config", "c", "", "config file")
rootCmd.Flags().StringVarP(&cfg.BaseURL, "base-url", "u", "", "A URL to prepend to the links")
rootCmd.Flags().StringVarP(&cfg.DateFormat, "date-format", "", "2006-01-02 15:04:05 MST", "The date format to use in the index page")
rootCmd.Flags().BoolVarP(&cfg.DirsFirst, "dirs-first", "", true, "List directories first")
rootCmd.Flags().StringVarP(&cfg.IndexFile, "index-file", "i", "index.html", "The name of the index file")
rootCmd.Flags().BoolVarP(&cfg.LinkToIndexes, "link-to-index", "l", false, "Link to the index file or just the path")
rootCmd.Flags().StringVarP(&cfg.LogLevel, "log-level", "L", "info", "The log level")
rootCmd.Flags().StringVarP(&cfg.LogFile, "log-file", "F", "", "The log file")
rootCmd.Flags().BoolVarP(&cfg.Minify, "minify", "m", false, "Minify the index page")
rootCmd.Flags().BoolVarP(&cfg.Quiet, "quiet", "q", false, "Suppress log output")
rootCmd.Flags().StringVarP(&cfg.Order, "order", "", "asc", "The order for the items. One of: asc, desc")
rootCmd.Flags().BoolVarP(&cfg.Recursive, "recursive", "r", false, "List files recursively")
rootCmd.Flags().StringSliceVarP(&cfg.Skips, "skip", "S", []string{}, "A list of files or directories to skip. "+
"Comma separated or specified multiple times")
rootCmd.Flags().StringVarP(&cfg.SortBy, "sort-by", "", "natural_name", "The order for the index page. One of: last_modified, name, natural_name")
rootCmd.Flags().StringVarP(&cfg.Source, "source", "s", "", "REQUIRED. The source directory or S3 URI to list")
rootCmd.Flags().StringVarP(&cfg.Target, "target", "t", "", "REQUIRED. The target directory or S3 URI to write to")
rootCmd.Flags().StringVarP(&cfg.Template, "template", "f", "", "A custom template file to use for the index page")
rootCmd.Flags().StringVarP(&cfg.Title, "title", "T", "", "The title of the index page")
err := viper.BindPFlags(rootCmd.Flags())
cobra.CheckErr(err)
}
func setupLogger(cfg webindexer.Config) error {
if cfg.LogLevel == "" || cfg.Quiet {
devnull, err := os.Open(os.DevNull)
if err != nil {
return err
}
log.SetOutput(devnull)
return nil
}
logLevel, err := log.ParseLevel(cfg.LogLevel)
if err != nil {
return fmt.Errorf("unable to parse log level: %w", err)
}
log.SetLevel(logLevel)
if cfg.LogFile == "" {
log.SetOutput(os.Stdout)
return nil
}
f, err := os.OpenFile(cfg.LogFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
if err != nil {
return err
} else {
log.SetOutput(f)
}
return nil
}
type Exiter interface {
Exit(code int)
}
type DefaultExiter struct{}
func (e DefaultExiter) Exit(code int) {
os.Exit(code)
}