Skip to content

Commit

Permalink
Allow adding CID to existing files
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed Sep 22, 2024
1 parent 4c69a5d commit c57928f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
19 changes: 17 additions & 2 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
StatusDownloaded Status = "downloaded"
StatusSkipped Status = "skipped"
StatusUsedCache Status = "used-cache"
StatusUsedIPFS Status = "used-ipfs"
)

type Result struct {
Expand All @@ -55,6 +56,7 @@ type options struct {
decompress bool // default: false (keep compression)
description string // default: url
expectedDigest digest.Digest
cid string
}

type Opt func(*options) error
Expand Down Expand Up @@ -121,6 +123,13 @@ func WithExpectedDigest(expectedDigest digest.Digest) Opt {
}
}

func WithContentIdentifier(cid string) Opt {
return func(o *options) error {
o.cid = cid
return nil
}
}

func readFile(path string) string {
if path == "" {
return ""
Expand Down Expand Up @@ -273,11 +282,17 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
if err := os.WriteFile(shadURL, []byte(remote), 0o644); err != nil {
return nil, err
}
status := StatusDownloaded
if o.cid != "" {
if err := downloadIPFS(ctx, shadData, fmt.Sprintf("ipfs://%s", o.cid), o.description, o.expectedDigest); err == nil {
status = StatusUsedIPFS
}
}
if IsIPFS(remote) {
if err := downloadIPFS(ctx, shadData, remote, o.description, o.expectedDigest); err != nil {
return nil, err
}
} else {
} else if status != StatusUsedIPFS {
if err := downloadHTTP(ctx, shadData, shadTime, shadType, remote, o.description, o.expectedDigest); err != nil {
return nil, err
}
Expand All @@ -292,7 +307,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
}
}
res := &Result{
Status: StatusDownloaded,
Status: status,
CachePath: shadData,
LastModified: readTime(shadTime),
ContentType: readFile(shadType),
Expand Down
3 changes: 3 additions & 0 deletions pkg/fileutils/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func DownloadFile(ctx context.Context, dest string, f limayaml.File, decompress
downloader.WithDecompress(decompress),
downloader.WithDescription(fmt.Sprintf("%s (%s)", description, path.Base(f.Location))),
downloader.WithExpectedDigest(f.Digest),
downloader.WithContentIdentifier(f.Cid),
)
if err != nil {
return "", fmt.Errorf("failed to download %q: %w", f.Location, err)
Expand All @@ -36,6 +37,8 @@ func DownloadFile(ctx context.Context, dest string, f limayaml.File, decompress
logrus.Infof("Downloaded %s from %q", description, f.Location)
case downloader.StatusUsedCache:
logrus.Infof("Using cache %q", res.CachePath)
case downloader.StatusUsedIPFS:
logrus.Infof("Used ipfs %q", f.Cid)
default:
logrus.Warnf("Unexpected result from downloader.Download(): %+v", res)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/limayaml/limayaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type File struct {
Location string `yaml:"location" json:"location"` // REQUIRED
Arch Arch `yaml:"arch,omitempty" json:"arch,omitempty"`
Digest digest.Digest `yaml:"digest,omitempty" json:"digest,omitempty"`
Cid string `yaml:"cid,omitempty" json:"cid,omitempty"`
}

type FileWithVMType struct {
Expand Down
13 changes: 13 additions & 0 deletions pkg/limayaml/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -39,6 +40,18 @@ func validateFileObject(f File, fieldName string) error {
return fmt.Errorf("field `%s.digest` is invalid: %s: %w", fieldName, f.Digest.String(), err)
}
}
if f.Cid != "" {
if _, err := exec.LookPath("ipfs"); err == nil {
cmd := exec.Command("ipfs", "cid", "format", f.Cid)
if cid, err := cmd.CombinedOutput(); err != nil || strings.TrimSuffix(string(cid), "\n") != f.Cid {
// unfortunately, the `ipfs cid` command does not return any proper exit codes
if err == nil {
return fmt.Errorf("field `%s.cid` is invalid: %s", fieldName, string(cid))
}
return fmt.Errorf("field `%s.cid` is invalid: %s: %w", fieldName, f.Cid, err)
}
}
}
return nil
}

Expand Down

0 comments on commit c57928f

Please sign in to comment.