/* The MIT License (MIT) Copyright © 2020-2025 pacman64 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* Single-file source-code for get. To compile a smaller-sized command-line app, you can use the `go` command as follows: go build -ldflags "-s -w" -trimpath get.go */ package main import ( "bufio" "encoding/base64" "errors" "fmt" "io" "net/http" "os" "strings" ) const info = ` get [files/URIs...] Load all files and/or web-resources given. ` // errNoMoreOutput is a dummy error whose message is ignored, and which // causes the app to quit immediately and successfully var errNoMoreOutput = errors.New(`no more output`) func main() { if len(os.Args) > 1 { switch os.Args[1] { case `-h`, `--h`, `-help`, `--help`: os.Stderr.WriteString(info[1:]) return } } if err := run(os.Args[1:]); err != nil && err != errNoMoreOutput { fmt.Fprintf(os.Stderr, "\x1b[31m%s\x1b[0m\n", err.Error()) os.Exit(1) } } func run(names []string) error { w := bufio.NewWriter(os.Stdout) defer w.Flush() dashes := 0 for _, s := range names { if s == `-` { dashes++ } } var stdin []byte gotStdin := false for _, name := range names { w.Flush() // handle reading from stdin, keeping its data for reuse if needed if name == `-` { if dashes > 1 { if !gotStdin { stdin, _ = io.ReadAll(os.Stdin) gotStdin = true } n, err := w.Write(stdin) if n < len(stdin) || err != nil { return nil } continue } _, err := io.Copy(os.Stdout, os.Stdin) if err != nil { return nil } continue } // handle data-URIs if seemsDataURI(name) { if err := handleDataURI(w, name); err != nil { return err } continue } // handle web-requests and actual files handle := handleFile if seemsURI(name) { handle = handleURI } if err := handle(w, name); err != nil { return err } } // use stdin when no filenames were given if len(names) == 0 { io.Copy(os.Stdout, os.Stdin) } return nil } // handleDataURI decodes base64 explicitly, so decoding errors can be told // apart from output-writing ones func handleDataURI(w *bufio.Writer, data string) error { i := strings.Index(data, `;base64,`) if i < 0 { return errors.New(`invalid data URI`) } r := strings.NewReader(data[i+len(`;base64,`):]) dec := base64.NewDecoder(base64.StdEncoding, r) var buf [16 * 1024]byte for { n, err := dec.Read(buf[:]) if n < 1 && err == io.EOF { return nil } if err != nil { return err } _, err = w.Write(buf[:n]) if err != nil { return errNoMoreOutput } } } func handleFile(w *bufio.Writer, path string) error { f, err := os.Open(path) if err != nil { return err } defer f.Close() _, err = io.Copy(w, f) if err != nil { return errNoMoreOutput } return nil } func handleURI(w *bufio.Writer, uri string) error { r, err := http.Get(uri) if err != nil { return err } defer r.Body.Close() _, err = io.Copy(w, r.Body) if err != nil { return errNoMoreOutput } return nil } func seemsDataURI(s string) bool { start := s if len(s) > 64 { start = s[:64] } return strings.HasPrefix(s, `data:`) && strings.Contains(start, `;base64,`) } func seemsURI(s string) bool { return false || strings.HasPrefix(s, `https://`) || strings.HasPrefix(s, `http://`) }