-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (82 loc) · 2.16 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main // import "github.com/doovemax/assh"
import (
"fmt"
"os"
"path"
"github.com/pkg/errors"
"github.com/urfave/cli"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/doovemax/assh/pkg/commands"
loggerpkg "github.com/doovemax/assh/pkg/logger"
"github.com/doovemax/assh/pkg/version"
)
func main() {
app := cli.NewApp()
app.Name = path.Base(os.Args[0])
app.Author = "Manfred Touron"
app.Email = "https://github.com/moul/assh"
app.Version = version.VERSION + " (" + version.GITCOMMIT + ")"
app.Usage = "advanced ssh config"
app.EnableBashCompletion = true
app.BashComplete = BashComplete
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "config, c",
EnvVar: "ASSH_CONFIG",
Value: "~/.ssh/assh.yml",
Usage: "Location of config file",
},
cli.BoolFlag{
Name: "debug, D",
EnvVar: "ASSH_DEBUG",
Usage: "Enable debug mode",
},
cli.BoolFlag{
Name: "verbose, V",
Usage: "Enable verbose mode",
},
}
app.Before = hookBefore
app.Commands = commands.Commands
if err := app.Run(os.Args); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
// BashComplete is used bu urfave/cli to allow dynamic bash completion.
func BashComplete(c *cli.Context) {
if len(c.Args()) == 0 {
for _, option := range []string{"--debug", "--verbose", "--help", "--version"} {
fmt.Println(option)
}
for _, command := range []string{"connect", "config", "info", "sockets", "help"} {
fmt.Println(command)
}
}
}
func hookBefore(c *cli.Context) error {
if c.Bool("debug") {
if err := os.Setenv("ASSH_DEBUG", "1"); err != nil {
return err
}
}
return initLogging(c.Bool("debug"), c.Bool("verbose"))
}
func initLogging(debug bool, verbose bool) error {
config := zap.NewDevelopmentConfig()
config.Level.SetLevel(loggerpkg.MustLogLevel(debug, verbose))
if !debug {
config.DisableStacktrace = true
config.DisableCaller = true
config.EncoderConfig.TimeKey = ""
config.EncoderConfig.NameKey = ""
}
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
l, err := config.Build()
if err != nil {
return errors.Wrap(err, "failed to initialize logger")
}
zap.ReplaceGlobals(l)
return nil
}