chore: restrict custom templates by command (#27297)
This commit is contained in:
parent
3b374f7795
commit
9a9527d224
8 changed files with 20 additions and 7 deletions
|
|
@ -20,6 +20,8 @@ var chargerCmd = &cobra.Command{
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(chargerCmd)
|
rootCmd.AddCommand(chargerCmd)
|
||||||
|
withCustomTemplate(chargerCmd)
|
||||||
|
|
||||||
chargerCmd.Flags().Float64P(flagCurrent, "i", 0, flagCurrentDescription)
|
chargerCmd.Flags().Float64P(flagCurrent, "i", 0, flagCurrentDescription)
|
||||||
//lint:ignore SA1019 as Title is safe on ascii
|
//lint:ignore SA1019 as Title is safe on ascii
|
||||||
chargerCmd.Flags().BoolP(flagEnable, "e", false, strings.Title(flagEnable))
|
chargerCmd.Flags().BoolP(flagEnable, "e", false, strings.Title(flagEnable))
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ var chargerRampCmd = &cobra.Command{
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
chargerCmd.AddCommand(chargerRampCmd)
|
chargerCmd.AddCommand(chargerRampCmd)
|
||||||
|
withCustomTemplate(chargerRampCmd)
|
||||||
|
|
||||||
chargerRampCmd.Flags().StringP(flagDigits, "", "0", "fractional digits (0..2)")
|
chargerRampCmd.Flags().StringP(flagDigits, "", "0", "fractional digits (0..2)")
|
||||||
chargerRampCmd.Flags().StringP(flagDelay, "", "1s", "ramp delay")
|
chargerRampCmd.Flags().StringP(flagDelay, "", "1s", "ramp delay")
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@ var deviceCmd = &cobra.Command{
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(deviceCmd)
|
rootCmd.AddCommand(deviceCmd)
|
||||||
|
withCustomTemplate(deviceCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runDevice(cmd *cobra.Command, args []string) {
|
func runDevice(cmd *cobra.Command, args []string) {
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,8 @@ var meterCmd = &cobra.Command{
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(meterCmd)
|
rootCmd.AddCommand(meterCmd)
|
||||||
|
withCustomTemplate(meterCmd)
|
||||||
|
|
||||||
meterCmd.Flags().StringP(flagBatteryMode, "b", "", flagBatteryModeDescription)
|
meterCmd.Flags().StringP(flagBatteryMode, "b", "", flagBatteryModeDescription)
|
||||||
meterCmd.Flags().DurationP(flagBatteryModeWait, "w", 0, flagBatteryModeWaitDescription)
|
meterCmd.Flags().DurationP(flagBatteryModeWait, "w", 0, flagBatteryModeWaitDescription)
|
||||||
meterCmd.Flags().Bool(flagDiagnose, false, flagDiagnoseDescription)
|
meterCmd.Flags().Bool(flagDiagnose, false, flagDiagnoseDescription)
|
||||||
|
|
|
||||||
14
cmd/root.go
14
cmd/root.go
|
|
@ -80,17 +80,14 @@ func init() {
|
||||||
|
|
||||||
cobra.OnInitialize(initConfig)
|
cobra.OnInitialize(initConfig)
|
||||||
|
|
||||||
|
withCustomTemplate(rootCmd)
|
||||||
|
|
||||||
// global options
|
// global options
|
||||||
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Config file (default \"~/evcc.yaml\" or \"/etc/evcc.yaml\")")
|
rootCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Config file (default \"~/evcc.yaml\" or \"/etc/evcc.yaml\")")
|
||||||
rootCmd.PersistentFlags().StringVar(&cfgDatabase, "database", "", "Database location (default \"~/.evcc/evcc.db\")")
|
rootCmd.PersistentFlags().StringVar(&cfgDatabase, "database", "", "Database location (default \"~/.evcc/evcc.db\")")
|
||||||
rootCmd.PersistentFlags().BoolP("help", "h", false, "Help")
|
rootCmd.PersistentFlags().BoolP("help", "h", false, "Help")
|
||||||
rootCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
|
rootCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
|
||||||
rootCmd.PersistentFlags().Bool(flagIgnoreDatabase, false, flagIgnoreDatabaseDescription)
|
rootCmd.PersistentFlags().Bool(flagIgnoreDatabase, false, flagIgnoreDatabaseDescription)
|
||||||
rootCmd.PersistentFlags().String(flagTemplate, "", flagTemplateDescription)
|
|
||||||
rootCmd.PersistentFlags().String(flagTemplateType, "", flagTemplateTypeDescription)
|
|
||||||
|
|
||||||
rootCmd.Flags().Bool(flagDemoMode, false, flagDemoModeDescription)
|
|
||||||
rootCmd.Flags().StringVar(&customCssFile, flagCustomCss, "", flagCustomCssDescription)
|
|
||||||
|
|
||||||
// config file options
|
// config file options
|
||||||
rootCmd.PersistentFlags().StringP("log", "l", "info", "Log level (fatal, error, warn, info, debug, trace)")
|
rootCmd.PersistentFlags().StringP("log", "l", "info", "Log level (fatal, error, warn, info, debug, trace)")
|
||||||
|
|
@ -106,6 +103,8 @@ func init() {
|
||||||
bind(rootCmd, "mcp")
|
bind(rootCmd, "mcp")
|
||||||
|
|
||||||
rootCmd.Flags().Bool(flagDisableAuth, false, flagDisableAuthDescription)
|
rootCmd.Flags().Bool(flagDisableAuth, false, flagDisableAuthDescription)
|
||||||
|
rootCmd.Flags().Bool(flagDemoMode, false, flagDemoModeDescription)
|
||||||
|
rootCmd.Flags().StringVar(&customCssFile, flagCustomCss, "", flagCustomCssDescription)
|
||||||
}
|
}
|
||||||
|
|
||||||
// initConfig reads in config file and ENV variables if set
|
// initConfig reads in config file and ENV variables if set
|
||||||
|
|
@ -138,6 +137,11 @@ func Execute() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func withCustomTemplate(cmd *cobra.Command) {
|
||||||
|
cmd.Flags().String(flagTemplate, "", flagTemplateDescription)
|
||||||
|
cmd.Flags().String(flagTemplateType, "", flagTemplateTypeDescription)
|
||||||
|
}
|
||||||
|
|
||||||
func allowCtrlC(cmd *cobra.Command, args []string) {
|
func allowCtrlC(cmd *cobra.Command, args []string) {
|
||||||
if cmd.Name() == rootName {
|
if cmd.Name() == rootName {
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -522,8 +522,8 @@ func configureEnvironment(cmd *cobra.Command, conf *globalconfig.All) error {
|
||||||
|
|
||||||
// setup additional templates
|
// setup additional templates
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if cmd.PersistentFlags().Changed(flagTemplate) {
|
if cmd.Flags().Changed(flagTemplate) {
|
||||||
class, err := templates.ClassString(cmd.PersistentFlags().Lookup(flagTemplateType).Value.String())
|
class, err := templates.ClassString(cmd.Flags().Lookup(flagTemplateType).Value.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ var tariffCmd = &cobra.Command{
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(tariffCmd)
|
rootCmd.AddCommand(tariffCmd)
|
||||||
|
withCustomTemplate(tariffCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runTariff(cmd *cobra.Command, args []string) {
|
func runTariff(cmd *cobra.Command, args []string) {
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ var vehicleCmd = &cobra.Command{
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(vehicleCmd)
|
rootCmd.AddCommand(vehicleCmd)
|
||||||
|
withCustomTemplate(vehicleCmd)
|
||||||
|
|
||||||
vehicleCmd.Flags().Int64P(flagCurrent, "i", 0, flagCurrentDescription)
|
vehicleCmd.Flags().Int64P(flagCurrent, "i", 0, flagCurrentDescription)
|
||||||
vehicleCmd.Flags().BoolP(flagStart, "a", false, flagStartDescription)
|
vehicleCmd.Flags().BoolP(flagStart, "a", false, flagStartDescription)
|
||||||
vehicleCmd.Flags().BoolP(flagStop, "o", false, flagStopDescription)
|
vehicleCmd.Flags().BoolP(flagStop, "o", false, flagStopDescription)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue