Skip to content

Commit

Permalink
Work around Fiber hijacking flags
Browse files Browse the repository at this point in the history
  • Loading branch information
doingodswork committed Jun 30, 2020
1 parent 3c46278 commit a57d3d4
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
6 changes: 6 additions & 0 deletions addon.go
Expand Up @@ -2,6 +2,7 @@ package stremio

import (
"errors"
"flag"
"fmt"
netpprof "net/http/pprof"
"os"
Expand Down Expand Up @@ -93,6 +94,11 @@ type Addon struct {
logger *zap.Logger
}

func init() {
// We need to overwrite the usage of the default FlagSet to hide the flags defined by Fiber
flag.CommandLine.Usage = usage
}

// NewAddon creates a new Addon object that can be started with Run().
// A proper manifest must be supplied, but all but one handler can be nil in case you only want to handle specific requests and opts can be the zero value of Options.
func NewAddon(manifest Manifest, catalogHandlers map[string]CatalogHandler, streamHandlers map[string]StreamHandler, opts Options) (Addon, error) {
Expand Down
81 changes: 81 additions & 0 deletions flags.go
@@ -0,0 +1,81 @@
package stremio

import (
"flag"
"fmt"
"os"
"reflect"
"strings"
)

// -- string Value
// This code is copied from the stdlib.
type stringValue string

// This code is copied from the stdlib.
func (s *stringValue) Set(val string) error {
*s = stringValue(val)
return nil
}

// This code is copied from the stdlib.
func (s *stringValue) Get() interface{} { return string(*s) }

// This code is copied from the stdlib.
func (s *stringValue) String() string { return string(*s) }

// usage prints the usage of the flags an SDK user sets.
// It skips printing Fiber's `-prefork` and `-child`, that it defines as of Fiber v1.12.0.
// This code is based on the stdlib with the only change to skip those two flags.
func usage() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.CommandLine.VisitAll(func(f *flag.Flag) {
// Skip printing usage info for Fiber's flags
if f.Name == "prefork" || f.Name == "child" {
return
}

s := fmt.Sprintf(" -%s", f.Name) // Two spaces before -; see next two comments.
name, usage := flag.UnquoteUsage(f)
if len(name) > 0 {
s += " " + name
}
// Boolean flags of one ASCII letter are so common we
// treat them specially, putting their usage on the same line.
if len(s) <= 4 { // space, space, '-', 'x'.
s += "\t"
} else {
// Four spaces before the tab triggers good alignment
// for both 4- and 8-space tab stops.
s += "\n \t"
}
s += strings.ReplaceAll(usage, "\n", "\n \t")

if !isZeroValue(f, f.DefValue) {
if _, ok := f.Value.(*stringValue); ok {
// put quotes on the value
s += fmt.Sprintf(" (default %q)", f.DefValue)
} else {
s += fmt.Sprintf(" (default %v)", f.DefValue)
}
}
fmt.Fprint(flag.CommandLine.Output(), s, "\n")
})
}

// isZeroValue determines whether the string represents the zero
// value for a flag.
// This code is copied from the stdlib.
func isZeroValue(f *flag.Flag, value string) bool {
// Build a zero value of the flag's Value type, and see if the
// result of calling its String method equals the value passed in.
// This works unless the Value type is itself an interface type.
typ := reflect.TypeOf(f.Value)
var z reflect.Value
if typ.Kind() == reflect.Ptr {
z = reflect.New(typ.Elem())
} else {
z = reflect.Zero(typ)
}
return value == z.Interface().(flag.Value).String()
}

0 comments on commit a57d3d4

Please sign in to comment.