-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: split flag setup, usage reporting etc
- Loading branch information
Showing
7 changed files
with
188 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//go:build ignore | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"flag" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/mgumz/knut/internal/pkg/knut" | ||
) | ||
|
||
func main() { | ||
|
||
oname := flag.String("o", "", "name of generated file") | ||
flag.Parse() | ||
|
||
ofile := os.Stdout | ||
if *oname != "" { | ||
ofile, _ = os.Create(*oname) | ||
defer ofile.Close() | ||
} | ||
|
||
kf := flag.NewFlagSet("knut", flag.ContinueOnError) | ||
knut.SetupFlags(kf) | ||
b := bytes.NewBuffer(nil) | ||
kf.SetOutput(b) | ||
kf.Usage() | ||
fmt.Fprintln(ofile, "// generated, do not edit.") | ||
fmt.Fprintln(ofile) | ||
fmt.Fprintln(ofile, "package main") | ||
fmt.Fprintln(ofile) | ||
fmt.Fprintln(ofile, "/*") | ||
fmt.Fprintln(ofile, string(b.Bytes())) | ||
fmt.Fprintln(ofile, "*/") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code | ||
// is governed by a BSD-style license that can be found in the LICENSE file. | ||
|
||
package knut | ||
|
||
import "flag" | ||
|
||
type Opts struct { | ||
BindAddr string | ||
DoLog bool | ||
DoAuth string | ||
DoCompress bool | ||
DoPrintVersion bool | ||
AddServerID string | ||
TlsOnetime bool | ||
TlsCert string | ||
TlsKey string | ||
} | ||
|
||
func SetupFlags(f *flag.FlagSet) *Opts { | ||
|
||
opts := Opts{ | ||
BindAddr: ":8080", | ||
DoLog: true, | ||
DoCompress: true, | ||
AddServerID: "knut/" + Version, | ||
} | ||
|
||
f.StringVar(&opts.BindAddr, "bind", opts.BindAddr, "address to bind to") | ||
f.BoolVar(&opts.DoLog, "log", opts.DoLog, "log requests to stdout") | ||
f.BoolVar(&opts.DoCompress, "compress", opts.DoCompress, `handle "Accept-Encoding" = "gzip,deflate"`) | ||
f.StringVar(&opts.DoAuth, "auth", "", "use 'name:password' to require") | ||
f.StringVar(&opts.AddServerID, "server-id", opts.AddServerID, `add "Server: <val-here>" to the response`) | ||
f.BoolVar(&opts.TlsOnetime, "tls-onetime", opts.TlsOnetime, "use a onetime-in-memory cert+key to drive tls") | ||
f.StringVar(&opts.TlsKey, "tls-key", opts.TlsKey, "use given key to start tls") | ||
f.StringVar(&opts.TlsCert, "tls-cert", opts.TlsCert, "use given cert to start tls") | ||
f.BoolVar(&opts.DoPrintVersion, "version", opts.DoPrintVersion, "print version") | ||
f.Usage = func() { printUsage(f) } | ||
|
||
return &opts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2015 Mathias Gumz. All rights reserved. Use of this source code | ||
// is governed by a BSD-style license that can be found in the LICENSE file. | ||
|
||
package knut | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
) | ||
|
||
const usageText = ` | ||
knut [opts] [uri:]folder-or-file [mapping2] [mapping3] [...] | ||
Sample: | ||
knut file.txt /this/:. /ding.txt:/tmp/dong.txt | ||
Mapping Format: | ||
file.txt - publish the file "file.txt" via "/file.txt" | ||
/:. - list contents of current directory via "/" | ||
/uri:folder - list contents of "folder" via "/uri" | ||
/uri:file - serve "file" via "/uri" | ||
/uri:@text - respond with "text" at "/uri" | ||
30x/uri:location - respond with 301 at "/uri" | ||
@/upload:folder - accept multipart encoded data via POST at "/upload" | ||
and store it inside "folder". A simple upload form | ||
is rendered on GET. | ||
/c.tgz:tar+gz://./ - creates a (gzipped) tarball from the current directory | ||
and serves it via "/c.tgz" | ||
/z.zip:zip://./ - creates a zip files from the current directory | ||
and serves it via "/z.zip" | ||
/z.zip:zipfs://a.zip - list and servce the content of the entries of an | ||
existing "z.zip" via the "/z.zip": consider a file | ||
"example.txt" inside "z.zip", it will be directly | ||
available via "/z.zip/example.txt" | ||
/uri:http://1.2.3.4/ - creates a reverse proxy and forwards requests to /uri | ||
to the given http-host | ||
/uri:git://folder/ - serves files via "git http-backend" | ||
/uri:cgit://path/to/dir - serves git-repos via "cgit" | ||
/uri:myip:// - serves a "myip" endpoint | ||
` | ||
|
||
func printUsage(fs *flag.FlagSet) { | ||
w := fs.Output() | ||
fmt.Fprintln(w, usageText, "Options:") | ||
fmt.Fprintln(w) | ||
fs.PrintDefaults() | ||
fmt.Fprintln(w) | ||
} |
Oops, something went wrong.