Skip to content
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
Empty file.
36 changes: 36 additions & 0 deletions cli/cmd/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cmd

import (
"fmt"

"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/spf13/cobra"
)

// DownloadCmd represents the download command
type DownloadCmd struct {
cmd *cobra.Command
}

func (c *DownloadCmd) RunE(_ *cobra.Command, args []string) error {
//Command execution goes here

fmt.Printf("running %s", c.cmd.Use)

return nil
}

func AddDownloadCmd(rootCmd *cobra.Command, opts GlobalOptions) {
download := DownloadCmd{
cmd: &cobra.Command{
Use: "download",
Short: "Download resources available through OMS",
Long: io.Long(`Download resources managed by or available for OMS,
e.g. available Codesphere packages`),
},
}
rootCmd.AddCommand(download.cmd)
download.cmd.RunE = download.RunE

AddDownloadPackageCmd(download.cmd, opts)
}
87 changes: 87 additions & 0 deletions cli/cmd/download_package.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package cmd

import (
"fmt"

"github.com/codesphere-cloud/cs-go/pkg/io"
"github.com/spf13/cobra"

"github.com/codesphere-cloud/oms/internal/portal"
"github.com/codesphere-cloud/oms/internal/util"
)

// DownloadPackageCmd represents the package command
type DownloadPackageCmd struct {
cmd *cobra.Command
Opts DownloadPackageOpts
FileWriter util.FileWriter
}

type DownloadPackageOpts struct {
GlobalOptions
Version string
Filename string
}

func (c *DownloadPackageCmd) RunE(_ *cobra.Command, args []string) error {
fmt.Printf("Downloading package %s\n", c.Opts.Version)

p := portal.NewPortalClient()
build, err := p.GetCodesphereBuildByVersion(c.Opts.Version)
if err != nil {
return fmt.Errorf("failed to get codesphere package: %w", err)
}

err = c.DownloadBuild(p, build, c.Opts.Filename)
if err != nil {
return fmt.Errorf("failed to download codesphere package: %w", err)
}

return nil
}

func AddDownloadPackageCmd(download *cobra.Command, opts GlobalOptions) {
pkg := DownloadPackageCmd{
cmd: &cobra.Command{
Use: "package",
Short: "Download a codesphere package",
Long: io.Long(`Download a specific version of a Codesphere package
To list available packages, run oms list packages.`),
Example: io.FormatExampleCommands("download package", []io.Example{
{Cmd: "--version 1.55.0", Desc: "Download Codesphere version 1.55.0"},
{Cmd: "--version 1.55.0 --file installer-lite.tar.gz", Desc: "Download lite package of Codesphere version 1.55.0"},
}),
},
FileWriter: util.NewFilesystemWriter(),
}
pkg.cmd.Flags().StringVarP(&pkg.Opts.Version, "version", "V", "", "Codesphere version to download")
pkg.cmd.Flags().StringVarP(&pkg.Opts.Filename, "file", "f", "installer.tar.gz", "Specify artifact to download")
download.AddCommand(pkg.cmd)

pkg.cmd.RunE = pkg.RunE
}

func (c *DownloadPackageCmd) DownloadBuild(p portal.Portal, build portal.CodesphereBuild, filename string) error {
for _, art := range build.Artifacts {
if art.Filename == filename {
fullFilename := build.Version + "-" + art.Filename
out, err := c.FileWriter.Create(fullFilename)
if err != nil {
return fmt.Errorf("failed to create file %s: %w", fullFilename, err)
}
defer func() { _ = out.Close() }()

buildWithArtifact := build
buildWithArtifact.Artifacts = []portal.Artifact{art}

err = p.DownloadBuildArtifact(buildWithArtifact, out)
if err != nil {
return fmt.Errorf("failed to download build: %w", err)
}
return nil
}

}

return fmt.Errorf("can't find artifact %s in version %s", filename, build.Version)
}
75 changes: 75 additions & 0 deletions cli/cmd/download_package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cmd_test

import (
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"

"github.com/codesphere-cloud/oms/cli/cmd"
"github.com/codesphere-cloud/oms/internal/portal"
"github.com/codesphere-cloud/oms/internal/util"
)

var _ = Describe("ListPackages", func() {

var (
c cmd.DownloadPackageCmd
filename string
version string
build portal.CodesphereBuild
mockPortal *portal.MockPortal
mockFileWriter *util.MockFileWriter
)

BeforeEach(func() {
filename = "installer.tar.gz"
version = "codesphere-1.42.0"
mockPortal = portal.NewMockPortal(GinkgoT())
mockFileWriter = util.NewMockFileWriter(GinkgoT())

c = cmd.DownloadPackageCmd{
Opts: cmd.DownloadPackageOpts{
Version: version,
Filename: filename,
},
FileWriter: mockFileWriter,
}
build = portal.CodesphereBuild{
Version: version,
Artifacts: []portal.Artifact{
{Filename: filename},
{Filename: "otherFilename.tar.gz"},
},
}
})
AfterEach(func() {
mockPortal.AssertExpectations(GinkgoT())
mockFileWriter.AssertExpectations(GinkgoT())
})

Context("File exists", func() {
It("Downloads the correct artifact to the correct output file", func() {
expectedBuildToDownload := portal.CodesphereBuild{
Version: version,
Artifacts: []portal.Artifact{
{Filename: filename},
},
}

fakeFile := os.NewFile(uintptr(0), filename)
mockFileWriter.EXPECT().Create(version+"-"+filename).Return(fakeFile, nil)
mockPortal.EXPECT().DownloadBuildArtifact(expectedBuildToDownload, mock.Anything).Return(nil)
err := c.DownloadBuild(mockPortal, build, filename)
Expect(err).NotTo(HaveOccurred())
})
})

Context("File doesn't exist in build", func() {
It("Returns an error", func() {
err := c.DownloadBuild(mockPortal, build, "installer-lite.tar.gz")
Expect(err).To(MatchError("can't find artifact installer-lite.tar.gz in version " + version))
})
})
})
2 changes: 1 addition & 1 deletion cli/cmd/list_packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ListBuildsOpts struct {

func (c *ListBuildsCmd) RunE(_ *cobra.Command, args []string) error {
p := portal.NewPortalClient()
packages, err := p.ListCodespherePackages()
packages, err := p.ListCodesphereBuilds()
if err != nil {
return fmt.Errorf("failed to list codesphere packages: %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions cli/cmd/list_packages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ import (
var _ = Describe("ListPackages", func() {

var (
mockTableWriter util.MockTableWriter
mockTableWriter *util.MockTableWriter
c cmd.ListBuildsCmd
internal bool
buildDate time.Time
)
BeforeEach(func() {
JustBeforeEach(func() {
mockTableWriter = util.NewMockTableWriter(GinkgoT())
c = cmd.ListBuildsCmd{
Opts: cmd.ListBuildsOpts{
Internal: internal,
},
TableWriter: &mockTableWriter,
TableWriter: mockTableWriter,
}

buildDate, _ = time.Parse("2006-01-02", "2025-05-01")
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func Execute() {
}
AddInstallCmd(rootCmd)
AddListCmd(rootCmd, opts)
AddDownloadCmd(rootCmd, opts)

err := rootCmd.Execute()
if err != nil {
Expand Down
Loading