-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
98 lines (81 loc) · 2.52 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
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
94
95
96
97
98
// Copyright 2013 Joe Walnes and the websocketd team.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"ablyD/libablyd"
"github.com/rs/xid"
)
type Config struct {
MaxForks int // Number of allowable concurrent forks
LogLevel libablyd.LogLevel
ServerID string
ChannelNamespace string
ChannelPrefix string // Ably channel prefix to use
*libablyd.ProcessConfig
}
func parseCommandLine() *Config {
var mainConfig Config
var config libablyd.ProcessConfig
flag.Usage = func() {}
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
// config options
versionFlag := flag.Bool("version", false, "Print version and exit")
logLevelFlag := flag.String("loglevel", "access", "Log level, one of: debug, trace, access, info, error, fatal")
maxForksFlag := flag.Int("maxforks", 20, "Max forks, zero means unlimited")
channelNamespace := flag.String("namespace", "ablyd", "Ably Channel Namespace")
serverID := flag.String("serverid", xid.New().String(), "Unique ID for the server")
err := flag.CommandLine.Parse(os.Args[1:])
if err != nil {
if err == flag.ErrHelp {
PrintHelp()
os.Exit(0)
} else {
ShortHelp()
os.Exit(2)
}
}
mainConfig.ServerID = *serverID
mainConfig.ChannelNamespace= *channelNamespace
mainConfig.ChannelPrefix = *channelNamespace + ":" + *serverID + ":"
mainConfig.MaxForks = *maxForksFlag
mainConfig.LogLevel = libablyd.LevelFromString(*logLevelFlag)
if mainConfig.LogLevel == libablyd.LogUnknown {
fmt.Printf("Incorrect loglevel flag '%s'. Use --help to see allowed values.\n", *logLevelFlag)
ShortHelp()
os.Exit(1)
}
if len(os.Args) == 1 {
fmt.Printf("Command line arguments are missing.\n")
ShortHelp()
os.Exit(1)
}
if *versionFlag {
fmt.Printf("%s %s\n", HelpProcessName(), Version())
os.Exit(0)
}
args := flag.Args()
if len(args) < 1 {
fmt.Fprintf(os.Stderr, "Please specify COMMAND or provide --dir, --staticdir or --cgidir argument.\n")
ShortHelp()
os.Exit(1)
}
if len(args) > 0 {
if path, err := exec.LookPath(args[0]); err == nil {
config.CommandName = path // This can be command in PATH that we are able to execute
config.CommandArgs = flag.Args()[1:]
// config.UsingScriptDir = false
} else {
fmt.Fprintf(os.Stderr, "Unable to locate specified COMMAND '%s' in OS path.\n", args[0])
ShortHelp()
os.Exit(1)
}
}
mainConfig.ProcessConfig = &config
return &mainConfig
}