2022-10-23 13:08:06 +00:00
|
|
|
package firefox
|
2019-02-27 16:39:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2022-10-23 13:08:06 +00:00
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2022-10-23 13:08:06 +00:00
|
|
|
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",
|
|
|
|
},
|
2022-10-23 13:08:06 +00:00
|
|
|
&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>
|
2022-10-23 13:08:06 +00:00
|
|
|
// NOTE: is called in *cli.App.Before callback
|
|
|
|
func globalCommandFlagsManager(c *cli.Context) error {
|
|
|
|
log.Debugf("<%s> registering global flag manager", BrowserName)
|
2020-08-12 19:11:48 +00:00
|
|
|
for _, f := range c.App.Flags {
|
2019-02-27 16:59:50 +00:00
|
|
|
|
2022-10-07 20:17:00 +00:00
|
|
|
if utils.Inlist(f.Names(), "help") ||
|
|
|
|
utils.Inlist(f.Names(), "version") {
|
2019-02-27 16:59:50 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-09-17 06:08:45 +00:00
|
|
|
if !c.IsSet(f.Names()[0]) {
|
2020-08-12 19:11:48 +00:00
|
|
|
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
|
2022-10-23 13:08:06 +00:00
|
|
|
// extract global options that start with --firefox-*
|
2019-02-27 16:59:50 +00:00
|
|
|
optionName := flect.Pascalize(strings.Join(sp[1:], " "))
|
2019-02-27 16:39:55 +00:00
|
|
|
var destVal interface{}
|
|
|
|
|
|
|
|
// Find the corresponding flag
|
2022-10-23 13:08:06 +00:00
|
|
|
for _, ff := range globalFirefoxFlags {
|
2020-08-12 19:11:48 +00:00
|
|
|
if ff.String() == f.String() {
|
2019-02-27 16:39:55 +00:00
|
|
|
|
|
|
|
// Type switch on the flag type
|
|
|
|
switch ff.(type) {
|
|
|
|
|
2020-08-12 19:11:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-23 13:08:06 +00:00
|
|
|
err := config.RegisterModuleOpt(BrowserName,
|
2019-02-27 16:59:50 +00:00
|
|
|
optionName, destVal)
|
|
|
|
if err != nil {
|
2022-10-23 13:08:06 +00:00
|
|
|
log.Fatal(err)
|
2019-02-27 16:59:50 +00:00
|
|
|
}
|
2019-02-27 16:39:55 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-23 13:08:06 +00:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmd.RegBeforeHook(BrowserName, globalCommandFlagsManager)
|
|
|
|
|
|
|
|
for _, flag := range globalFirefoxFlags {
|
|
|
|
cmd.RegGlobalFlag(BrowserName, flag)
|
|
|
|
}
|
|
|
|
}
|