-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
121 lines (108 loc) · 3.14 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
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// keep-sorted is a tool that sorts lines between two markers in a larger file.
package main
import (
"fmt"
"os"
"path"
"runtime/debug"
"time"
"github.com/google/keep-sorted/cmd"
"github.com/mattn/go-isatty"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
flag "github.com/spf13/pflag"
)
func main() {
c := &cmd.Config{}
c.FromFlags(nil)
logLevel := flag.CountP("verbose", "v", "Log more verbosely")
colorMode := flag.String("color", "auto", "Whether to color debug output. One of \"always\", \"never\", or \"auto\"")
omitTimestamps := flag.Bool("omit-timestamps", false, "Do not emit timestamps in console logging. Useful for tests")
version := flag.Bool("version", false, "Report the keep-sorted version.")
if err := flag.CommandLine.MarkHidden("omit-timestamps"); err != nil {
panic(err)
}
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] file1 [file2 ...]\n\n", path.Base(os.Args[0]))
fmt.Fprint(os.Stderr, "Note that '-' can be used to read from stdin, "+
"in which case the output is written to stdout.\n\n")
fmt.Fprintln(os.Stderr, "Flags:")
flag.PrintDefaults()
}
flag.Parse()
if *version {
fmt.Fprintln(os.Stdout, readVersion())
return
}
out := os.Stderr
var shouldColor bool
switch *colorMode {
case "always":
shouldColor = true
case "never":
shouldColor = false
case "auto":
shouldColor = isatty.IsTerminal(out.Fd())
default:
log.Err(fmt.Errorf("invalid --color %q", *colorMode)).Msg("")
}
cw := zerolog.ConsoleWriter{Out: out, TimeFormat: time.RFC3339, NoColor: !shouldColor}
if *omitTimestamps {
cw.FormatTimestamp = func(any) string { return "" }
}
log.Logger = log.Output(cw)
zerolog.SetGlobalLevel(zerolog.Level(int(zerolog.WarnLevel) - *logLevel))
if ok, err := cmd.Run(c, flag.Args()); err != nil {
log.Fatal().AnErr("error", err).Msg("")
} else if !ok {
os.Exit(1)
}
}
func readVersion() string {
bi, ok := debug.ReadBuildInfo()
if !ok {
return "unknown"
}
const revisionKey = "vcs.revision"
const timeKey = "vcs.time"
const dirtyKey = "vcs.modified"
settings := make(map[string]string)
for _, s := range bi.Settings {
settings[s.Key] = s.Value
}
var s string
if v := bi.Main.Version; v != "" && v != "(devel)" {
s = v
} else if r := settings[revisionKey]; r != "" {
s = r
if len(s) > 7 {
s = s[:7]
}
}
if s == "" {
return "unknown"
}
if settings[dirtyKey] == "true" {
s += "-dev"
}
if t := settings[timeKey]; t != "" {
if ts, err := time.Parse(time.RFC3339, t); err == nil {
t = ts.In(time.Local).Format(time.RFC3339)
}
s += fmt.Sprintf(" (%s)", t)
}
return s
}