Skip to content

Commit 2fc2b07

Browse files
committed
Update deps, add color
1 parent 61ee657 commit 2fc2b07

File tree

7 files changed

+118
-488
lines changed

7 files changed

+118
-488
lines changed

config.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package main
33
import (
44
_ "embed"
55
"errors"
6+
"fmt"
7+
"os"
68

79
"github.com/bbkane/glib"
810
"go.bbkane.com/logos"
911
"go.bbkane.com/warg/command"
12+
"go.bbkane.com/warg/help/common"
1013
"go.uber.org/zap"
1114
lumberjack "gopkg.in/natefinch/lumberjack.v2"
1215
)
@@ -25,28 +28,30 @@ func editConfig(ctx command.Context) error {
2528
Compress: false,
2629
}
2730

28-
logger := logos.NewLogger(
29-
logos.NewZapSugaredLogger(
30-
lumberJackLogger, zap.DebugLevel, getVersion(),
31-
),
32-
)
31+
color, err := common.ConditionallyEnableColor(ctx.Flags, os.Stdout)
32+
if err != nil {
33+
fmt.Fprintf(os.Stderr, "Error enabling color, continuing without: %s", err.Error())
34+
}
35+
36+
zapLogger := logos.NewBBKaneZapLogger(lumberJackLogger, zap.DebugLevel, version)
37+
logger := logos.New(zapLogger, color)
3338
defer logger.Sync()
3439
logger.LogOnPanic()
3540

