Files
umbrix-libcore/config/debug.go

46 lines
961 B
Go
Raw Normal View History

2024-01-15 17:17:05 +03:30
package config
2023-08-22 00:54:58 +03:30
import (
"bytes"
"encoding/json"
2023-10-14 17:22:26 +03:30
"fmt"
2023-08-22 00:54:58 +03:30
"os"
"path/filepath"
2023-10-14 17:22:26 +03:30
"runtime/debug"
2023-08-22 00:54:58 +03:30
"github.com/sagernet/sing-box/option"
)
func SaveCurrentConfig(path string, options option.Options) error {
2024-01-29 21:55:01 +01:00
json, err := ToJson(options)
if err != nil {
return err
}
p, err := filepath.Abs(filepath.Join(path, "current-config.json"))
fmt.Printf("Saving config to %v %+v\n", p, err)
if err != nil {
return err
}
return os.WriteFile(p, []byte(json), 0644)
}
func ToJson(options option.Options) (string, error) {
2023-08-22 00:54:58 +03:30
var buffer bytes.Buffer
encoder := json.NewEncoder(&buffer)
encoder.SetIndent("", " ")
2024-01-29 21:55:01 +01:00
// fmt.Printf("%+v\n", options)
2023-08-22 00:54:58 +03:30
err := encoder.Encode(options)
if err != nil {
2024-01-29 21:55:01 +01:00
fmt.Printf("ERROR in coding:%+v\n", err)
return "", err
2023-08-22 00:54:58 +03:30
}
2024-01-29 21:55:01 +01:00
return buffer.String(), nil
2023-08-22 00:54:58 +03:30
}
2023-10-14 17:22:26 +03:30
func DeferPanicToError(name string, err func(error)) {
if r := recover(); r != nil {
s := fmt.Errorf("%s panic: %s\n%s", name, r, string(debug.Stack()))
err(s)
}
}