Remove wwctl node import --csv
- Closes #1862 Signed-off-by: Jonathon Anderson <janderson@ciq.com>
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
package imprt
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
@@ -27,56 +25,16 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not read: %s", err)
|
||||
}
|
||||
if !ImportCSV {
|
||||
err = yaml.Unmarshal(buffer, importMap)
|
||||
if err == nil {
|
||||
if setYes || util.Confirm(fmt.Sprintf("Are you sure you want to modify %d nodes", len(importMap))) {
|
||||
err = apinode.NodeAddFromYaml(&wwapiv1.NodeYaml{NodeConfMapYaml: string(buffer)})
|
||||
if err != nil {
|
||||
return fmt.Errorf("got following problem when writing back yaml: %s", err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
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()
|
||||
|
||||
err = yaml.Unmarshal(buffer, importMap)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not parse import file: %s", err)
|
||||
}
|
||||
|
||||
if setYes || util.Confirm(fmt.Sprintf("Are you sure you want to modify %d nodes", len(importMap))) {
|
||||
err = apinode.NodeAddFromYaml(&wwapiv1.NodeYaml{NodeConfMapYaml: string(buffer)})
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not parse %s: %s", args[0], err)
|
||||
}
|
||||
if len(records) < 1 || len(records[0]) < 1 {
|
||||
return fmt.Errorf("did not find any data in %s", args[0])
|
||||
}
|
||||
if !(records[0][0] == "node" || records[0][0] == "nodename") {
|
||||
Usage()
|
||||
os.Exit(1)
|
||||
}
|
||||
argsLen := len(records[0])
|
||||
for i, line := range records[1:] {
|
||||
if len(line) != argsLen {
|
||||
return fmt.Errorf("wrong number of fields in lube %d", i+1)
|
||||
}
|
||||
for j := range line {
|
||||
if j == 0 {
|
||||
continue
|
||||
}
|
||||
if importMap[line[0]] == nil {
|
||||
importMap[line[0]] = new(node.Node)
|
||||
}
|
||||
}
|
||||
}
|
||||
if setYes || util.Confirm(fmt.Sprintf("Are you sure you want to import %d nodes", len(importMap))) {
|
||||
// create second buffer an marshall nodeMap to it
|
||||
buffer, err = yaml.Marshal(importMap)
|
||||
if err != nil {
|
||||
return fmt.Errorf("got following problem when creating yaml: %s", err)
|
||||
}
|
||||
err = apinode.NodeAddFromYaml(&wwapiv1.NodeYaml{NodeConfMapYaml: string(buffer)})
|
||||
if err != nil {
|
||||
return fmt.Errorf("got following problem when writing back yaml: %s", err)
|
||||
}
|
||||
return fmt.Errorf("got following problem when writing back yaml: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package imprt
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -11,22 +9,15 @@ var (
|
||||
DisableFlagsInUseLine: true,
|
||||
Use: "import [OPTIONS] FILE",
|
||||
Short: "Import node(s) from yaml FILE",
|
||||
Long: "This command imports all the nodes defined in a file. It will overwrite nodes with same name.",
|
||||
Long: "This command imports all the nodes defined in a YAML file. It will overwrite nodes with same name.",
|
||||
RunE: CobraRunE,
|
||||
Args: cobra.ExactArgs(1),
|
||||
Aliases: []string{"import"},
|
||||
}
|
||||
ImportCSV bool
|
||||
setYes bool
|
||||
setYes bool
|
||||
)
|
||||
|
||||
func init() {
|
||||
baseCmd.PersistentFlags().BoolVarP(&ImportCSV, "csv", "c", false, "Import CSV file")
|
||||
baseCmd.Flags().BoolVar(&ImportCSV, "cvs", false, "Import CSV file")
|
||||
baseCmd.Flags().Lookup("cvs").Hidden = true
|
||||
if err := baseCmd.Flags().MarkDeprecated("cvs", "use --csv instead"); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
baseCmd.PersistentFlags().BoolVarP(&setYes, "yes", "y", false, "Set 'yes' to all questions asked")
|
||||
}
|
||||
|
||||
@@ -34,9 +25,3 @@ func init() {
|
||||
func GetCommand() *cobra.Command {
|
||||
return baseCmd
|
||||
}
|
||||
|
||||
func Usage() {
|
||||
fmt.Println(`The csv file must be structured in following way:
|
||||
node,option1,option2,net.netname1.netopt
|
||||
node01,value1,value2,net.netname1,netvalue`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user