github.com/rzurga/go-swagger@v0.28.1-0.20211109195225-5d1f453ffa3a/examples/file-server/upload_file.go (about)

     1  // +build ignore
     2  
     3  package main
     4  
     5  import (
     6  	"log"
     7  	"os"
     8  
     9  	"github.com/go-openapi/runtime"
    10  	"github.com/go-swagger/go-swagger/examples/file-server/client"
    11  	"github.com/go-swagger/go-swagger/examples/file-server/client/uploads"
    12  )
    13  
    14  func main() {
    15  	if len(os.Args) < 2 {
    16  		log.Fatalf("must provide a file name as argument")
    17  	}
    18  
    19  	filename := os.Args[1]
    20  
    21  	f, err := os.Open(filename)
    22  	if err != nil {
    23  		log.Fatal(err)
    24  	}
    25  
    26  	err = upload(f)
    27  	if err != nil {
    28  		log.Fatal(err)
    29  	}
    30  }
    31  
    32  func upload(reader runtime.NamedReadCloser) error {
    33  
    34  	config := client.DefaultTransportConfig().WithHost("localhost:8000")
    35  
    36  	uploader := client.NewHTTPClientWithConfig(nil, config)
    37  
    38  	params := uploads.NewUploadFileParams().WithFile(reader)
    39  
    40  	_, err := uploader.Uploads.UploadFile(params)
    41  
    42  	return err
    43  }