[#733] neofs-adm: read contracts on start

Fail early and reduce disk operations when reading from archive.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-10-25 16:19:56 +03:00 committed by Alex Vanin
parent 0b6350d463
commit 088c894f44
6 changed files with 105 additions and 107 deletions

View file

@ -6,18 +6,17 @@ import (
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/google/go-github/v39/github"
"github.com/spf13/cobra"
)
func downloadContractsFromGithub(cmd *cobra.Command) (string, error) {
func downloadContractsFromGithub(cmd *cobra.Command) (io.ReadCloser, error) {
gcl := github.NewClient(nil)
release, _, err := gcl.Repositories.GetLatestRelease(context.Background(), "nspcc-dev", "neofs-contract")
if err != nil {
return "", fmt.Errorf("can't fetch release info: %w", err)
return nil, fmt.Errorf("can't fetch release info: %w", err)
}
cmd.Printf("Found %s (%s), downloading...\n", release.GetTagName(), release.GetName())
@ -30,21 +29,12 @@ func downloadContractsFromGithub(cmd *cobra.Command) (string, error) {
}
}
if url == "" {
return "", errors.New("can't find contracts archive in release assets")
return nil, errors.New("can't find contracts archive in release assets")
}
resp, err := http.Get(url)
if err != nil {
return "", fmt.Errorf("can't fetch contracts archive: %w", err)
return nil, fmt.Errorf("can't fetch contracts archive: %w", err)
}
defer resp.Body.Close()
f, err := os.CreateTemp("", "neofs-contract-*.tar.gz")
if err != nil {
return "", fmt.Errorf("can't allocate temporary file: %w", err)
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
return f.Name(), err
return resp.Body, nil
}