gosuki/firefox/cmd_flags.go
Chakib Ben Ziane 03d30e6704 VERY MUCH WIP big refactoring into browser modules
- inspired from golang database package and caddyserver modules
- Migrating from class style to interfaces
- FIX: browser provisioning is broken, config is not ready when the
  browser module is registered.
2022-10-23 15:08:06 +02:00

88 lines
1.7 KiB
Go

package firefox
import (
"strings"
"git.sp4ke.xyz/sp4ke/gomark/cmd"
"git.sp4ke.xyz/sp4ke/gomark/config"
"git.sp4ke.xyz/sp4ke/gomark/utils"
"github.com/gobuffalo/flect"
"github.com/urfave/cli/v2"
)
const (
FirefoxDefaultProfileFlag = "firefox-default-profile"
)
var globalFirefoxFlags = []cli.Flag{
&cli.StringFlag{
Name: FirefoxDefaultProfileFlag,
Usage: "Set the default firefox `PROFILE` to use",
},
&cli.StringFlag{
Name: "firefox-default-dir",
Usage: "test",
},
}
// 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 {
if utils.Inlist(f.Names(), "help") ||
utils.Inlist(f.Names(), "version") {
continue
}
if !c.IsSet(f.Names()[0]) {
continue
}
sp := strings.Split(f.Names()[0], "-")
if len(sp) < 2 {
continue
}
if sp[0] != "firefox" {
continue
}
//TODO: document this feature
// extract global options that start with --firefox-*
optionName := flect.Pascalize(strings.Join(sp[1:], " "))
var destVal interface{}
// Find the corresponding flag
for _, ff := range globalFirefoxFlags {
if ff.String() == f.String() {
// Type switch on the flag type
switch ff.(type) {
case *cli.StringFlag:
destVal = c.String(f.Names()[0])
}
}
}
err := config.RegisterModuleOpt(BrowserName,
optionName, destVal)
if err != nil {
log.Fatal(err)
}
}
return nil
}
func init() {
cmd.RegBeforeHook(BrowserName, globalCommandFlagsManager)
for _, flag := range globalFirefoxFlags {
cmd.RegGlobalFlag(BrowserName, flag)
}
}