gosuki/firefox/cmd_flags.go

104 lines
2.3 KiB
Go
Raw Normal View History

package firefox
2019-02-27 16:39:55 +00:00
import (
"strings"
2023-02-18 23:13:23 +00:00
"git.blob42.xyz/gomark/gosuki/cmd"
"git.blob42.xyz/gomark/gosuki/config"
"git.blob42.xyz/gomark/gosuki/utils"
2020-08-12 18:13:01 +00:00
2019-02-27 16:39:55 +00:00
"github.com/gobuffalo/flect"
2020-08-12 18:13:01 +00:00
"github.com/urfave/cli/v2"
2019-02-27 16:39:55 +00:00
)
const (
2023-09-05 17:45:21 +00:00
FirefoxProfileFlag = "ff-profile"
2019-02-27 16:39:55 +00:00
)
var globalFirefoxFlags = []cli.Flag{
2022-11-07 19:07:13 +00:00
// This allows us to register dynamic cli flags which get converted to
// config.Configurator options.
// The flag must be given a name in the form `--firefox-<flag>`.
2020-08-12 18:13:01 +00:00
&cli.StringFlag{
2022-11-07 19:07:13 +00:00
Name: FirefoxProfileFlag,
2023-09-05 17:45:21 +00:00
Category: "firefox",
2019-02-27 16:39:55 +00:00
Usage: "Set the default firefox `PROFILE` to use",
},
2023-09-05 17:45:21 +00:00
// &cli.BoolFlag{
// Name: "ff-watch-all-profiles",
// Category: "firefox",
// Usage: "Watch all detected firefox profiles at the same time.",
// Aliases: []string{"ff-watch-all"},
// },
2019-02-27 16:39:55 +00:00
}
2020-09-17 06:18:53 +00:00
// Firefox global flags must start with --firefox-<flag name here>
// NOTE: is called in *cli.App.Before callback
func globalCommandFlagsManager(c *cli.Context) error {
log.Debugf("<%s> registering global flag manager", BrowserName)
for _, f := range c.App.Flags {
2023-01-11 11:14:10 +00:00
if utils.InList(f.Names(), "help") ||
utils.InList(f.Names(), "version") {
continue
}
2020-09-17 06:08:45 +00:00
if !c.IsSet(f.Names()[0]) {
continue
}
sp := strings.Split(f.Names()[0], "-")
2019-02-27 16:39:55 +00:00
if len(sp) < 2 {
continue
}
2023-09-05 17:45:21 +00:00
// TEST:
// TODO: add doc
// Firefox flags must start with --firefox-<flag name here>
// or -ff-<flag name here>
if !utils.InList([]string{"firefox", "ff"}, sp[0]) {
2019-02-27 16:39:55 +00:00
continue
}
2022-10-07 20:17:00 +00:00
//TODO: document this feature
2023-09-05 17:45:21 +00:00
// extracts global options that start with --firefox-*
optionName := flect.Pascalize(strings.Join(sp[1:], " "))
2019-02-27 16:39:55 +00:00
var destVal interface{}
// Find the corresponding flag
for _, ff := range globalFirefoxFlags {
if ff.String() == f.String() {
2019-02-27 16:39:55 +00:00
// Type switch on the flag type
switch ff.(type) {
case *cli.StringFlag:
2020-09-17 06:08:45 +00:00
destVal = c.String(f.Names()[0])
2023-09-05 17:45:21 +00:00
case *cli.BoolFlag:
destVal = c.Bool(f.Names()[0])
2019-02-27 16:39:55 +00:00
}
2023-09-05 17:45:21 +00:00
2019-02-27 16:39:55 +00:00
}
}
err := config.RegisterModuleOpt(BrowserName,
optionName, destVal)
2023-09-05 17:45:21 +00:00
if err != nil {
2023-09-05 17:45:21 +00:00
log.Panic(err)
}
2019-02-27 16:39:55 +00:00
}
return nil
}
func init() {
2023-09-05 17:45:21 +00:00
// register dynamic flag manager for firefox
cmd.RegBeforeHook(BrowserName, globalCommandFlagsManager)
for _, flag := range globalFirefoxFlags {
2023-09-05 17:45:21 +00:00
cmd.RegGlobalModFlag(BrowserName, flag)
}
}