Recursive handling for command line flags

Every struct in a NodeConf with `lopt:"foo" set is now added as a
command-line flag with the RecursiveCreateFlags call. For maps a struct
with the key UNDEF is added so that it can be parsed out.

As the flags for the command-line need variables which hold the values,
for every map an element map[UNDEF] is added.  When now calling the
internal add, these map element can be filtered out and replace by the
given name. (e.g., --netname)

* rewrote node/profile add for recursive functions
* rewrote node/profile set for recursive functions
* rewrote node/profile list for recursive functions

Signed-off-by: Christian Goll <cgoll@suse.com>
This commit is contained in:
Christian Goll
2023-08-11 10:31:29 -06:00
committed by Jonathon Anderson
parent c55c5a2ac4
commit 45539a0d1f
29 changed files with 1346 additions and 714 deletions

View File

@@ -3,8 +3,10 @@ package add
import (
"fmt"
"os"
"strings"
apiprofile "github.com/hpcng/warewulf/internal/pkg/api/profile"
"github.com/hpcng/warewulf/internal/pkg/node"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"gopkg.in/yaml.v2"
@@ -16,7 +18,7 @@ import (
func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err error) {
return func(cmd *cobra.Command, args []string) (err error) {
// run converters for different types
for _, c := range Converters {
for _, c := range vars.Converters {
if err := c(); err != nil {
return err
}
@@ -24,25 +26,55 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
// remove the default network as the all network values are assigned
// to this network
if vars.netName != "" {
netDev := *vars.profileConf.NetDevs["default"]
netDev := *vars.profileConf.NetDevs["UNDEF"]
vars.profileConf.NetDevs[vars.netName] = &netDev
delete(vars.profileConf.NetDevs, "default")
delete(vars.profileConf.NetDevs, "UNDEF")
}
// remove the UNDEF network as all network values are assigned
// to this network
if !node.ObjectIsEmpty(vars.profileConf.NetDevs["UNDEF"]) {
netDev := *vars.profileConf.NetDevs["UNDEF"]
vars.profileConf.NetDevs[vars.netName] = &netDev
}
delete(vars.profileConf.NetDevs, "UNDEF")
if vars.fsName != "" {
if !strings.HasPrefix(vars.fsName, "/dev") {
if vars.fsName == vars.partName {
vars.fsName = "/dev/disk/by-partlabel/" + vars.partName
} else {
return fmt.Errorf("filesystems need to have a underlying blockdev")
}
}
fs := *vars.profileConf.FileSystems["UNDEF"]
vars.profileConf.FileSystems[vars.fsName] = &fs
}
delete(vars.profileConf.FileSystems, "UNDEF")
if vars.diskName != "" && vars.partName != "" {
prt := *vars.profileConf.Disks["UNDEF"].Partitions["UNDEF"]
vars.profileConf.Disks["UNDEF"].Partitions[vars.partName] = &prt
delete(vars.profileConf.Disks["UNDEF"].Partitions, "UNDEF")
dsk := *vars.profileConf.Disks["UNDEF"]
vars.profileConf.Disks[vars.diskName] = &dsk
}
if (vars.diskName != "") != (vars.partName != "") {
return fmt.Errorf("partition and disk must be specified")
}
delete(vars.profileConf.Disks, "UNDEF")
buffer, err := yaml.Marshal(vars.profileConf)
if err != nil {
wwlog.Error("Cant marshall nodeInfo", err)
os.Exit(1)
}
set := wwapiv1.NodeSetParameter{
set := wwapiv1.ProfileSetParameter{
NodeConfYaml: string(buffer[:]),
NetdevDelete: SetNetDevDel,
AllNodes: SetNodeAll,
Force: SetForce,
NodeNames: args,
NetdevDelete: vars.SetNetDevDel,
AllProfiles: vars.SetNodeAll,
Force: vars.SetForce,
ProfileNames: args,
}
if !SetYes {
if !vars.SetYes {
// The checks run twice in the prompt case.
// Avoiding putting in a blocking prompt in an API.
_, _, err = apiprofile.ProfileSetParameterCheck(&set, false)