-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
68 lines (59 loc) · 1.56 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
_ "embed"
"errors"
"fmt"
"os"
"github.com/bbkane/glib"
"go.bbkane.com/logos"
"go.bbkane.com/warg/command"
"go.bbkane.com/warg/help/common"
"go.bbkane.com/warg/path"
"go.uber.org/zap"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
//go:embed embedded/grabbit.yaml
var embeddedConfig []byte
func editConfig(ctx command.Context) error {
// retrieve types:
lumberJackLogger := &lumberjack.Logger{
Filename: ctx.Flags["--log-filename"].(path.Path).MustExpand(),
MaxAge: ctx.Flags["--log-maxage"].(int),
MaxBackups: ctx.Flags["--log-maxbackups"].(int),
MaxSize: ctx.Flags["--log-maxsize"].(int),
LocalTime: true,
Compress: false,
}
color, err := common.ConditionallyEnableColor(ctx.Flags, os.Stdout)
if err != nil {
fmt.Fprintf(os.Stderr, "Error enabling color, continuing without: %s", err.Error())
}
zapLogger := logos.NewBBKaneZapLogger(lumberJackLogger, zap.DebugLevel, version)
logger := logos.New(zapLogger, color)
logger.LogOnPanic()
configPath, configPathExists := ctx.Flags["--config"]
if !configPathExists {
err := errors.New("must path --config")
logger.Errorw(
"Must pass --config",
"err", err,
)
return err
}
editor := ctx.Flags["--editor"].(string)
err = glib.EditFile(embeddedConfig, configPath.(path.Path).MustExpand(), editor)
if err != nil {
logger.Errorw(
"Unable to edit config",
"configPath", configPath,
"editorPath", editor,
"err", err,
)
return err
}
err = logger.Sync()
if err != nil {
return fmt.Errorf("could not sync logger: %w", err)
}
return nil
}