Skip to content

Commit d8a605d

Browse files
committed
WIP: Extract package
1 parent 8c12ed6 commit d8a605d

File tree

16 files changed

+709
-40
lines changed

16 files changed

+709
-40
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ bin/
2929
oms-cli
3030
oms-service
3131
dist/
32+
oms-workdir/

cli/cmd/download_package.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
type DownloadPackageCmd struct {
1818
cmd *cobra.Command
1919
Opts DownloadPackageOpts
20-
FileWriter util.FileWriter
20+
FileWriter util.FileIO
2121
}
2222

2323
type DownloadPackageOpts struct {

cli/cmd/download_package_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ var _ = Describe("ListPackages", func() {
2323
version string
2424
build portal.Build
2525
mockPortal *portal.MockPortal
26-
mockFileWriter *util.MockFileWriter
26+
mockFileWriter *util.MockFileIO
2727
)
2828

2929
BeforeEach(func() {
3030
filename = "installer.tar.gz"
3131
version = "codesphere-1.42.0"
3232
mockPortal = portal.NewMockPortal(GinkgoT())
33-
mockFileWriter = util.NewMockFileWriter(GinkgoT())
33+
mockFileWriter = util.NewMockFileIO(GinkgoT())
3434

3535
c = cmd.DownloadPackageCmd{
3636
Opts: cmd.DownloadPackageOpts{

cli/cmd/extend.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// ExtendCmd represents the extend command
10+
type ExtendCmd struct {
11+
cmd *cobra.Command
12+
}
13+
14+
func (c *ExtendCmd) RunE(_ *cobra.Command, args []string) error {
15+
//Command execution goes here
16+
17+
fmt.Printf("running %s", c.cmd.Use)
18+
19+
return nil
20+
}
21+
22+
func AddExtendCmd(rootCmd *cobra.Command, opts *GlobalOptions) {
23+
extend := ExtendCmd{
24+
cmd: &cobra.Command{
25+
Use: "extend",
26+
Short: "A brief description of your command",
27+
Long: `A longer description that spans multiple lines and likely contains examples
28+
and usage of using your command. For example:
29+
30+
Cobra is a CLI library for Go that empowers applications.
31+
This application is a tool to generate the needed files
32+
to quickly create a Cobra application.`,
33+
},
34+
}
35+
// Here you will define your flags and configuration settings.
36+
37+
// Cobra supports Persistent Flags which will work for this command
38+
// and all subcommands, e.g.:
39+
// extend.cmd.PersistentFlags().String("foo", "", "A help for foo")
40+
41+
// Cobra supports local flags which will only run when this command
42+
// is called directly, e.g.:
43+
// extend.cmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
44+
rootCmd.AddCommand(extend.cmd)
45+
extend.cmd.RunE = extend.RunE
46+
47+
// Add child commands here
48+
AddExtendBaseimageCmd(extend.cmd, opts)
49+
}

cli/cmd/extend_baseimage.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cmd
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
9+
"github.com/codesphere-cloud/oms/internal/env"
10+
"github.com/codesphere-cloud/oms/internal/installer"
11+
)
12+
13+
// ExtendBaseimageCmd represents the baseimage command
14+
type ExtendBaseimageCmd struct {
15+
cmd *cobra.Command
16+
Opts *ExtendBaseimageOpts
17+
}
18+
19+
type ExtendBaseimageOpts struct {
20+
*GlobalOptions
21+
Package string
22+
Force bool
23+
}
24+
25+
func (c *ExtendBaseimageCmd) RunE(_ *cobra.Command, args []string) error {
26+
if c.Opts.Package == "" {
27+
return errors.New("Required option package not set")
28+
}
29+
30+
p := installer.NewPackage(c.Opts.Package)
31+
err := p.Extract(env.NewEnv().GetOmsWorkdir(), c.Opts.Force)
32+
if err != nil {
33+
return fmt.Errorf("failed to extract package to workdir: %w", err)
34+
}
35+
36+
return nil
37+
}
38+
39+
func AddExtendBaseimageCmd(extend *cobra.Command, opts *GlobalOptions) {
40+
baseimage := ExtendBaseimageCmd{
41+
cmd: &cobra.Command{
42+
Use: "baseimage",
43+
Short: "A brief description of your command",
44+
Long: `A longer description that spans multiple lines and likely contains examples
45+
and usage of using your command. For example:
46+
47+
Cobra is a CLI library for Go that empowers applications.
48+
This application is a tool to generate the needed files
49+
to quickly create a Cobra application.`,
50+
},
51+
Opts: &ExtendBaseimageOpts{GlobalOptions: opts},
52+
}
53+
baseimage.cmd.Flags().StringVarP(&baseimage.Opts.Package, "package", "p", "", "Package file (e.g. codesphere-v1.2.3-installer.tar.gz) to load base image from")
54+
baseimage.cmd.Flags().BoolVarP(&baseimage.Opts.Force, "force", "f", false, "Enforce package extraction")
55+
extend.AddCommand(baseimage.cmd)
56+
baseimage.cmd.RunE = baseimage.RunE
57+
}

cli/cmd/root.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Copyright © 2025 NAME HERE <EMAIL ADDRESS>
44
package cmd
55

66
import (
7+
"log"
78
"os"
89

910
"github.com/codesphere-cloud/cs-go/pkg/io"
@@ -29,11 +30,15 @@ func GetRootCmd() *cobra.Command {
2930
AddUpdateCmd(rootCmd)
3031
AddListCmd(rootCmd, opts)
3132
AddDownloadCmd(rootCmd, opts)
33+
AddExtendCmd(rootCmd, &opts)
3234
return rootCmd
3335
}
3436

3537
// Execute executes the root command. This is called by main.main(). It only needs to happen once to the rootCmd.
3638
func Execute() {
39+
//Disable printing timestamps on log lines
40+
log.SetFlags(0)
41+
3742
err := GetRootCmd().Execute()
3843
if err != nil {
3944
os.Exit(1)

cli/cmd/testdata/testcli.tar.gz

-1 Bytes
Binary file not shown.

cli/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
22
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
3-
43
*/
54
package main
65

7-
import "github.com/codesphere-cloud/oms/cli/cmd"
6+
import (
7+
"github.com/codesphere-cloud/oms/cli/cmd"
8+
)
89

910
func main() {
1011
cmd.Execute()

internal/env/env.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
type Env interface {
1212
GetOmsPortalApiKey() (string, error)
1313
GetOmsPortalApi() string
14+
GetOmsWorkdir() string
1415
}
1516

1617
type Environment struct {
@@ -28,6 +29,14 @@ func (e *Environment) GetOmsPortalApiKey() (string, error) {
2829
return apiToken, nil
2930
}
3031

32+
func (e *Environment) GetOmsWorkdir() string {
33+
workdir := os.Getenv("OMS_WORKDIR")
34+
if workdir == "" {
35+
return "./oms-workdir"
36+
}
37+
return workdir
38+
}
39+
3140
func (e *Environment) GetOmsPortalApi() string {
3241
apiUrl := os.Getenv("OMS_PORTAL_API")
3342
if apiUrl == "" {

internal/env/mocks.go

Lines changed: 44 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)