gosuki/firefox/cmd_flags.go

88 lines
1.7 KiB
Go
Raw Normal View History

package firefox
2019-02-27 16:39:55 +00:00
import (
"strings"
"git.sp4ke.xyz/sp4ke/gomark/cmd"
2020-11-06 17:50:36 +00:00
"git.sp4ke.xyz/sp4ke/gomark/config"
"git.sp4ke.xyz/sp4ke/gomark/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 (
FirefoxDefaultProfileFlag = "firefox-default-profile"
)
var globalFirefoxFlags = []cli.Flag{
2020-08-12 18:13:01 +00:00
&cli.StringFlag{
2019-02-27 16:39:55 +00:00
Name: FirefoxDefaultProfileFlag,
Usage: "Set the default firefox `PROFILE` to use",
},
&cli.StringFlag{
Name: "firefox-default-dir",
Usage: "test",
},
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 {
2022-10-07 20:17:00 +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
}
if sp[0] != "firefox" {
continue
}
2022-10-07 20:17:00 +00:00
//TODO: document this feature
// extract 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])
2019-02-27 16:39:55 +00:00
}
}
}
err := config.RegisterModuleOpt(BrowserName,
optionName, destVal)
if err != nil {
log.Fatal(err)
}
2019-02-27 16:39:55 +00:00
}
return nil
}
func init() {
cmd.RegBeforeHook(BrowserName, globalCommandFlagsManager)
for _, flag := range globalFirefoxFlags {
cmd.RegGlobalFlag(BrowserName, flag)
}
}