-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
145 lines (127 loc) · 3.23 KB
/
Copy pathmain.go
File metadata and controls
145 lines (127 loc) · 3.23 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
141
142
143
144
145
package main
import (
"fmt"
"log/slog"
"os"
"time"
"github.com/lmittmann/tint"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
lfm "github.com/twangodev/lfm-api"
"github.com/urfave/cli/v2"
)
const name = "lfm-cli"
const version = "v1.6.2" // x-release-please-version
const discordAppId = "970003417277812736"
// Flags
var username string
var refreshInterval int
var showProfile bool
var showLoved bool
var covers bool
var elapsed bool
var keepStatus bool
var debug bool
var profileUrl string
// logLevel lets main() build the handler before flags are parsed; exec() raises it for --debug.
var logLevel = new(slog.LevelVar)
func exec(ctx *cli.Context) error {
showProfile = !ctx.Bool("hide-profile")
showLoved = ctx.Bool("show-loved")
covers = !ctx.Bool("rm-covers")
elapsed = !ctx.Bool("rm-time")
keepStatus = ctx.Bool("keep-status")
debug = ctx.Bool("debug")
if debug {
logLevel.Set(slog.LevelDebug)
} else {
logLevel.Set(slog.LevelInfo)
}
profileUrl = fmt.Sprintf("%vuser/%v", lfm.LastFmUrl, username)
slog.Info("Configuration loaded from arguments",
"username", username,
"refresh_interval", refreshInterval,
"show_profile", showProfile,
"show_loved", showLoved,
"show_covers", covers,
"show_elapsed", elapsed,
"keep_status", keepStatus,
"debug_enabled", debug,
)
for {
slog.Debug("Cycle begin.")
cycle()
slog.Debug("Cycle complete.")
time.Sleep(time.Duration(refreshInterval) * time.Second)
}
}
func main() {
slog.SetDefault(slog.New(tint.NewHandler(colorable.NewColorableStderr(), &tint.Options{
Level: logLevel,
TimeFormat: "15:04:05.000",
NoColor: !isatty.IsTerminal(os.Stderr.Fd()),
})))
app := &cli.App{
Name: name,
Description: "Show your Last.FM scrobbles on Discord!",
Version: version,
Compiled: time.Now(),
Authors: []*cli.Author{
{
Name: "James Ding",
Email: "james@twango.dev",
},
},
Copyright: "(c) 2022 James Ding",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "user",
Aliases: []string{"u"},
Usage: "Display Last.FM scrobbles from `USERNAME`",
Required: true,
Destination: &username,
},
&cli.IntFlag{
Name: "refresh",
Aliases: []string{"r"},
Usage: "Checks Last.FM every `X` seconds for new scrobbles",
Value: 10,
Destination: &refreshInterval,
},
&cli.BoolFlag{
Name: "hide-profile",
Usage: "Removes buttons to the specified Last.FM profile",
},
&cli.BoolFlag{
Name: "show-loved",
Aliases: []string{"l"},
Usage: "Replaces the default smallImage key with a heart for loved songs.",
},
&cli.BoolFlag{
Name: "rm-covers",
Usage: "Does not show album cover images.",
},
&cli.BoolFlag{
Name: "rm-time",
Usage: "Does not show time elapsed for the scrobble.",
},
&cli.BoolFlag{
Name: "keep-status",
Usage: "Shows status even when there is no active scrobble.",
},
&cli.BoolFlag{
Name: "debug",
Aliases: []string{"d"},
Usage: "Enable verbose and debug logging",
},
},
Action: func(context *cli.Context) error {
return exec(context)
},
}
err := app.Run(os.Args)
if err != nil {
slog.Error("fatal error", tint.Err(err))
os.Exit(1)
}
}