forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor application shutdown (evcc-io#4757)
- Loading branch information
Showing
10 changed files
with
137 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/evcc-io/evcc/cmd/shutdown" | ||
) | ||
|
||
// unwrap converts a wrapped error into slice of strings | ||
func unwrap(err error) (res []string) { | ||
for err != nil { | ||
inner := errors.Unwrap(err) | ||
if inner == nil { | ||
res = append(res, err.Error()) | ||
} else { | ||
cur := strings.TrimSuffix(err.Error(), ": "+inner.Error()) | ||
cur = strings.TrimSuffix(cur, inner.Error()) | ||
res = append(res, strings.TrimSpace(cur)) | ||
} | ||
err = inner | ||
} | ||
return | ||
} | ||
|
||
// redact redacts a configuration string | ||
func redact(src string) string { | ||
secrets := []string{ | ||
"url", "uri", "host", "broker", "mac", // infrastructure | ||
"sponsortoken", "plant", // global settings | ||
"user", "password", "pin", // users | ||
"token", "access", "refresh", // tokens | ||
"ain", "id", "secret", "serial", "deviceid", "machineid", // devices | ||
"vin"} // vehicles | ||
return regexp. | ||
MustCompile(fmt.Sprintf(`\b(%s)\b.*?:.*`, strings.Join(secrets, "|"))). | ||
ReplaceAllString(src, "$1: *****") | ||
} | ||
|
||
func publishErrorInfo(cfgFile string, err error) { | ||
if cfgFile != "" { | ||
file, pathErr := filepath.Abs(cfgFile) | ||
if pathErr != nil { | ||
file = cfgFile | ||
} | ||
publish("file", file) | ||
|
||
if src, fileErr := os.ReadFile(cfgFile); fileErr != nil { | ||
log.ERROR.Println("could not open config file:", fileErr) | ||
} else { | ||
publish("config", redact(string(src))) | ||
|
||
// find line number | ||
if match := regexp.MustCompile(`yaml: line (\d+):`).FindStringSubmatch(err.Error()); len(match) == 2 { | ||
if line, err := strconv.Atoi(match[1]); err == nil { | ||
publish("line", line) | ||
} | ||
} | ||
} | ||
} | ||
|
||
publish("fatal", unwrap(err)) | ||
} | ||
|
||
// fatal logs a fatal error and runs shutdown functions before terminating | ||
func fatal(err error) { | ||
log.FATAL.Println(err) | ||
<-shutdownDoneC() | ||
os.Exit(1) | ||
} | ||
|
||
// shutdownDoneC returns a channel that closes when shutdown has completed | ||
func shutdownDoneC() <-chan struct{} { | ||
doneC := make(chan struct{}) | ||
go shutdown.Cleanup(doneC) | ||
return doneC | ||
} | ||
|
||
// exitWhenDone waits for shutdown to complete with timeout | ||
func exitWhenDone(timeout time.Duration) { | ||
select { | ||
case <-shutdownDoneC(): // wait for shutdown | ||
case <-time.After(timeout): | ||
} | ||
|
||
os.Exit(1) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.