-
Notifications
You must be signed in to change notification settings - Fork 394
/
cmd_lazy_filewalker.go
168 lines (138 loc) · 4.07 KB
/
cmd_lazy_filewalker.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
// Copyright (C) 2023 Storj Labs, Inc.
// See LICENSE for copying information.
package internalcmd
import (
"context"
"flag"
"fmt"
"io"
"net/url"
"os"
"github.com/spf13/cobra"
"go.uber.org/zap"
"storj.io/private/process"
"storj.io/storj/storagenode/pieces/lazyfilewalker"
"storj.io/storj/storagenode/pieces/lazyfilewalker/execwrapper"
"storj.io/storj/storagenode/storagenodedb"
)
// FilewalkerCfg is the config structure for the lazyfilewalker commands.
type FilewalkerCfg struct {
lazyfilewalker.Config
}
// DatabaseConfig returns the storagenodedb.Config that should be used with this lazyfilewalker.
func (config *FilewalkerCfg) DatabaseConfig() storagenodedb.Config {
return storagenodedb.Config{
Storage: config.Storage,
Info: config.Info,
Info2: config.Info2,
Pieces: config.Pieces,
Filestore: config.Filestore,
Driver: config.Driver,
}
}
// RunOptions defines the options for the lazyfilewalker runners.
type RunOptions struct {
Ctx context.Context
Logger *zap.Logger
config *FilewalkerCfg
stdin io.Reader
stderr io.Writer
stdout io.Writer
}
// LazyFilewalkerCmd is a wrapper for the lazyfilewalker commands.
type LazyFilewalkerCmd struct {
Command *cobra.Command
*RunOptions
}
var _ execwrapper.Command = (*LazyFilewalkerCmd)(nil)
// SetArgs sets arguments for the command.
// The command or executable path should be passed as the first argument.
func (cmd *LazyFilewalkerCmd) SetArgs(args []string) {
if len(args) > 0 {
// arg[0] is the command name or executable path, which we don't need
// args[1] is the lazyfilewalker subcommand.
args = args[2:]
}
cmd.Command.SetArgs(args)
}
// Run runs the LazyFileWalker.
func (cmd *LazyFilewalkerCmd) Run() error {
return cmd.Command.ExecuteContext(cmd.Ctx)
}
// Start starts the LazyFileWalker command, assuming it behaves like the Start method on exec.Cmd.
// This is a no-op and only exists to satisfy the execwrapper.Command interface.
// Wait must be called to actually run the command.
func (cmd *LazyFilewalkerCmd) Start() error {
return nil
}
// Wait waits for the LazyFileWalker to finish, assuming it behaves like the Wait method on exec.Cmd.
func (cmd *LazyFilewalkerCmd) Wait() error {
return cmd.Run()
}
func (r *RunOptions) normalize(cmd *cobra.Command) {
if r.Ctx == nil {
ctx, _ := process.Ctx(cmd)
r.Ctx = ctx
}
if r.stdin == nil {
r.SetIn(os.Stdin)
}
if r.stdout == nil {
r.SetOut(os.Stdout)
}
if r.stderr == nil {
r.SetErr(os.Stderr)
}
if r.Logger == nil {
r.Logger = zap.L()
}
}
// SetIn sets the stdin reader.
func (r *RunOptions) SetIn(reader io.Reader) {
r.stdin = reader
}
// SetOut sets the stdout writer.
func (r *RunOptions) SetOut(writer io.Writer) {
r.stdout = writer
}
// SetErr sets the stderr writer.
func (r *RunOptions) SetErr(writer io.Writer) {
r.stderr = writer
r.tryCreateNewLogger()
}
func (r *RunOptions) tryCreateNewLogger() {
// If the writer is os.Stderr, we don't need to register it because the stderr
// writer is registered by default.
if r.stderr == os.Stderr {
return
}
writerkey := "zapwriter"
err := zap.RegisterSink(writerkey, func(u *url.URL) (zap.Sink, error) {
return nopWriterSyncCloser{r.stderr}, nil
})
// this error is expected if the sink is already registered.
duplicateSinkErr := fmt.Errorf("sink factory already registered for scheme %q", writerkey)
if err != nil && err.Error() != duplicateSinkErr.Error() {
r.Logger.Error("failed to register logger sink", zap.Error(err))
return
}
err = flag.Set("log.encoding", "json")
if err != nil {
r.Logger.Error("failed to set log encoding", zap.Error(err))
return
}
// create a new logger with the writer as the output path.
path := fmt.Sprintf("%s:subprocess", writerkey)
logger, err := process.NewLoggerWithOutputPaths("lazyfilewalker", path)
if err != nil {
r.Logger.Error("failed to create logger", zap.Error(err))
return
}
// set the logger to the new logger.
r.Logger = logger
}
type nopWriterSyncCloser struct {
io.Writer
}
func (cw nopWriterSyncCloser) Close() error { return nil }
func (cw nopWriterSyncCloser) Sync() error { return nil }