Return non-zero exit code on node sub-commands
Signed-off-by: xu yang <xyang@ciq.com>
This commit is contained in:
@@ -16,14 +16,12 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
wwlog.Error("Could not open node configuration: %s", err)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("could not open node configuration: %s", err)
|
||||
}
|
||||
|
||||
nodes, err := nodeDB.FindAllNodes()
|
||||
if err != nil {
|
||||
wwlog.Error("Could not get node list: %s", err)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("could not get node list: %s", err)
|
||||
}
|
||||
|
||||
args = hostlist.Expand(args)
|
||||
@@ -37,8 +35,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
if len(nodes) == 0 {
|
||||
fmt.Printf("No nodes found\n")
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("no nodes found")
|
||||
}
|
||||
|
||||
for _, node := range nodes {
|
||||
@@ -23,12 +23,10 @@ import (
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
canWrite, err := apiutil.CanWriteConfig()
|
||||
if err != nil {
|
||||
wwlog.Error("While checking whether can write config, err: %w", err)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("while checking whether can write config, err: %w", err)
|
||||
}
|
||||
if !canWrite.CanWriteConfig {
|
||||
wwlog.Error("Can't write to config exiting")
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("can not write to config exiting")
|
||||
}
|
||||
editor := os.Getenv("EDITOR")
|
||||
if editor == "" {
|
||||
@@ -46,7 +44,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
_ = yaml.Unmarshal([]byte(nodeListMsg.NodeConfMapYaml), nodeMap)
|
||||
file, err := os.CreateTemp(os.TempDir(), "ww4NodeEdit*.yaml")
|
||||
if err != nil {
|
||||
wwlog.Error("Could not create temp file:%s \n", err)
|
||||
return fmt.Errorf("could not create temp file: %s", err)
|
||||
}
|
||||
defer os.Remove(file.Name())
|
||||
yamlTemplate := node.UnmarshalConf(node.NodeConf{}, []string{"tagsdel", "default", "profiles"})
|
||||
@@ -65,8 +63,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
sum1 := hex.EncodeToString(hasher.Sum(nil))
|
||||
err = util.ExecInteractive(editor, file.Name())
|
||||
if err != nil {
|
||||
wwlog.Error("Editor process existed with non-zero\n")
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("editor process existed with non-zero")
|
||||
}
|
||||
_, _ = file.Seek(0, 0)
|
||||
hasher.Reset()
|
||||
@@ -125,8 +122,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
Hash: newHash.Hash,
|
||||
})
|
||||
if err != nil {
|
||||
wwlog.Error("Got following problem when writing back yaml: %s", err)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("got following problem when writing back yaml: %s", err)
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
package export
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
apinode "github.com/warewulf/warewulf/internal/pkg/api/node"
|
||||
"github.com/warewulf/warewulf/internal/pkg/api/routes/wwapiv1"
|
||||
"github.com/warewulf/warewulf/internal/pkg/wwlog"
|
||||
)
|
||||
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
@@ -21,6 +20,6 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
// got proper yaml back
|
||||
_ = yaml.Unmarshal([]byte(nodeListMsg.NodeConfMapYaml), nodeMap)
|
||||
*/
|
||||
fmt.Println(nodeListMsg.NodeConfMapYaml)
|
||||
wwlog.Info(nodeListMsg.NodeConfMapYaml)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -19,16 +19,14 @@ import (
|
||||
func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
file, err := os.Open(args[0])
|
||||
if err != nil {
|
||||
wwlog.Error("Could not open file:%s \n", err)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("could not open file: %s", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
importMap := make(map[string]*node.NodeConf)
|
||||
buffer, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
wwlog.Error("Could not read:%s\n", err)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("could not read: %s", err)
|
||||
}
|
||||
if !ImportCVS {
|
||||
err = yaml.Unmarshal(buffer, importMap)
|
||||
@@ -37,24 +35,21 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
if yes {
|
||||
err = apinode.NodeAddFromYaml(&wwapiv1.NodeYaml{NodeConfMapYaml: string(buffer)})
|
||||
if err != nil {
|
||||
wwlog.Error("Got following problem when writing back yaml: %s", err)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("got following problem when writing back yaml: %s", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
wwlog.Error("Could not parse import file")
|
||||
return fmt.Errorf("could not parse import file: %s", err)
|
||||
}
|
||||
} else {
|
||||
// reading from buffer is a bit overshot
|
||||
csvReader := csv.NewReader(bytes.NewReader(buffer))
|
||||
records, err := csvReader.ReadAll()
|
||||
if err != nil {
|
||||
wwlog.Error("Could not parse %s: %s\n", args[0], err)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("could not parse %s: %s", args[0], err)
|
||||
}
|
||||
if len(records) < 1 || len(records[0]) < 1 {
|
||||
wwlog.Error("Did not find any data in %s\n", args[0])
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("did not find any data in %s", args[0])
|
||||
}
|
||||
if !(records[0][0] == "node" || records[0][0] == "nodename") {
|
||||
Usage()
|
||||
@@ -63,8 +58,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
argsLen := len(records[0])
|
||||
for i, line := range records[1:] {
|
||||
if len(line) != argsLen {
|
||||
wwlog.Error("Wrong number of fields in lube %u\n", i+1)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("wrong number of fields in lube %d", i+1)
|
||||
}
|
||||
for j := range line {
|
||||
if j == 0 {
|
||||
@@ -84,12 +78,11 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
// create second buffer an marshall nodeMap to it
|
||||
buffer, err = yaml.Marshal(importMap)
|
||||
if err != nil {
|
||||
wwlog.Error("Got following problem when creating yaml: %s", err)
|
||||
return fmt.Errorf("got following problem when creating yaml: %s", err)
|
||||
}
|
||||
err = apinode.NodeAddFromYaml(&wwapiv1.NodeYaml{NodeConfMapYaml: string(buffer)})
|
||||
if err != nil {
|
||||
wwlog.Error("Got following problem when writing back yaml: %s", err)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("got following problem when writing back yaml: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,14 +17,12 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
wwlog.Error("Could not open node configuration: %s", err)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("could not open node configuration: %s", err)
|
||||
}
|
||||
|
||||
nodes, err := nodeDB.FindAllNodes()
|
||||
if err != nil {
|
||||
wwlog.Error("Could not get node list: %s", err)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("could not get node list: %s", err)
|
||||
}
|
||||
|
||||
args = hostlist.Expand(args)
|
||||
@@ -38,7 +36,7 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
}
|
||||
|
||||
if len(nodes) == 0 {
|
||||
fmt.Printf("No nodes found\n")
|
||||
wwlog.Info("No nodes found")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package set
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@@ -54,8 +53,7 @@ func CobraRunE(vars *variables) func(cmd *cobra.Command, args []string) (err err
|
||||
delete(vars.nodeConf.Disks, "UNDEF")
|
||||
buffer, err := yaml.Marshal(vars.nodeConf)
|
||||
if err != nil {
|
||||
wwlog.Error("Can't marshall nodeInfo", err)
|
||||
os.Exit(1)
|
||||
return fmt.Errorf("can not marshall nodeInfo: %s", err)
|
||||
}
|
||||
wwlog.Debug("sending following values: %s", string(buffer))
|
||||
set := wwapiv1.NodeSetParameter{
|
||||
|
||||
@@ -102,8 +102,7 @@ func NodeDelete(ndp *wwapiv1.NodeDeleteParameter) (err error) {
|
||||
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
wwlog.Error("Failed to open node database: %s", err)
|
||||
return
|
||||
return fmt.Errorf("failed to open node database: %s", err)
|
||||
}
|
||||
dbHash := nodeDB.Hash()
|
||||
if hex.EncodeToString(dbHash[:]) != ndp.Hash && !ndp.Force {
|
||||
@@ -138,27 +137,23 @@ func NodeDelete(ndp *wwapiv1.NodeDeleteParameter) (err error) {
|
||||
func NodeDeleteParameterCheck(ndp *wwapiv1.NodeDeleteParameter, console bool) (nodeList []node.NodeInfo, err error) {
|
||||
|
||||
if ndp == nil {
|
||||
err = fmt.Errorf("NodeDeleteParameter is nil")
|
||||
return
|
||||
return nodeList, fmt.Errorf("NodeDeleteParameter is nil")
|
||||
}
|
||||
|
||||
nodeDB, err := node.New()
|
||||
if err != nil {
|
||||
wwlog.Error("Failed to open node database: %s", err)
|
||||
return
|
||||
return nodeList, fmt.Errorf("failed to open node database: %s", err)
|
||||
}
|
||||
dbHash := nodeDB.Hash()
|
||||
if hex.EncodeToString(dbHash[:]) != ndp.Hash && !ndp.Force {
|
||||
wwlog.Debug("got hash: %s", ndp.Hash)
|
||||
wwlog.Debug("actual hash: %s", hex.EncodeToString(dbHash[:]))
|
||||
err = fmt.Errorf("got wrong hash, not modifying node database")
|
||||
return
|
||||
return nodeList, fmt.Errorf("got wrong hash, not modifying node database")
|
||||
}
|
||||
|
||||
nodes, err := nodeDB.FindAllNodes()
|
||||
if err != nil {
|
||||
wwlog.Error("Could not get node list: %s", err)
|
||||
return
|
||||
return nodeList, fmt.Errorf("could not get node list: %s", err)
|
||||
}
|
||||
|
||||
node_args := hostlist.Expand(ndp.NodeNames)
|
||||
@@ -173,12 +168,12 @@ func NodeDeleteParameterCheck(ndp *wwapiv1.NodeDeleteParameter, console bool) (n
|
||||
}
|
||||
|
||||
if !match {
|
||||
fmt.Fprintf(os.Stderr, "ERROR: No match for node: %s\n", r)
|
||||
wwlog.Error("ERROR: No match for node: %s\n", r)
|
||||
}
|
||||
}
|
||||
|
||||
if len(nodeList) == 0 {
|
||||
fmt.Printf("No nodes found\n")
|
||||
wwlog.Info("No nodes found")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user