gosuki/browsers/firefox/cmd_flags.go

125 lines
3.2 KiB
Go
Raw Normal View History

2023-09-16 15:25:25 +00:00
//
// Copyright ⓒ 2023 Chakib Ben Ziane <contact@blob42.xyz> and [`GoSuki` contributors]
// (https://github.com/blob42/gosuki/graphs/contributors).
//
// All rights reserved.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This file is part of GoSuki.
//
// GoSuki is free software: you can redistribute it and/or modify it under the terms of
// the GNU Affero General Public License as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// GoSuki is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License along with
// gosuki. If not, see <http://www.gnu.org/licenses/>.
package firefox
2019-02-27 16:39:55 +00:00
import (
"strings"
2023-10-08 12:41:04 +00:00
"github.com/blob42/gosuki/cmd"
"github.com/blob42/gosuki/internal/config"
"github.com/blob42/gosuki/internal/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",
Usage: "set the default firefox `PROFILE` to use",
2019-02-27 16:39:55 +00:00
},
&cli.BoolFlag{
Name: "ff-watch-all-profiles",
Category: "firefox",
Usage: "watch all firefox profiles for changes",
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)
}
}