Still working on CLI framework for wwctl overlay
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
)
|
||||
|
||||
12
internal/app/wwctl/overlay/delete/main.go
Normal file
12
internal/app/wwctl/overlay/delete/main.go
Normal file
@@ -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
|
||||
}
|
||||
31
internal/app/wwctl/overlay/delete/root.go
Normal file
31
internal/app/wwctl/overlay/delete/root.go
Normal file
@@ -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
|
||||
}
|
||||
|
||||
54
internal/app/wwctl/overlay/edit/main.go
Normal file
54
internal/app/wwctl/overlay/edit/main.go
Normal file
@@ -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)
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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())
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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-._:/]+$")
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user