Skip to content

Commit

Permalink
feat: Added new subcommand remove to commmad kraft pkg
Browse files Browse the repository at this point in the history
Signed-off-by: Md Sahil <mohdssahil1@gmail.com>
  • Loading branch information
MdSahil-oss committed Aug 15, 2023
1 parent 047e915 commit b0eaeb1
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/kraft/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"kraftkit.sh/cmd/kraft/pkg/list"
"kraftkit.sh/cmd/kraft/pkg/pull"
"kraftkit.sh/cmd/kraft/pkg/push"
"kraftkit.sh/cmd/kraft/pkg/remove"
"kraftkit.sh/cmd/kraft/pkg/source"
"kraftkit.sh/cmd/kraft/pkg/unsource"
"kraftkit.sh/cmd/kraft/pkg/update"
Expand Down Expand Up @@ -81,6 +82,7 @@ func New() *cobra.Command {
cmd.AddCommand(source.New())
cmd.AddCommand(unsource.New())
cmd.AddCommand(update.New())
cmd.AddCommand(remove.New())

return cmd
}
Expand Down
85 changes: 85 additions & 0 deletions cmd/kraft/pkg/remove/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package remove

import (
"os"

"github.com/spf13/cobra"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/packmanager"
"kraftkit.sh/unikraft/app"
)

type Remove struct {
Workdir string `long:"workdir" short:"w" usage:"workdir location to remove pkg from that location"`
Kraftfile string `long:"kraftfile" usage:"Set an alternative path of the Kraftfile"`
}

func New() *cobra.Command {
cmd, err := cmdfactory.New(&Remove{}, cobra.Command{
Short: "Remove unikraft library from the project directory",
Use: "remove [FLAGS] [PACKAGE] [DIR]",
Aliases: []string{"rm"},
Args: cmdfactory.MinimumArgs(1, "package name is not specified to remove from the project"),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "pkg",
},
})
if err != nil {
panic(err)
}

return cmd
}

func (opts *Remove) Pre(cmd *cobra.Command, _ []string) error {
ctx := cmd.Context()
pm, err := packmanager.NewUmbrellaManager(ctx)
if err != nil {
return err
}

cmd.SetContext(packmanager.WithPackageManager(ctx, pm))

return nil
}

func (opts *Remove) Run(cmd *cobra.Command, args []string) error {
var workdir string
var err error

if len(args) > 1 {
workdir = args[1]
} else {
workdir = opts.Workdir
}

if workdir == "." || workdir == "" {
workdir, err = os.Getwd()
}
if err != nil {
return err
}
ctx := cmd.Context()

popts := []app.ProjectOption{}

if len(opts.Kraftfile) > 0 {
popts = append(popts, app.WithProjectKraftfile(opts.Kraftfile))
} else {
popts = append(popts, app.WithProjectDefaultKraftfiles())
}

project, err := app.NewProjectFromOptions(
ctx,
append(popts, app.WithProjectWorkdir(workdir))...,
)
if err != nil {
return err
}

if err = project.RemoveLibrary(ctx, args[0]); err != nil {
return err
}

return project.Save()
}
38 changes: 38 additions & 0 deletions unikraft/app/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ type Application interface {

// Serialize and save the application to the kraftfile
Save() error

// Removes library from the project directory
RemoveLibrary(ctx context.Context, libraryName string) error
}

type application struct {
Expand Down Expand Up @@ -784,3 +787,38 @@ func (app application) Save() error {

return nil
}

func (app application) RemoveLibrary(ctx context.Context, libraryName string) error {
for libKey, lib := range app.libraries {
if lib.Name() == libraryName {
var emptyKraftfile application
yamlFileInfo, err := os.Stat(app.kraftfile.path)
if err != nil {
return err
}

delete(app.libraries, libKey)

yaml, err := yaml.Marshal(&emptyKraftfile)
if err != nil {
return err
}

// Write an empty Kraftfile.
err = os.WriteFile(app.kraftfile.path, []byte(yaml), yamlFileInfo.Mode().Perm())
if err != nil {
return err
}

// Remove library directory from the project directory
libPath := filepath.Join(app.WorkingDir(), unikraft.LibsDir, libraryName)
if _, err = os.Stat(libPath); err == nil {
err = os.RemoveAll(libPath)
if err != nil {
return err
}
}
}
}
return nil
}
1 change: 1 addition & 0 deletions unikraft/unikraft.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ const (
// Built-in paths
VendorDir = ".unikraft"
BuildDir = ".unikraft/build"
LibsDir = ".unikraft/libs"
)

0 comments on commit b0eaeb1

Please sign in to comment.