Skip to content

Commit

Permalink
Move flags inside GoXel package
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickael committed Apr 21, 2019
1 parent c27163f commit 1c4cf17
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 77 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ $(OUTPUT)/goxel: $(SOURCES)
deps:
$(GOGET) github.com/dustin/go-humanize
$(GOGET) golang.org/x/net/proxy
$(GOGET) github.com/spf13/pflag

clean:
$(GO) clean -x
Expand Down
71 changes: 70 additions & 1 deletion goxel/goxel.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,23 @@ import (
"math"
"net/http"
"os"
"strings"
"sync"
"time"

"github.com/dustin/go-humanize"

flag "github.com/spf13/pflag"
)

var activeConnections counter
var goxel *GoXel

const (
version = 0.10
usageMsg string = "goxel [options] [url1] [url2] [url...]\n"
)

// GoXel structure contains all the parameters to be used for the GoXel accelerator
// Credentials can either be passed in command line arguments or using the following environment variables:
// - GOXEL_ALLDEBRID_USERNAME
Expand All @@ -28,9 +36,70 @@ type GoXel struct {
URLs []string
}

// NewGoXel builds a GoXel instance based on the command line arguments
func NewGoXel() *GoXel {
goxel = &GoXel{}

flag.IntVarP(&goxel.MaxConnectionsPerFile, "max-conn-file", "m", 4, "Max number of connections per file")
flag.IntVar(&goxel.MaxConnections, "max-conn", 8, "Max number of connections")

flag.StringVarP(&goxel.InputFile, "file", "f", "", "File containing links to download (1 per line)")
flag.StringVarP(&goxel.OutputDirectory, "output", "o", "", "Output directory")

flag.BoolVar(&goxel.IgnoreSSLVerification, "insecure", false, "Bypass SSL validation")
flag.BoolVar(&goxel.OverwriteOutputFile, "overwrite", false, "Overwrite existing file(s)")

flag.BoolVarP(&goxel.Quiet, "quiet", "q", false, "No stdout output")
flag.StringVarP(&goxel.Proxy, "proxy", "p", "", "Proxy string: (http|https|socks5)://0.0.0.0:0000")
flag.IntVar(&goxel.BufferSize, "buffer-size", 256, "Buffer size in KB")
flag.BoolVarP(&goxel.Scroll, "scroll", "s", false, "Scroll output instead of in place display")

noresume := flag.Bool("no-resume", false, "Don't resume downloads")

flag.StringVar(&goxel.AlldebridLogin, "alldebrid-username", "", "Alldebrid username, can also be passed in the GOXEL_ALLDEBRID_USERNAME environment variable")
flag.StringVar(&goxel.AlldebridPassword, "alldebrid-password", "", "Alldebrid password, can also be passed in the GOXEL_ALLDEBRID_PASSWD environment variable")

versionFlag := flag.Bool("version", false, "Version")

var h headerFlag
flag.Var(&h, "header", "Extra header(s)")

help := flag.BoolP("help", "h", false, "This information")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, usageMsg)
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nVisit https://github.com/m1ck43l/goxel/issues to report bugs.\n")
}

flag.Parse()
goxel.URLs = flag.Args()

if *help {
flag.Usage()
os.Exit(0)
}

if *versionFlag {
fmt.Printf("GoXel v%.1f\n", version)
os.Exit(0)
}

// headers must be transformed to match a map[string]string
goxel.Headers = make(map[string]string)
for _, header := range h {
split := strings.Split(header, "=")
goxel.Headers[split[0]] = split[1]
}

// Resume must be inverted
goxel.Resume = !*noresume

return goxel
}

// Run starts the downloading process
func (g *GoXel) Run() {
goxel = g
activeConnections = counter{}

if g.IgnoreSSLVerification {
Expand Down
18 changes: 18 additions & 0 deletions goxel/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ func fmtDuration(d uint64) string {
return fmt.Sprintf("%02d:%02d:%02d", h, m, s)
}

// headerFlag is used to parse headers on the CLI
// It allows multiple elements to be passed
type headerFlag []string

func (h *headerFlag) String() string {
return fmt.Sprintf("%v", *h)
}

func (h *headerFlag) Set(value string) error {
*h = append(*h, value)
return nil
}

func (h *headerFlag) Type() string {
return "header-name=header-value"
}

// counter allows for an atomic counter
type counter struct {
v int
mux sync.Mutex
Expand Down
78 changes: 2 additions & 76 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,90 +1,16 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"strings"

"github.com/m1ck43l/goxel/goxel"
)

const (
version = 0.10
usageMsg string = "goxel [options] [url1] [url2] [url...]\n"
)

type arrayFlags []string

func (i *arrayFlags) String() string {
return fmt.Sprintf("%v", *i)
}

func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}

func main() {
log.SetOutput(ioutil.Discard)

nbrPerFile := flag.Int("max-conn-file", 4, "Max number of connections per file")
nbrConnexion := flag.Int("max-conn", 8, "Max number of connections")
inputFile := flag.String("file", "", "Links file")
outputDirectory := flag.String("output", "", "Output directory")
ignoreSSLVerification := flag.Bool("insecure", false, "Bypass SSL validation")
overwriteOutputFile := flag.Bool("overwrite", false, "Overwrite existing file(s)")
quiet := flag.Bool("quiet", false, "No stdout output")
proxy := flag.String("proxy", "", "Proxy string: (http|https|socks5)://0.0.0.0:0000")
bufferSize := flag.Int("buffer-size", 256, "Buffer size in KB")
scroll := flag.Bool("scroll", false, "Scroll output instead of in place display")
noResume := flag.Bool("no-resume", false, "Don't resume downloads")

alldebridLogin := flag.String("alldebrid-username", "", "Alldebrid username, can also be passed in the GOXEL_ALLDEBRID_USERNAME environment variable")
alldebridPassword := flag.String("alldebrid-password", "", "Alldebrid password, can also be passed in the GOXEL_ALLDEBRID_PASSWD environment variable")

versionFlag := flag.Bool("version", false, "Version")

var headersFlag arrayFlags
flag.Var(&headersFlag, "header", "Extra header(s), key=value")

flag.Usage = func() {
fmt.Fprintf(os.Stderr, usageMsg)
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nVisit https://github.com/m1ck43l/goxel/issues to report bugs.\n")
}
flag.Parse()
urls := flag.Args()

if *versionFlag {
fmt.Printf("GoXel v%.1f\n", version)
os.Exit(0)
}

headers := make(map[string]string)
for _, header := range headersFlag {
split := strings.Split(header, "=")
headers[split[0]] = split[1]
}

goxel := goxel.GoXel{
URLs: urls,
Headers: headers,
IgnoreSSLVerification: *ignoreSSLVerification,
OutputDirectory: *outputDirectory,
InputFile: *inputFile,
MaxConnections: *nbrConnexion,
MaxConnectionsPerFile: *nbrPerFile,
OverwriteOutputFile: *overwriteOutputFile,
Quiet: *quiet,
Proxy: *proxy,
AlldebridLogin: *alldebridLogin,
AlldebridPassword: *alldebridPassword,
BufferSize: *bufferSize,
Scroll: *scroll,
Resume: !*noResume,
}
// Create a new GoXel instance and run it.
goxel := goxel.NewGoXel()
goxel.Run()
}

0 comments on commit 1c4cf17

Please sign in to comment.