-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
131 lines (112 loc) · 3.4 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
package main
import (
"fmt"
"log"
"os"
"github.com/Gelio/go-global-update/internal/colors"
"github.com/Gelio/go-global-update/internal/gobinaries"
"github.com/Gelio/go-global-update/internal/gocli"
"github.com/Gelio/go-global-update/internal/updater"
"github.com/fatih/color"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/urfave/cli/v2"
)
func main() {
loggerConfig := zap.NewDevelopmentConfig()
// NOTE: re-define the version flag to use `V` instead of `v` as the alias
// `-v` belongs to the `--verbose` flag
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"V"},
Usage: "print the version",
}
app := &cli.App{
Name: "go-global-update",
Usage: `Update globally installed go binaries.
By default it will update all upgradable binaries in GOBIN.
If arguments are provided, only those binaries will be updated.
Examples:
* go-global-update gofumpt gopls shfmt
* go-global-update --dry-run`,
Version: "v0.2.5",
ArgsUsage: "[binaries to update...]",
UseShortOptionHandling: true,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "debug",
Usage: "Display debug information",
},
&cli.BoolFlag{
Name: "dry-run",
Aliases: []string{"n"},
Usage: "Check which binaries are upgradable without actually installing new versions",
},
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Usage: "Include more detailed information in the logs",
},
&cli.BoolFlag{
Name: "colors",
Usage: "Force using ANSI color codes in the output even if the output is not a TTY.\n\t\tSet the NO_COLOR environment variable if you want to force-disable colors (see https://no-color.org/).",
},
&cli.BoolFlag{
Name: "force",
Aliases: []string{"f"},
Usage: "Force reinstall all binaries, even if they do not need to be updated",
},
},
Action: func(c *cli.Context) error {
forceColors := c.Bool("colors")
colorsDecoratorFactory := colors.NewFactory(forceColors)
logger, err := loggerConfig.Build()
if err != nil {
return fmt.Errorf("cannot initialize zap logger: %w", err)
}
defer logger.Sync()
cmdRunner := gocli.NewCmdRunner(logger)
options := updater.Options{
DryRun: c.Bool("dry-run"),
Verbose: c.Bool("verbose"),
ForceReinstall: c.Bool("force"),
BinariesToUpdate: c.Args().Slice(),
}
if options.DryRun && options.ForceReinstall {
return fmt.Errorf("--dry-run and --force options cannot be used together")
}
err = updater.UpdateBinaries(
logger,
options,
os.Stdout,
&colorsDecoratorFactory,
&cmdRunner,
&gobinaries.FilesystemDirectoryLister{},
&updater.Filesystem{},
)
return err
},
Before: func(c *cli.Context) error {
debugMode := c.Bool("debug")
updateLoggerLevel(&loggerConfig, debugMode)
forceColors := c.Bool("colors")
updateLoggerColors(&loggerConfig, forceColors)
return nil
},
}
if err := app.Run(os.Args); err != nil {
log.Fatalf("could not run command: %v", err)
}
}
func updateLoggerLevel(loggerConfig *zap.Config, debugMode bool) {
logLevel := zap.InfoLevel
if debugMode {
logLevel = zap.DebugLevel
}
loggerConfig.Level.SetLevel(logLevel)
}
func updateLoggerColors(loggerConfig *zap.Config, forceColors bool) {
if forceColors || !color.NoColor {
loggerConfig.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
}