gosuki/cmd/firefox_flags.go

75 lines
1.3 KiB
Go
Raw Normal View History

2019-02-27 16:39:55 +00:00
package cmd
import (
"strings"
2020-11-06 17:50:36 +00:00
"git.sp4ke.xyz/sp4ke/gomark/config"
"git.sp4ke.xyz/sp4ke/gomark/mozilla"
"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 FirefoxGlobalFlags = []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",
},
}
2020-09-17 06:18:53 +00:00
// Firefox global flags must start with --firefox-<flag name here>
2019-02-27 16:39:55 +00:00
func GlobalFirefoxFlagsManager(c *cli.Context) error {
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
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 FirefoxGlobalFlags {
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(mozilla.ConfigName,
optionName, destVal)
if err != nil {
fflog.Fatal(err)
}
2019-02-27 16:39:55 +00:00
}
return nil
}