-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.go
64 lines (53 loc) · 2.21 KB
/
init.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
package main
import (
"fmt"
"log"
"os"
"github.com/spf13/pflag"
"github.com/spf13/viper"
)
// GitCommit holds the git commit message on compile time
var GitCommit string
// GitBranch holds the active git branch on compile time
var GitBranch string
// GitState holds the dirty-state on compile time
var GitState string
// BuildDate date of compile
var BuildDate string
// Version version string as contained in VERSION
var Version string
// init parses the command line arguments with pflag and viper
func init() {
pflag.BoolP("help", "h", false, "Prints this help message")
pflag.StringP("output", "o", "./code", "Relative or absolute path to the directory where you want to store user-posted pastes.")
pflag.StringP("domain", "d", "localhost", "This will be used as a prefix for an output received by the client. Value will be prepended with http[s].")
pflag.IntP("port", "p", 9999, "Port in which the service should listen on.")
pflag.BoolP("https", "S", false, fmt.Sprintf("If set, %s returns url with https prefix instead of http.", AppName))
pflag.IntP("buffer", "B", 32768, "This parameter defines size of the buffer used for getting data from the user. Maximum size (in bytes) of all input files is defined by this value.")
pflag.StringP("log", "l", "", "Log file. This file has to be user-writable.")
pflag.BoolP("http", "H", false, "Enable the embedded http daemon.")
pflag.IntP("httpport", "P", 9989, "Sets the port for the embedded http daemon.")
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
if viper.GetBool("help") {
fmt.Printf("%s! - Version %s, Built on %s from Git tag [%s:%s-%s)\n", AppName, Version, BuildDate, GitBranch, GitCommit, GitState)
pflag.Usage()
os.Exit(2)
}
if viper.GetBool("https") {
viper.Set("uriprefix", "https")
} else {
viper.Set("uriprefix", "http")
}
_, err := os.Stat(viper.GetString("output"))
if err != nil {
log.Printf("unable to stat() output directory: %s\n", viper.GetString("output"))
log.Printf("attempting to create %s\n", viper.GetString("output"))
err = os.Mkdir(viper.GetString("output"), os.ModeDir)
if err != nil {
log.Printf("Unable to create directory %s\n", viper.GetString("output"))
log.Fatalf("System error: %s\n", err)
os.Exit(1)
}
}
}