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 Jul 16, 2023
1 parent 54d9504 commit 37c8409
Show file tree
Hide file tree
Showing 3 changed files with 148 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 @@ -26,6 +26,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 @@ -80,6 +81,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
88 changes: 88 additions & 0 deletions cmd/kraft/pkg/remove/remove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package remove

import (
"fmt"
"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]",
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 {
if len(args) == 0 {
return fmt.Errorf("Package Name is not specified to remove from the project.")
}
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
}

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

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

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

// Removes library from the project directory with the help
// of given name and version by user.
RemoveLibrary(ctx context.Context, libraryName string) error
}

type MinimalKraftfile struct {
Specification string `yaml:"specification"`
Name string `yaml:"name"`
Unikraft map[string]interface{} `yaml:"unikraft"`
Targets []map[string]interface{} `yaml:"targets"`
Libraries map[string]map[string]interface{} `yaml:"libraries"`
}

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

return nil
}

func (app application) RemoveLibrary(ctx context.Context, libraryNameAndVersion string) error {
nameAndVersion := strings.Split(libraryNameAndVersion, ":")
nameAndVersion[0] = strings.ToLower(nameAndVersion[0])
version := "stable"
if len(nameAndVersion) > 1 {
version = nameAndVersion[1]
}

for libKey, lib := range app.libraries {
if lib.Name() == nameAndVersion[0] && version == lib.Version() {
var kraftfileValues MinimalKraftfile
yamlFile, err := os.ReadFile(app.kraftfile.path)
if err != nil {
return err
}
err = yaml.Unmarshal(yamlFile, &kraftfileValues)
if err != nil {
return err
}
delete(app.libraries, libKey)
delete(kraftfileValues.Libraries, nameAndVersion[0])

// Marshal the Node structure back to YAML
yaml, err := yaml.Marshal(&kraftfileValues)
if err != nil {
return err
}

// Write the YAML data to the file
err = os.WriteFile(app.kraftfile.path, []byte(yaml), 0o644)
if err != nil {
return err
}

// Remove library directory from the project directory
if _, err = os.Stat(app.WorkingDir() + "/.unikraft/libs/" + nameAndVersion[0]); err == nil {
err = os.RemoveAll(app.WorkingDir() + "/.unikraft/libs/" + nameAndVersion[0])
if err != nil {
return err
}
}
}
}
return nil
}

0 comments on commit 37c8409

Please sign in to comment.