chore: restrict custom templates by command (#27297)

This commit is contained in:
andig 2026-02-08 11:27:38 +01:00 • committed by GitHub
parent 3b374f7795
commit 9a9527d224
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 20 additions and 7 deletions

View file

@ -20,6 +20,8 @@ var chargerCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(chargerCmd)
withCustomTemplate(chargerCmd)
chargerCmd.Flags().Float64P(flagCurrent, "i", 0, flagCurrentDescription)
//lint:ignore SA1019 as Title is safe on ascii
chargerCmd.Flags().BoolP(flagEnable, "e", false, strings.Title(flagEnable))

View file

@ -21,6 +21,7 @@ var chargerRampCmd = &cobra.Command{
func init() {
chargerCmd.AddCommand(chargerRampCmd)
withCustomTemplate(chargerRampCmd)
chargerRampCmd.Flags().StringP(flagDigits, "", "0", "fractional digits (0..2)")
chargerRampCmd.Flags().StringP(flagDelay, "", "1s", "ramp delay")

View file

@ -18,6 +18,7 @@ var deviceCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(deviceCmd)
withCustomTemplate(deviceCmd)
}
func runDevice(cmd *cobra.Command, args []string) {

View file

@ -18,6 +18,8 @@ var meterCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(meterCmd)
withCustomTemplate(meterCmd)
meterCmd.Flags().StringP(flagBatteryMode, "b", "", flagBatteryModeDescription)
meterCmd.Flags().DurationP(flagBatteryModeWait, "w", 0, flagBatteryModeWaitDescription)
meterCmd.Flags().Bool(flagDiagnose, false, flagDiagnoseDescription)

View file

@ -80,17 +80,14 @@ func init() {
cobra.OnInitialize(initConfig)
withCustomTemplate(rootCmd)
// global options
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().BoolP("help", "h", false, "Help")
rootCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
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
rootCmd.PersistentFlags().StringP("log", "l", "info", "Log level (fatal, error, warn, info, debug, trace)")
@ -106,6 +103,8 @@ func init() {
bind(rootCmd, "mcp")
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
@ -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) {
if cmd.Name() == rootName {
return

View file

@ -522,8 +522,8 @@ func configureEnvironment(cmd *cobra.Command, conf *globalconfig.All) error {
// setup additional templates
if err == nil {
if cmd.PersistentFlags().Changed(flagTemplate) {
class, err := templates.ClassString(cmd.PersistentFlags().Lookup(flagTemplateType).Value.String())
if cmd.Flags().Changed(flagTemplate) {
class, err := templates.ClassString(cmd.Flags().Lookup(flagTemplateType).Value.String())
if err != nil {
return err
}

View file

@ -19,6 +19,7 @@ var tariffCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(tariffCmd)
withCustomTemplate(tariffCmd)
}
func runTariff(cmd *cobra.Command, args []string) {

View file

@ -20,6 +20,8 @@ var vehicleCmd = &cobra.Command{
func init() {
rootCmd.AddCommand(vehicleCmd)
withCustomTemplate(vehicleCmd)
vehicleCmd.Flags().Int64P(flagCurrent, "i", 0, flagCurrentDescription)
vehicleCmd.Flags().BoolP(flagStart, "a", false, flagStartDescription)
vehicleCmd.Flags().BoolP(flagStop, "o", false, flagStopDescription)