3641
configPath, configPathExists := ctx.Flags["--config"].(string)
3742
if !configPathExists {
3843
err := errors.New("must path --config")
39-
logos.Errorw(
44+
logger.Errorw(
4045
"Must pass --config",
4146
"err", err,
4247
)
4348
return err
4449
}
4550
editor := ctx.Flags["--editor"].(string)
4651

47-
err := glib.EditFile(embeddedConfig, configPath, editor)
52+
err = glib.EditFile(embeddedConfig, configPath, editor)
4853
if err != nil {
49-
logos.Errorw(
54+
logger.Errorw(
5055
"Unable to edit config",
5156
"configPath", configPath,
5257
"editorPath", editor,

go.mod

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,33 @@ require (
66
github.com/bbkane/glib v0.1.1
77
github.com/pkg/errors v0.9.1
88
github.com/vartanbeno/go-reddit/v2 v2.0.1
9-
go.bbkane.com/logos v0.3.0
10-
go.bbkane.com/warg v0.0.15
11-
go.uber.org/zap v1.20.0
12-
gopkg.in/natefinch/lumberjack.v2 v2.0.0
9+
go.bbkane.com/logos v0.4.0
10+
go.bbkane.com/warg v0.0.17
11+
go.uber.org/zap v1.24.0
12+
gopkg.in/natefinch/lumberjack.v2 v2.2.1
1313
)
1414

1515
require (
1616
github.com/davecgh/go-spew v1.1.1 // indirect
17+
github.com/kr/text v0.2.0 // indirect
1718
github.com/pmezard/go-difflib v1.0.0 // indirect
1819
gopkg.in/yaml.v3 v3.0.1 // indirect
1920
)
2021

2122
require (
22-
github.com/golang/protobuf v1.5.2 // indirect
23+
github.com/golang/protobuf v1.5.3 // indirect
2324
github.com/google/go-querystring v1.1.0 // indirect
24-
github.com/mattn/go-isatty v0.0.14 // indirect
25+
github.com/mattn/go-isatty v0.0.17 // indirect
2526
github.com/mitchellh/go-homedir v1.1.0 // indirect
2627
github.com/stretchr/testify v1.8.2
27-
github.com/xhit/go-str2duration/v2 v2.0.0 // indirect
28-
go.bbkane.com/gocolor v0.0.4 // indirect
29-
go.uber.org/atomic v1.9.0 // indirect
30-
go.uber.org/multierr v1.7.0 // indirect
31-
golang.org/x/net v0.0.0-20220114011407-0dd24b26b47d // indirect
32-
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
33-
golang.org/x/sys v0.0.0-20220608164250-635b8c9b7f68 // indirect
28+
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
29+
go.bbkane.com/gocolor v0.0.5 // indirect
30+
go.uber.org/atomic v1.10.0 // indirect
31+
go.uber.org/multierr v1.10.0 // indirect
32+
golang.org/x/net v0.8.0 // indirect
33+
golang.org/x/oauth2 v0.6.0 // indirect
34+
golang.org/x/sys v0.6.0 // indirect
3435
google.golang.org/appengine v1.6.7 // indirect
35-
google.golang.org/protobuf v1.27.1 // indirect
36+
google.golang.org/protobuf v1.29.0 // indirect
3637
gopkg.in/yaml.v2 v2.4.0 // indirect
3738
)

go.sum

Lines changed: 34 additions & 396 deletions
Large diffs are not rendered by default.

grab.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/vartanbeno/go-reddit/v2/reddit"
2020
"go.bbkane.com/logos"
2121
"go.bbkane.com/warg/command"
22+
"go.bbkane.com/warg/help/common"
2223
"go.uber.org/zap"
2324
lumberjack "gopkg.in/natefinch/lumberjack.v2"
2425
)
@@ -146,7 +147,7 @@ func validateImageURL(fullURL string) (string, error) {
146147
// Set baseURL to a non-empty string to override where the HTTP requests go, useful for tests
147148
func getTopPosts(ctx context.Context, timeout time.Duration, logger *logos.Logger, sr subreddit, baseURL string) ([]*reddit.Post, error) {
148149

149-
ua := runtime.GOOS + ":" + "grabbit" + ":" + getVersion() + " (go.bbkane.com/grabbit)"
150+
ua := runtime.GOOS + ":" + "grabbit" + ":" + version + " (go.bbkane.com/grabbit)"
150151

151152
// The reddit API does not like HTTP/2
152153
// Per https://pkg.go.dev/net/http?utm_source=gopls#pkg-overview ,
@@ -362,11 +363,13 @@ func grab(ctx command.Context) error {
362363
Compress: false,
363364
}
364365

365-
logger := logos.NewLogger(
366-
logos.NewZapSugaredLogger(
367-
lumberJackLogger, zap.DebugLevel, getVersion(),
368-
),
369-
)
366+
color, err := common.ConditionallyEnableColor(ctx.Flags, os.Stdout)
367+
if err != nil {
368+
fmt.Fprintf(os.Stderr, "Error enabling color, continuing without: %s", err.Error())
369+
}
370+
371+
zapLogger := logos.NewBBKaneZapLogger(lumberJackLogger, zap.DebugLevel, version)
372+
logger := logos.New(zapLogger, color)
370373
defer logger.Sync()
371374
logger.LogOnPanic()
372375

@@ -388,7 +391,7 @@ func grab(ctx command.Context) error {
388391
return errors.New("Non-matching lengths")
389392
}
390393

391-
err := testRedditConnection(logger)
394+
err = testRedditConnection(logger)
392395
if err != nil {
393396
return fmt.Errorf("Cannot connect to reddit: %w", err)
394397
}

main.go

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package main
22

33
import (
44
"os"
5+
"time"
56

67
"go.bbkane.com/warg"
78
"go.bbkane.com/warg/command"
89
"go.bbkane.com/warg/config/yamlreader"
910
"go.bbkane.com/warg/flag"
1011
"go.bbkane.com/warg/section"
11-
"go.bbkane.com/warg/value"
12+
"go.bbkane.com/warg/value/scalar"
13+
"go.bbkane.com/warg/value/slice"
1214
)
1315

1416
func app() *warg.App {
@@ -35,45 +37,51 @@ Homepage: https://github.com/bbkane/grabbit
3537
command.Flag(
3638
"--subreddit-name",
3739
"Subreddit to grab",
38-
value.StringSlice,
40+
slice.String(
41+
slice.Default([]string{"earthporn", "wallpapers"}),
42+
),
3943
flag.Alias("-sn"),
40-
flag.Default("earthporn", "wallpapers"),
4144
flag.ConfigPath("subreddits[].name"),
4245
flag.Required(),
4346
),
4447
command.Flag(
4548
"--subreddit-destination",
4649
"Where to store the subreddit",
47-
value.PathSlice,
50+
slice.Path(
51+
slice.Default([]string{".", "."}),
52+
),
4853
flag.Alias("-sd"),
49-
flag.Default(".", "."),
5054
flag.ConfigPath("subreddits[].destination"),
5155
flag.Required(),
5256
),
5357
command.Flag(
5458
"--subreddit-timeframe",
5559
"Take the top subreddits from this timeframe",
56-
value.StringEnumSlice("day", "week", "month", "year", "all"),
60+
slice.String(
61+
slice.Choices("day", "week", "month", "year", "all"),
62+
slice.Default([]string{"week", "week"}),
63+
),
5764
flag.Alias("-st"),
58-
flag.Default("week", "week"),
5965
flag.ConfigPath("subreddits[].timeframe"),
6066
flag.Required(),
6167
),
6268
command.Flag(
6369
"--subreddit-limit",
6470
"Max number of links to try to download",
65-
value.IntSlice,
71+
slice.Int(
72+
slice.Default([]int{2, 3}),
73+
),
6674
flag.Alias("-sl"),
67-
flag.Default("2", "3"),
6875
flag.ConfigPath("subreddits[].limit"),
6976
flag.Required(),
7077
),
7178
command.Flag(
7279
"--timeout",
7380
"Timeout for a single download",
74-
value.Duration,
81+
scalar.Duration(
82+
scalar.Default(time.Second*30),
83+
),
7584
flag.Alias("-t"),
76-
flag.Default("30s"),
7785
flag.Required(),
7886
),
7987
)
@@ -87,46 +95,39 @@ Homepage: https://github.com/bbkane/grabbit
8795
grabCmd,
8896
),
8997
section.Footer(appFooter),
90-
section.Command(
91-
"version",
92-
"Print version",
93-
printVersion,
94-
),
95-
section.Flag(
96-
"--color",
97-
"Use colorized output",
98-
value.StringEnum("true", "false", "auto"),
99-
flag.Default("auto"),
100-
),
10198
section.Flag(
10299
"--log-filename",
103100
"Log filename",
104-
value.Path,
105-
flag.Default("~/.config/grabbit.jsonl"),
101+
scalar.Path(
102+
scalar.Default("~/.config/grabbit.jsonl"),
103+
),
106104
flag.ConfigPath("lumberjacklogger.filename"),
107105
flag.Required(),
108106
),
109107
section.Flag(
110108
"--log-maxage",
111-
"Max age before log rotation in days",
112-
value.Int,
113-
flag.Default("30"),
109+
"Max age before log rotation in days", // TODO: change to duration flag
110+
scalar.Int(
111+
scalar.Default(30),
112+
),
114113
flag.ConfigPath("lumberjacklogger.maxage"),
115114
flag.Required(),
116115
),
117116
section.Flag(
118117
"--log-maxbackups",
119118
"Num backups for the log",
120-
value.Int,
121-
flag.Default("0"),
119+
scalar.Int(
120+
scalar.Default(0),
121+
),
122122
flag.ConfigPath("lumberjacklogger.maxbackups"),
123123
flag.Required(),
124124
),
125125
section.Flag(
126-
"--log-maxsize",
126+
"--log-maxsize", // TODO: make bytesize package similar to time?
127127
"Max size of log in megabytes",
128-
value.Int,
129-
flag.Default("5"),
128+
scalar.Int(
129+
scalar.Default(5),
130+
),
130131
flag.ConfigPath("lumberjacklogger.maxsize"),
131132
flag.Required(),
132133
),
@@ -140,9 +141,10 @@ Homepage: https://github.com/bbkane/grabbit
140141
command.Flag(
141142
"--editor",
142143
"Path to editor",
143-
value.String,
144+
scalar.String(
145+
scalar.Default("vi"),
146+
),
144147
flag.Alias("-e"),
145-
flag.Default("vi"),
146148
flag.EnvVars("EDITOR"),
147149
flag.Required(),
148150
),
@@ -151,11 +153,15 @@ Homepage: https://github.com/bbkane/grabbit
151153
),
152154
warg.ConfigFlag(
153155
"--config",
156+
[]scalar.ScalarOpt[string]{
157+
scalar.Default("~/.config/grabbit.yaml"),
158+
},
154159
yamlreader.New,
155160
"Config filepath",
156161
flag.Alias("-c"),
157-
flag.Default("~/.config/grabbit.yaml"),
158162
),
163+
warg.AddColorFlag(),
164+
warg.AddVersionCommand(version),
159165
warg.SkipValidation(),
160166
)
161167
return &app

main_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"testing"
55
)
66

7-
func TestBuildApp(t *testing.T) {
8-
if err := app().Validate(); err != nil {
7+
func TestApp_Validate(t *testing.T) {
8+
app := app()
9+
10+
if err := app.Validate(); err != nil {
911
t.Fatal(err)
1012
}
1113
}

version.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,3 @@
11
package main
22

3-
import (
4-
"fmt"
5-
"runtime/debug"
6-
7-
"go.bbkane.com/warg/command"
8-
)
9-
10-
// This will be overriden by goreleaser
11-
var version = "unkown version: error reading goreleaser info"
12-
13-
func getVersion() string {
14-
info, ok := debug.ReadBuildInfo()
15-
if !ok {
16-
return "unknown version: error reading build info"
17-
}
18-
// go install will embed this
19-
if info.Main.Version != "(devel)" {
20-
return info.Main.Version
21-
}
22-
return version
23-
}
24-
25-
func printVersion(_ command.Context) error {
26-
fmt.Println(getVersion())
27-
return nil
28-
}
3+
var version string

0 commit comments

Comments
 (0)