diff options
| author | Jeff Carr <[email protected]> | 2019-06-06 20:02:53 -0700 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2019-06-06 20:02:53 -0700 |
| commit | 93d3e13acc00e5b2e6f3dcc4ae6e12ef86b63e8b (patch) | |
| tree | 8bbd10c54a2cbea54d707eb4c6bfda1609e4a681 | |
| parent | 758e7d157b0a6191f83a5756d561a62a8142010f (diff) | |
move wget into here
Signed-off-by: Jeff Carr <[email protected]>
| -rw-r--r-- | chomp.go | 18 | ||||
| -rw-r--r-- | wget.go | 78 |
2 files changed, 93 insertions, 3 deletions
@@ -54,10 +54,22 @@ func Chomp(a interface{}) string { return perlChomp(s) case []uint8: log.Printf("shell.Chomp() FOUND []uint8") - var u8s []uint8 - u8s = a.([]uint8) + var tmp []uint8 + tmp = a.([]uint8) - s := string(u8s) + s := string(tmp) + return perlChomp(s) + case uint64: + log.Printf("shell.Chomp() FOUND []uint64") + var tmp uint64 + tmp = a.(uint64) + s := string(tmp) + return perlChomp(s) + case int64: + log.Printf("shell.Chomp() FOUND []uint64") + var tmp int64 + tmp = a.(int64) + s := string(tmp) return perlChomp(s) case *bytes.Buffer: log.Printf("shell.Chomp() FOUND *bytes.Buffer") @@ -0,0 +1,78 @@ +package shell + +/* + This simply parses the command line arguments using the default golang + package called 'flag'. This can be used as a simple template to parse + command line arguments in other programs. + + It puts everything in a 'config' Protobuf which I think is a good + wrapper around the 'flags' package and doesn't need a whole mess of + global variables +*/ + +import "io" +import "os" +import "bytes" +import "strings" +import "net/http" + +/* +import "git.wit.com/wit/shell" +import "github.com/davecgh/go-spew/spew" +*/ + +func Wget(url string) (*bytes.Buffer) { + buf := new(bytes.Buffer) + + // Get the data + resp, err := http.Get(url) + if err != nil { + handleError(err, -1) + return nil + } + defer resp.Body.Close() + + buf.ReadFrom(resp.Body) + return buf +} + +func WgetToFile(filepath string, url string) error { + // Get the data + resp, err := http.Get(url) + if err != nil { + handleError(err, -1) + return err + } + defer resp.Body.Close() + + // Create the file + out, err := os.Create(filepath) + if err != nil { + handleError(err, -1) + return err + } + defer out.Close() + + // Write the body to file + _, err = io.Copy(out, resp.Body) + return err +} + +func Write(filepath string, data string) bool { + // Create the file + out, err := os.Create(filepath) + if err != nil { + return false + } + defer out.Close() + + // Write the body to file + // _, err = io.Copy(out, resp.Body) + count, err := io.Copy(out, strings.NewReader(data)) + if err != nil { + handleError(err, -1) + return false + } + handleError(nil, int(count)) + return true +} |
