Return non-zero exit code on node sub-commands

Signed-off-by: xu yang <xyang@ciq.com>
This commit is contained in:
xu yang
2024-09-26 06:15:02 +00:00
parent eb2fab9902
commit 3f120f2c4b
8 changed files with 31 additions and 54 deletions

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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)
}
}
}

View File

@@ -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)
}

View File

@@ -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{