...

Source file src/github.com/theupdateframework/go-tuf/cmd/tuf-client/get.go

Documentation: github.com/theupdateframework/go-tuf/cmd/tuf-client

     1  package main
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  
     7  	"github.com/flynn/go-docopt"
     8  	tuf "github.com/theupdateframework/go-tuf/client"
     9  	"github.com/theupdateframework/go-tuf/util"
    10  )
    11  
    12  func init() {
    13  	register("get", cmdGet, `
    14  usage: tuf-client get [-s|--store=<path>] <url> <target>
    15  
    16  Options:
    17    -s <path>    The path to the local file store [default: tuf.db]
    18  
    19  Get a target from the repository.
    20    `)
    21  }
    22  
    23  type tmpFile struct {
    24  	*os.File
    25  }
    26  
    27  func (t *tmpFile) Delete() error {
    28  	t.Close()
    29  	return os.Remove(t.Name())
    30  }
    31  
    32  func cmdGet(args *docopt.Args, client *tuf.Client) error {
    33  	if _, err := client.Update(); err != nil {
    34  		return err
    35  	}
    36  	target := util.NormalizeTarget(args.String["<target>"])
    37  	file, err := os.CreateTemp("", "go-tuf")
    38  	if err != nil {
    39  		return err
    40  	}
    41  	tmp := tmpFile{file}
    42  	if err := client.Download(target, &tmp); err != nil {
    43  		return err
    44  	}
    45  	defer tmp.Delete()
    46  	if _, err := tmp.Seek(0, io.SeekStart); err != nil {
    47  		return err
    48  	}
    49  	_, err = io.Copy(os.Stdout, file)
    50  	return err
    51  }
    52  

View as plain text