Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSDK-8827 module download command #4353

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions cli/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
moduleFlagLocal = "local"
moduleFlagHomeDir = "home"
moduleCreateLocalOnly = "local-only"
moduleFlagID = "id"

moduleBuildFlagPath = "module"
moduleBuildFlagRef = "ref"
Expand Down Expand Up @@ -1746,6 +1747,32 @@ This won't work unless you have an existing installation of our GitHub app on yo
},
Action: ReloadModuleAction,
},
{
Name: "download",
Usage: "download a module package from the registry",
UsageText: createUsageText("module download", []string{}, false),
Flags: []cli.Flag{
&cli.PathFlag{
Name: packageFlagDestination,
Usage: "output directory for downloaded package",
Value: ".",
},
&cli.StringFlag{
Name: moduleFlagID,
Usage: "module ID as org-id:name or namespace:name. if missing, will try to read from meta.json",
},
&cli.StringFlag{
Name: packageFlagVersion,
Usage: "version of the requested package, can be `latest` to get the most recent version",
Value: "latest",
},
&cli.StringFlag{
Name: moduleFlagPlatform,
Usage: "platform like 'linux/amd64'. if missing, will use platform of the CLI binary",
},
},
Action: DownloadModuleAction,
},
},
},
{
Expand All @@ -1757,30 +1784,25 @@ This won't work unless you have an existing installation of our GitHub app on yo
Name: "export",
Usage: "download a package from Viam cloud",
UsageText: createUsageText("packages export",
[]string{
packageFlagDestination, generalFlagOrgID, packageFlagName,
packageFlagVersion, packageFlagType,
}, false),
[]string{packageFlagType}, false),
Flags: []cli.Flag{
&cli.PathFlag{
Name: packageFlagDestination,
Required: true,
Usage: "output directory for downloaded package",
Name: packageFlagDestination,
Usage: "output directory for downloaded package",
Value: ".",
},
&cli.StringFlag{
Name: generalFlagOrgID,
Required: true,
Usage: "organization ID of the requested package",
Name: generalFlagOrgID,
Usage: "organization ID or namespace of the requested package. if missing, will try to read from meta.json",
},
&cli.StringFlag{
Name: packageFlagName,
Required: true,
Usage: "name of the requested package",
Name: packageFlagName,
Usage: "name of the requested package. if missing, will try to read from meta.json",
},
&cli.StringFlag{
Name: packageFlagVersion,
Required: true,
Usage: "version of the requested package, can be `latest` to get the most recent version",
Name: packageFlagVersion,
Usage: "version of the requested package, can be `latest` to get the most recent version",
Value: "latest",
},
&cli.StringFlag{
Name: packageFlagType,
Expand Down
76 changes: 76 additions & 0 deletions cli/module_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
"math"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"slices"
"strings"

"github.com/google/uuid"
Expand Down Expand Up @@ -877,3 +880,76 @@ func getNextModuleUploadRequest(file *os.File) (*apppb.UploadModuleFileRequest,
},
}, nil
}

// DownloadModuleAction downloads a module.
func DownloadModuleAction(c *cli.Context) error {
moduleID := c.String(moduleFlagID)
if moduleID == "" {
manifest, err := loadManifest(defaultManifestFilename)
if err != nil {
return errors.Wrap(err, "trying to get package ID from meta.json")
}
moduleID = manifest.ModuleID
}
client, err := newViamClient(c)
if err != nil {
return err
}
if err := client.ensureLoggedIn(); err != nil {
return err
}
req := &apppb.GetModuleRequest{ModuleId: moduleID}
res, err := client.client.GetModule(c.Context, req)
if err != nil {
return err
}
if len(res.Module.Versions) == 0 {
return errors.New("module has 0 uploaded versions, nothing to download")
}
requestedVersion := c.String(packageFlagVersion)
var ver *apppb.VersionHistory
if requestedVersion == "latest" {
ver = res.Module.Versions[len(res.Module.Versions)-1]
} else {
for _, iVer := range res.Module.Versions {
if iVer.Version == requestedVersion {
ver = iVer
break
}
}
if ver == nil {
return fmt.Errorf("version %s not found in versions for module", requestedVersion)
}
}
infof(c.App.ErrWriter, "found version %s", ver.Version)
if len(ver.Files) == 0 {
return fmt.Errorf("version %s has 0 files uploaded", ver.Version)
}
platform := c.String(moduleFlagPlatform)
if platform == "" {
platform = fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH)
infof(c.App.ErrWriter, "using default platform %s", platform)
}
if !slices.ContainsFunc(ver.Files, func(file *apppb.Uploads) bool { return file.Platform == platform }) {
return fmt.Errorf("platform %s not present for version %s", platform, ver.Version)
}
include := true
packageType := packagespb.PackageType_PACKAGE_TYPE_MODULE
// note: this is working around a GetPackage quirk where platform messes with version
fullVersion := fmt.Sprintf("%s-%s", ver.Version, strings.ReplaceAll(platform, "/", "-"))
pkg, err := client.packageClient.GetPackage(c.Context, &packagespb.GetPackageRequest{
Id: strings.ReplaceAll(moduleID, ":", "/"),
Version: fullVersion,
IncludeUrl: &include,
Type: &packageType,
})
if err != nil {
return err
}
destName := strings.ReplaceAll(moduleID, ":", "-")
infof(c.App.ErrWriter, "saving to %s", path.Join(c.String(packageFlagDestination), fullVersion, destName+".tar.gz"))
return downloadPackageFromURL(c.Context, client.authFlow.httpClient,
c.String(packageFlagDestination), destName,
fullVersion, pkg.Package.Url, client.conf.Auth,
)
}
10 changes: 10 additions & 0 deletions cli/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ func (c *viamClient) packageExportAction(orgID, name, version, packageType, dest
if err := c.ensureLoggedIn(); err != nil {
return err
}
if orgID == "" || name == "" {
if orgID != "" || name != "" {
return fmt.Errorf("if either of %s or %s is missing, both must be missing", generalFlagOrgID, packageFlagName)
}
manifest, err := loadManifest(defaultManifestFilename)
if err != nil {
return errors.Wrap(err, "trying to get package ID from meta.json")
}
orgID, name, _ = strings.Cut(manifest.ModuleID, ":")
}
// Package ID is the <organization-ID>/<package-name>
packageID := path.Join(orgID, name)
packageTypeProto, err := convertPackageTypeToProto(packageType)
Expand Down
Loading