Repository navigation
Expand file tree
/
Copy pathcli.go
More file actions
140 lines (123 loc) · 4.2 KB
/
Copy pathcli.go
File metadata and controls
140 lines (123 loc) · 4.2 KB
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
package main
import (
"errors"
"flag"
"fmt"
"io"
"github.com/BurntSushi/toml"
"github.com/refringe/anchor-lfs/config"
)
// usage describes the command line.
const usage = `Usage: anchor-lfs [--config <path>] [command]
Without a command, anchor-lfs starts the server.
Commands:
check Validate the configuration and print it with secrets redacted, or list every problem and exit 1
healthcheck Exit 0 if the local server's GET /ready answers 200, or 1 otherwise
version Print the version, commit and build date (also --version)
help Print this help (also -h, --help)
Options:
--config <path> Configuration file (default: $ANCHOR_LFS_CONFIG, then config.toml)
`
// Command names.
const (
commandCheck = "check"
commandHealthcheck = "healthcheck"
commandVersion = "version"
commandHelp = "help"
)
// redacted replaces secret values in the configuration printed by the check command.
const redacted = "<redacted>"
// runCommand parses the command line and runs the command, or the server when there is none, returning the exit code.
func runCommand(args []string, stdout, stderr io.Writer) int {
flags := flag.NewFlagSet("anchor-lfs", flag.ContinueOnError)
flags.SetOutput(io.Discard)
configFlag := flags.String("config", "", "")
versionFlag := flags.Bool("version", false, "")
command, err := parseArgs(flags, args)
switch {
case errors.Is(err, flag.ErrHelp):
command = commandHelp
case err != nil:
_, _ = fmt.Fprintf(stderr, "anchor-lfs: %v\n\n%s", err, usage)
return 2
case *versionFlag:
command = commandVersion
}
configPath := config.ResolvePath(*configFlag)
switch command {
case "":
return serve(configPath)
case commandCheck:
return runCheck(configPath, stdout, stderr)
case commandHealthcheck:
return runHealthcheck(configPath, stdout, stderr)
case commandVersion:
_, _ = fmt.Fprintf(stdout, "anchor-lfs %s (commit %s, built %s)\n", version, commit, date)
return 0
case commandHelp:
_, _ = fmt.Fprint(stdout, usage)
return 0
default:
_, _ = fmt.Fprintf(stderr, "anchor-lfs: unknown command %q\n\n%s", command, usage)
return 2
}
}
// parseArgs parses flags on both sides of the command and returns the command, or "" when there is none.
func parseArgs(flags *flag.FlagSet, args []string) (string, error) {
if err := flags.Parse(args); err != nil {
return "", err
}
if flags.NArg() == 0 {
return "", nil
}
command := flags.Arg(0)
if err := flags.Parse(flags.Args()[1:]); err != nil {
return "", err
}
if flags.NArg() > 0 {
return "", fmt.Errorf("unexpected argument %q", flags.Arg(0))
}
return command, nil
}
// runCheck loads the configuration at configPath and prints it with secrets redacted, or lists its problems.
func runCheck(configPath string, stdout, stderr io.Writer) int {
cfg, err := config.Load(configPath)
if invalid, ok := errors.AsType[*config.ValidationError](err); ok {
_, _ = fmt.Fprintf(stderr, "anchor-lfs: %s is invalid:\n", configPath)
for _, problem := range invalid.Problems {
_, _ = fmt.Fprintf(stderr, " - %v\n", problem)
}
return 1
}
if err != nil {
_, _ = fmt.Fprintf(stderr, "anchor-lfs: %v\n", err)
return 1
}
_, _ = fmt.Fprintf(stdout, "# %s is valid. Effective configuration, with secrets redacted:\n\n", configPath)
encoder := toml.NewEncoder(stdout)
encoder.Indent = ""
if err := encoder.Encode(effectiveConfig(cfg)); err != nil {
_, _ = fmt.Fprintf(stderr, "anchor-lfs: printing configuration: %v\n", err)
return 1
}
return 0
}
// effectiveConfig returns a copy of cfg with defaults spelt out, unused S3 settings dropped and secrets redacted.
func effectiveConfig(cfg *config.Config) config.Config {
out := *cfg
accessLog := cfg.Options.AccessLogEnabled()
out.Options.AccessLog = &accessLog
if out.Options.SigningKey != "" {
out.Options.SigningKey = redacted
}
if cfg.Storage.Backend != config.StorageS3 {
out.Storage = config.Storage{Backend: cfg.Storage.Backend}
return out
}
presignedURLs, forcePathStyle := cfg.Storage.S3PresignedURLsEnabled(), cfg.Storage.S3ForcePathStyleEnabled()
out.Storage.S3PresignedURLs, out.Storage.S3ForcePathStyle = &presignedURLs, &forcePathStyle
if out.Storage.S3SecretAccessKey != "" {
out.Storage.S3SecretAccessKey = redacted
}
return out
}