diff --git a/internal/app/wwctl/overlay/create/main.go b/internal/app/wwctl/overlay/create/main.go index cd1fa603..7566c6f8 100644 --- a/internal/app/wwctl/overlay/create/main.go +++ b/internal/app/wwctl/overlay/create/main.go @@ -11,6 +11,11 @@ import ( func CobraRunE(cmd *cobra.Command, args []string) error { + if len(args) < 1 { + cmd.Help() + os.Exit(1) + } + if SystemOverlay == true { err := overlay.SystemOverlayInit(args[0]) if err != nil { diff --git a/internal/app/wwctl/overlay/create/root.go b/internal/app/wwctl/overlay/create/root.go index be18f2a3..d26ae250 100644 --- a/internal/app/wwctl/overlay/create/root.go +++ b/internal/app/wwctl/overlay/create/root.go @@ -6,10 +6,12 @@ import ( var ( baseCmd = &cobra.Command{ - Use: "create", + Use: "create [overlay name]", Short: "Initialize a new Overlay", Long: "Create a new Warewulf provisioning overlay", RunE: CobraRunE, + Args: cobra.ExactArgs(1), + } SystemOverlay bool ) diff --git a/internal/app/wwctl/overlay/delete/main.go b/internal/app/wwctl/overlay/delete/main.go new file mode 100644 index 00000000..65fc96aa --- /dev/null +++ b/internal/app/wwctl/overlay/delete/main.go @@ -0,0 +1,12 @@ +package delete + +import ( + "fmt" + "github.com/spf13/cobra" +) + +func CobraRunE(cmd *cobra.Command, args []string) error { + fmt.Printf("Delete: Hello World\n") + + return nil +} \ No newline at end of file diff --git a/internal/app/wwctl/overlay/delete/root.go b/internal/app/wwctl/overlay/delete/root.go new file mode 100644 index 00000000..20d40cdb --- /dev/null +++ b/internal/app/wwctl/overlay/delete/root.go @@ -0,0 +1,31 @@ +package delete + +import ( + "github.com/spf13/cobra" +) + +var ( + baseCmd = &cobra.Command{ + Use: "delete [overlay name]", + Short: "Delete Warewulf Overlay files", + Long: "Warewulf Delete overlay files", + RunE: CobraRunE, + Args: cobra.ExactArgs(1), + + } + SystemOverlay bool + Force bool + +) + +func init() { + baseCmd.PersistentFlags().BoolVarP(&SystemOverlay, "system", "s", false, "Show system overlays instead of runtime") + baseCmd.PersistentFlags().BoolVar(&Force, "force", false, "Force deletion of a non-empty overlay") + +} + +// GetRootCommand returns the root cobra.Command for the application. +func GetCommand() *cobra.Command { + return baseCmd +} + diff --git a/internal/app/wwctl/overlay/edit/main.go b/internal/app/wwctl/overlay/edit/main.go new file mode 100644 index 00000000..0465d9ec --- /dev/null +++ b/internal/app/wwctl/overlay/edit/main.go @@ -0,0 +1,54 @@ +package edit + +import ( + "fmt" + "github.com/hpcng/warewulf/internal/pkg/config" + "github.com/hpcng/warewulf/internal/pkg/util" + "github.com/hpcng/warewulf/internal/pkg/wwlog" + "github.com/spf13/cobra" + "os" + "path" +) + +func CobraRunE(cmd *cobra.Command, args []string) error { + config := config.New() + editor := config.Editor + var overlaySourceDir string + + + if len(args) < 2 { + fmt.Printf("wwctl overlay edit [overlay name] [overlay file]\n") + cmd.Help() + os.Exit(1) + } + + if SystemOverlay == true { + overlaySourceDir = config.SystemOverlaySource(args[0]) + } else { + overlaySourceDir = config.RuntimeOverlaySource(args[0]) + } + + if util.IsDir(overlaySourceDir) == false { + wwlog.Printf(wwlog.ERROR, "Overlay does not exist: %s\n", args[0]) + os.Exit(1) + } + + overlayFile := path.Join(overlaySourceDir, args[1]) + + wwlog.Printf(wwlog.DEBUG, "Will edit overlay file: %s\n", overlayFile) + + err := os.MkdirAll(path.Dir(overlayFile), 0755) + if err != nil { + wwlog.Printf(wwlog.ERROR, "Could not create directory: %s\n", path.Dir(overlayFile)) + os.Exit(1) + } + + if editor == "" { + wwlog.Printf(wwlog.WARN, "No default editor provided, will use `nano`.") + editor = "nano" + } + + return util.ExecInteractive(editor, overlayFile) + +} + diff --git a/internal/app/wwctl/overlay/edit/root.go b/internal/app/wwctl/overlay/edit/root.go index 3c4f38fc..15b86a1b 100644 --- a/internal/app/wwctl/overlay/edit/root.go +++ b/internal/app/wwctl/overlay/edit/root.go @@ -1,16 +1,17 @@ package edit import ( - "fmt" "github.com/spf13/cobra" ) var ( baseCmd = &cobra.Command{ - Use: "edit", + Use: "edit [overlay name] [file path]", Short: "Edit Warewulf Overlay files", Long: "Warewulf edit overlay files", RunE: CobraRunE, + Args: cobra.ExactArgs(2), + } SystemOverlay bool ListFiles bool @@ -29,7 +30,3 @@ func GetCommand() *cobra.Command { } -func CobraRunE(cmd *cobra.Command, args []string) error { - fmt.Printf("Edit: Hello World\n") - return nil -} \ No newline at end of file diff --git a/internal/app/wwctl/overlay/root.go b/internal/app/wwctl/overlay/root.go index a28afed0..f1947da0 100644 --- a/internal/app/wwctl/overlay/root.go +++ b/internal/app/wwctl/overlay/root.go @@ -2,6 +2,7 @@ package overlay import ( "github.com/hpcng/warewulf/internal/app/wwctl/overlay/create" + "github.com/hpcng/warewulf/internal/app/wwctl/overlay/delete" "github.com/hpcng/warewulf/internal/app/wwctl/overlay/edit" "github.com/hpcng/warewulf/internal/app/wwctl/overlay/list" "github.com/hpcng/warewulf/internal/app/wwctl/overlay/show" @@ -25,7 +26,7 @@ func init() { baseCmd.AddCommand(show.GetCommand()) baseCmd.AddCommand(create.GetCommand()) baseCmd.AddCommand(edit.GetCommand()) - + baseCmd.AddCommand(delete.GetCommand()) } diff --git a/internal/pkg/config/config.go b/internal/pkg/config/config.go index 92426005..702af958 100644 --- a/internal/pkg/config/config.go +++ b/internal/pkg/config/config.go @@ -19,6 +19,7 @@ type Config struct { Debug bool `yaml:"debug"` SysConfDir string `yaml:"system config dir"` LocalStateDir string `yaml:"local state dir"` + Editor string `yaml:"default editor", envconfig:"EDITOR"` } var c Config @@ -56,6 +57,7 @@ func init() { util.ValidateOrDie("warewulf.conf", "warewulfd ipaddr", c.Ipaddr, "^[0-9]+.[0-9]+.[0-9]+.[0-9]+$") util.ValidateOrDie("warewulf.conf", "system config dir", c.SysConfDir, "^[a-zA-Z0-9-._:/]+$") util.ValidateOrDie("warewulf.conf", "local state dir", c.LocalStateDir, "^[a-zA-Z0-9-._:/]+$") + util.ValidateOrDie("warewulf.conf", "default editor", c.LocalStateDir, "^[a-zA-Z0-9-._:/]+$") } diff --git a/internal/pkg/util/util.go b/internal/pkg/util/util.go index 2c0a6f7e..32851d7b 100644 --- a/internal/pkg/util/util.go +++ b/internal/pkg/util/util.go @@ -5,6 +5,7 @@ import ( "io" "math/rand" "os" + "os/exec" "path/filepath" "regexp" "time" @@ -132,4 +133,14 @@ func FindFiles(path string) []string { } return ret +} + +func ExecInteractive(command string, a...string) error { + wwlog.Printf(wwlog.DEBUG, "ExecInteractive(%s, %s)\n", command, a) + c := exec.Command(command, a...) + c.Stdin = os.Stdin + c.Stdout = os.Stdout + c.Stderr = os.Stderr + err := c.Run() + return err } \ No newline at end of file