Skip to content

Commit

Permalink
fix(pkg): Add --force flag to sourcing and prevent check on unsourc…
Browse files Browse the repository at this point in the history
…ing (#878)

Reviewed-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io>
Approved-by: Cezar Craciunoiu <cezar.craciunoiu@unikraft.io>
  • Loading branch information
craciunoiuc committed Oct 14, 2023
2 parents 89d8977 + d793c88 commit 0307fe9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
16 changes: 10 additions & 6 deletions cmd/kraft/pkg/source/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import (
"kraftkit.sh/packmanager"
)

type Source struct{}
type Source struct {
Force bool `short:"F" long:"force" usage:"Do not run a compatibility test before sourcing."`
}

func New() *cobra.Command {
cmd, err := cmdfactory.New(&Source{}, cobra.Command{
Expand Down Expand Up @@ -58,11 +60,13 @@ func (opts *Source) Run(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

for _, source := range args {
_, compatible, err := packmanager.G(ctx).IsCompatible(ctx, source)
if err != nil {
return err
} else if !compatible {
return errors.New("incompatible package manager")
if !opts.Force {
_, compatible, err := packmanager.G(ctx).IsCompatible(ctx, source)
if err != nil {
return err
} else if !compatible {
return errors.New("incompatible package manager")
}
}

for _, manifest := range config.G[config.KraftKit](ctx).Unikraft.Manifests {
Expand Down
9 changes: 0 additions & 9 deletions cmd/kraft/pkg/unsource/unsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
package unsource

import (
"errors"

"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -58,13 +56,6 @@ func (*Unsource) Pre(cmd *cobra.Command, _ []string) error {
func (opts *Unsource) Run(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
for _, source := range args {
_, compatible, err := packmanager.G(ctx).IsCompatible(ctx, source)
if err != nil {
return err
} else if !compatible {
return errors.New("incompatible package manager")
}

manifests := []string{}

var manifestRemoved bool
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/cli/pkg_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ var _ = Describe("kraft pkg", func() {
Expect(err).ToNot(HaveOccurred())
})
It("should create the config file, add the default manifests, and the new link, and print nothing", func() {
cmd.Args = append(cmd.Args, "--force")
cmd.Args = append(cmd.Args, "https://example.com")
err := cmd.Run()
if err != nil {
Expand Down Expand Up @@ -130,6 +131,7 @@ var _ = Describe("kraft pkg", func() {
oldArgs := make([]string, len(cmd.Args))
copy(oldArgs, cmd.Args)

cmd.Args = append(cmd.Args, "--force")
cmd.Args = append(cmd.Args, "https://example1.com")
err := cmd.Run()
if err != nil {
Expand All @@ -147,6 +149,7 @@ var _ = Describe("kraft pkg", func() {
})

It("should leave the config file intact, add the new link, and print nothing", func() {
cmd.Args = append(cmd.Args, "--force")
cmd.Args = append(cmd.Args, "https://example2.com")
err := cmd.Run()
if err != nil {
Expand Down Expand Up @@ -270,6 +273,7 @@ var _ = Describe("kraft pkg", func() {
Context("sourcing multiple links in the config file", func() {
When("the config file was already present, and all links are unique", func() {
It("should add all links and print nothing", func() {
cmd.Args = append(cmd.Args, "--force")
cmd.Args = append(cmd.Args, "https://example1.com")
cmd.Args = append(cmd.Args, "https://example2.com")
cmd.Args = append(cmd.Args, "https://example3.com")
Expand Down Expand Up @@ -339,6 +343,7 @@ var _ = Describe("kraft pkg", func() {

When("the config file was already present, and a link is duplicate", func() {
It("should add links until the first error is met", func() {
cmd.Args = append(cmd.Args, "--force")
cmd.Args = append(cmd.Args, "https://example.com")
cmd.Args = append(cmd.Args, "https://example.com")
cmd.Args = append(cmd.Args, "https://example2.com")
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/cli/pkg_unsource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ var _ = Describe("kraft pkg", func() {
break
}
}

cmd.Args = append(cmd.Args, "--force")
cmd.Args = append(cmd.Args, "https://example1.com")
cmd.Args = append(cmd.Args, "https://example2.com")
cmd.Args = append(cmd.Args, "https://example3.com")
Expand Down Expand Up @@ -363,6 +365,8 @@ var _ = Describe("kraft pkg", func() {
break
}
}

cmd.Args = append(cmd.Args, "--force")
cmd.Args = append(cmd.Args, "https://example1.com")
cmd.Args = append(cmd.Args, "https://example2.com")
cmd.Args = append(cmd.Args, "https://example3.com")
Expand Down

0 comments on commit 0307fe9

Please sign in to comment.