diff --git a/.travis.yml b/.travis.yml index 7ce48bc..31c6920 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ go: - tip before_install: - - go get -u -v github.com/spf13/pflag github.com/spf13/viper github.com/ahmetb/govvv github.com/golang/lint/golint + - go get -u -v github.com/ahmetb/govvv github.com/golang/lint/golint script: - govvv build \ No newline at end of file diff --git a/init.go b/init.go new file mode 100644 index 0000000..21506e7 --- /dev/null +++ b/init.go @@ -0,0 +1,49 @@ +package main + +import ( + "fmt" + "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.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") + } +} diff --git a/main.go b/main.go index a07dbe2..5403fd9 100644 --- a/main.go +++ b/main.go @@ -6,57 +6,14 @@ import ( "log" "net" "os" - "strconv" "time" - "github.com/spf13/pflag" "github.com/spf13/viper" ) // AppName is the canonical name of this program const AppName = "Go-Fiche" -const slugMap = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789" - -// 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 - -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.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") - } -} - func main() { log.Printf("Starting %s on %s...", AppName, time.Now().Format(time.RFC822Z)) listener, err := net.Listen("tcp", fmt.Sprintf(":%d", viper.Get("port"))) @@ -76,19 +33,6 @@ func main() { } } -func generateSlug(seed int64) string { - stringSeed := fmt.Sprintf("%d", seed) - evenLength := (len(stringSeed) / 2) * 2 - digitHold := int64(0) - out := "" - for i := 0; i < evenLength; i += 2 { - digitHold, _ = strconv.ParseInt(stringSeed[i:i+2], 10, 8) - digitHold = digitHold % int64(len(slugMap)) - out += slugMap[digitHold : digitHold+1] - } - return out -} - func handleConnection(conn net.Conn) { remoteHost, err := net.LookupCNAME(conn.RemoteAddr().String()) if err != nil { diff --git a/slug.go b/slug.go new file mode 100644 index 0000000..c295a68 --- /dev/null +++ b/slug.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "strconv" +) + +// slugMap contains a sequence of valid characters for slug generation +const slugMap = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789" + +// generateSlug takes an integer and generates a string based on slugMap +func generateSlug(seed int64) string { + stringSeed := fmt.Sprintf("%d", seed) + evenLength := (len(stringSeed) / 2) * 2 + digitHold := int64(0) + out := "" + for i := 0; i < evenLength; i += 2 { + digitHold, _ = strconv.ParseInt(stringSeed[i:i+2], 10, 8) + digitHold = digitHold % int64(len(slugMap)) + out += slugMap[digitHold : digitHold+1] + } + return out +}