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
|
package main
import (
"context"
"flag"
"io/ioutil"
"os"
"strings"
"github.com/mimecast/dtail/internal/clients"
"github.com/mimecast/dtail/internal/color"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/logger"
"github.com/mimecast/dtail/internal/user"
"github.com/mimecast/dtail/internal/version"
)
// The evil begins here.
func main() {
var args clients.Args
var background string
var cfgFile string
var command string
var debugEnable bool
var displayVersion bool
var jobName string
var noColor bool
var quietEnable bool
var sshPort int
userName := user.Name()
flag.BoolVar(&args.TrustAllHosts, "trustAllHosts", false, "Auto trust all unknown host keys")
flag.BoolVar(&debugEnable, "debug", false, "Activate debug messages")
flag.BoolVar(&displayVersion, "version", false, "Display version")
flag.BoolVar(&noColor, "noColor", false, "Disable ANSII terminal colors")
flag.BoolVar(&quietEnable, "quiet", false, "Reduce output")
flag.IntVar(&args.ConnectionsPerCPU, "cpc", 10, "How many connections established per CPU core concurrently")
flag.IntVar(&args.Timeout, "timeout", 0, "Command execution timeout")
flag.IntVar(&sshPort, "port", 2222, "SSH server port")
flag.StringVar(&args.Discovery, "discovery", "", "Server discovery method")
flag.StringVar(&args.PrivateKeyPathFile, "key", "", "Path to private key")
flag.StringVar(&args.ServersStr, "servers", "", "Remote servers to connect")
flag.StringVar(&args.UserName, "user", userName, "Your system user name")
flag.StringVar(&background, "background", "", "Can be one of 'start', 'cancel', 'list' or empty")
flag.StringVar(&cfgFile, "cfg", "", "Config file path")
flag.StringVar(&command, "command", "", "Command to run")
flag.StringVar(&jobName, "name", "", "The job name (if run in background)")
flag.Parse()
config.Read(cfgFile, sshPort)
color.Colored = !noColor
if displayVersion {
version.PrintAndExit()
}
ctx := context.TODO()
logger.Start(ctx, logger.Modes{Debug: debugEnable || config.Common.DebugEnable, Quiet: quietEnable})
args.What, args.Arguments = readCommand(command)
client, err := clients.NewRunClient(args, background, jobName)
if err != nil {
panic(err)
}
status := client.Start(ctx)
logger.Flush()
os.Exit(status)
}
func readCommand(command string) (string, []string) {
splitted := strings.Split(command, " ")
script := splitted[0]
if _, err := os.Stat(script); os.IsNotExist(err) {
var commandArgs []string
return command, commandArgs
}
commandArgs := splitted[1:]
bytes, err := ioutil.ReadFile(script)
if err != nil {
panic(err)
}
return string(bytes), commandArgs
}
|