[#733] neofs-adm: fetch contracts release from Github

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-10-21 18:32:01 +03:00 committed by Alex Vanin
parent c1e1b65ad9
commit 0b6350d463
4 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,50 @@
package morph
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/google/go-github/v39/github"
"github.com/spf13/cobra"
)
func downloadContractsFromGithub(cmd *cobra.Command) (string, 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)
}
cmd.Printf("Found %s (%s), downloading...\n", release.GetTagName(), release.GetName())
var url string
for _, a := range release.Assets {
if strings.HasPrefix(a.GetName(), "neofs-contract") {
url = a.GetBrowserDownloadURL()
break
}
}
if url == "" {
return "", 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)
}
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
}

View file

@ -127,6 +127,14 @@ func newInitializeContext(cmd *cobra.Command, v *viper.Viper) (*initializeContex
if err != nil { if err != nil {
return nil, fmt.Errorf("missing contracts path: %w", err) return nil, fmt.Errorf("missing contracts path: %w", err)
} }
if ctrPath == "" {
cmd.Println("Contracts flag is missing, latest release will be fetched from Github.")
ctrPath, err = downloadContractsFromGithub(cmd)
if err != nil {
return nil, err
}
cmd.Printf("Saved to %s\n", ctrPath)
}
} }
ns, err := c.GetNativeContracts() ns, err := c.GetNativeContracts()

1
go.mod
View file

@ -3,6 +3,7 @@ module github.com/nspcc-dev/neofs-node
go 1.16 go 1.16
require ( require (
github.com/google/go-github/v39 v39.2.0
github.com/google/uuid v1.2.0 github.com/google/uuid v1.2.0
github.com/hashicorp/golang-lru v0.5.4 github.com/hashicorp/golang-lru v0.5.4
github.com/klauspost/compress v1.13.1 github.com/klauspost/compress v1.13.1

BIN
go.sum

Binary file not shown.