Add explicit demo mode (#21848)
Co-authored-by: Michael Geers <michael@geers.tv> Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
This commit is contained in:
parent
efa34402b5
commit
f927d5fa18
20 changed files with 289 additions and 96 deletions
|
|
@ -234,7 +234,7 @@ vehicles:
|
|||
icon: waterheater
|
||||
|
||||
site:
|
||||
title: Zuhause
|
||||
title: Demo Mode
|
||||
meters:
|
||||
grid: grid
|
||||
pv: pv
|
||||
|
|
|
|||
|
|
@ -12,6 +12,9 @@ const (
|
|||
flagHeaders = "log-headers"
|
||||
flagHeadersDescription = "Log headers"
|
||||
|
||||
flagDemoMode = "demo"
|
||||
flagDemoModeDescription = "Enter demo mode. Disables auth, config ui and restart"
|
||||
|
||||
flagIgnoreDatabase = "ignore-db"
|
||||
flagIgnoreDatabaseDescription = "Run command ignoring service database"
|
||||
|
||||
|
|
|
|||
28
cmd/root.go
28
cmd/root.go
|
|
@ -72,6 +72,7 @@ func init() {
|
|||
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(flagDemoMode, false, flagDemoModeDescription)
|
||||
rootCmd.PersistentFlags().Bool(flagHeaders, false, flagHeadersDescription)
|
||||
rootCmd.PersistentFlags().Bool(flagIgnoreDatabase, false, flagIgnoreDatabaseDescription)
|
||||
rootCmd.PersistentFlags().String(flagTemplate, "", flagTemplateDescription)
|
||||
|
|
@ -129,13 +130,22 @@ func runRoot(cmd *cobra.Command, args []string) {
|
|||
|
||||
// load config and re-configure logging after reading config file
|
||||
var err error
|
||||
if cfgErr := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); errors.As(cfgErr, &vpr.ConfigFileNotFoundError{}) {
|
||||
log.INFO.Println("missing config file - switching into demo mode")
|
||||
if ok, _ := cmd.Flags().GetBool(flagDemoMode); ok {
|
||||
log.INFO.Println("switching into demo mode as requested through the --demo flag")
|
||||
if err := demoConfig(&conf); err != nil {
|
||||
log.FATAL.Fatal(err)
|
||||
}
|
||||
} else {
|
||||
err = wrapErrorWithClass(ClassConfigFile, cfgErr)
|
||||
if cfgErr := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); errors.As(cfgErr, &vpr.ConfigFileNotFoundError{}) {
|
||||
log.INFO.Println("config file missing, create configuration using config UI")
|
||||
// evcc.yaml not found, use default configuration
|
||||
if err := viper.UnmarshalExact(&conf); err != nil {
|
||||
log.FATAL.Fatalf("failed to unmarshal default configuration: %v", err)
|
||||
}
|
||||
} else {
|
||||
// evcc.yaml found, might have errors
|
||||
err = wrapErrorWithClass(ClassConfigFile, cfgErr)
|
||||
}
|
||||
}
|
||||
|
||||
// setup environment
|
||||
|
|
@ -296,13 +306,19 @@ func runRoot(cmd *cobra.Command, args []string) {
|
|||
// allow web access for vehicles
|
||||
configureAuth(httpd.Router())
|
||||
|
||||
auth := auth.New()
|
||||
authObject := auth.New()
|
||||
if ok, _ := cmd.Flags().GetBool(flagDisableAuth); ok {
|
||||
log.WARN.Println("❗❗❗ Authentication is disabled. This is dangerous. Your data and credentials are not protected.")
|
||||
auth.Disable()
|
||||
authObject.SetAuthMode(auth.Disabled)
|
||||
}
|
||||
|
||||
httpd.RegisterSystemHandler(site, valueChan, cache, auth, func() {
|
||||
if ok, _ := cmd.Flags().GetBool(flagDemoMode); ok {
|
||||
log.WARN.Println("Authentication is locked in demo mode. Login-features are disabled.")
|
||||
authObject.SetAuthMode(auth.Locked)
|
||||
valueChan <- util.Param{Key: keys.DemoMode, Val: true}
|
||||
}
|
||||
|
||||
httpd.RegisterSystemHandler(site, valueChan, cache, authObject, func() {
|
||||
log.INFO.Println("evcc was stopped by user. OS should restart the service. Or restart manually.")
|
||||
err = errors.New("restart required") // https://gokrazy.org/development/process-interface/
|
||||
once.Do(func() { close(stopC) }) // signal loop to end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue