8000 Add export command to all items by SchoolGuy · Pull Request #70 · cobbler/cli · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add export command to all items #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions cmd/distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
package cmd

import (
"encoding/json"
"fmt"
cobbler "github.com/cobbler/cobblerclient"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
)

func updateDistroFromFlags(cmd *cobra.Command, distro *cobbler.Distro) error {
Expand Down Expand Up @@ -366,6 +368,7 @@ See https://cobbler.readthedocs.io/en/latest/cobbler.html#cobbler-distro for mor
return nil, err
}
distroCmd.AddCommand(distroReportCmd)
distroCmd.AddCommand(NewDistroExportCmd())
return distroCmd, nil
}

Expand Down Expand Up @@ -678,3 +681,72 @@ func NewDistroReportCmd() (*cobra.Command, error) {
distroReportCmd.Flags().String("name", "", "the distro name")
return distroReportCmd, nil
}

func NewDistroExportCmd() *cobra.Command {
distroExportCmd := &cobra.Command{
Use: "export",
Short: "export distributions",
Long: `Export distributions.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
formatOption, err := cmd.Flags().GetString("format")
if err != nil {
return err
}
if formatOption != "json" && formatOption != "yaml" {
return fmt.Errorf("format must be json or yaml")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
err := generateCobblerClient()
if err != nil {
return err
}

name, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
formatOption, err := cmd.Flags().GetString("format")
if err != nil {
return err
}

itemNames := make([]string, 0)
if name == "" {
itemNames, err = Client.ListDistroNames()
if err != nil {
return err
}
} else {
itemNames = append(itemNames, name)
}

for _, itemName := range itemNames {
distro, err := Client.GetDistro(itemName, false, false)
if err != nil {
return err
}
if formatOption == "json" {
jsonDocument, err := json.Marshal(distro)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(jsonDocument))
}
if formatOption == "yaml" {
yamlDocument, err := yaml.Marshal(distro)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), "---")
fmt.Fprintln(cmd.OutOrStdout(), string(yamlDocument))
}
}
return nil
},
}
distroExportCmd.Flags().String("name", "", "the distro name")
distroExportCmd.Flags().String(exportStringMetadata["format"].Name, exportStringMetadata["format"].DefaultValue, exportStringMetadata["format"].Usage)
return distroExportCmd
}
76 changes: 74 additions & 2 deletions cmd/file.go
8000
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
package cmd

import (
"encoding/json"
"fmt"
cobbler "github.com/cobbler/cobblerclient"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
)

func updateFileFromFlags(cmd *cobra.Command, file *cobbler.File) error {
Expand Down Expand Up @@ -103,7 +105,7 @@ func updateFileFromFlags(cmd *cobra.Command, file *cobbler.File) error {
}

// NewFileCmd builds a new command that represents the file action
func NewFileCmd() *cobra.Command {
func NewFileCmd() (*cobra.Command, error) {
fileCmd := &cobra.Command{
Use: "file",
Short: "File management",
Expand All @@ -121,7 +123,8 @@ See https://cobbler.readthedocs.io/en/latest/cobbler.html#cobbler-file for more
fileCmd.AddCommand(NewFileRemoveCmd())
fileCmd.AddCommand(NewFileRenameCmd())
fileCmd.AddCommand(NewFileReportCmd())
return fileCmd
fileCmd.AddCommand(NewFileExportCmd())
return fileCmd, nil
}

func NewFileAddCmd() *cobra.Command {
Expand Down Expand Up @@ -405,3 +408,72 @@ func NewFileReportCmd() *cobra.Command {
fileReportCmd.Flags().String("name", "", "the file name")
return fileReportCmd
}

func NewFileExportCmd() *cobra.Command {
fileExportCmd := &cobra.Command{
Use: "export",
Short: "export files",
Long: `Export files.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
formatOption, err := cmd.Flags().GetString("format")
if err != nil {
return err
}
if formatOption != "json" && formatOption != "yaml" {
return fmt.Errorf("format must be json or yaml")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
err := generateCobblerClient()
if err != nil {
return err
}

name, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
formatOption, err := cmd.Flags().GetString("format")
if err != nil {
return err
}

itemNames := make([]string, 0)
if name == "" {
itemNames, err = Client.ListFileNames()
if err != nil {
return err
}
} else {
itemNames = append(itemNames, name)
}

for _, itemName := range itemNames {
file, err := Client.GetFile(itemName, false, false)
if err != nil {
return err
}
if formatOption == "json" {
jsonDocument, err := json.Marshal(file)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(jsonDocument))
}
if formatOption == "yaml" {
yamlDocument, err := yaml.Marshal(file)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), "---")
fmt.Fprintln(cmd.OutOrStdout(), string(yamlDocument))
}
}
return nil
},
}
fileExportCmd.Flags().String("name", "", "the file name")
fileExportCmd.Flags().String(exportStringMetadata["format"].Name, exportStringMetadata["format"].DefaultValue, exportStringMetadata["format"].Usage)
return fileExportCmd
}
76 changes: 74 additions & 2 deletions cmd/image.go
9E81
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
package cmd

import (
"encoding/json"
"fmt"
cobbler "github.com/cobbler/cobblerclient"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
)

func updateImageFromFlags(cmd *cobra.Command, image *cobbler.Image) error {
Expand Down Expand Up @@ -348,7 +350,7 @@ func updateImageFromFlags(cmd *cobra.Command, image *cobbler.Image) error {
}

// NewImageCmd builds a new command that represents the image action
func NewImageCmd() *cobra.Command {
func NewImageCmd() (*cobra.Command, error) {
imageCmd := &cobra.Command{
Use: "image",
Short: "Image management",
Expand All @@ -366,7 +368,8 @@ See https://cobbler.readthedocs.io/en/latest/cobbler.html#cobbler-image for more
imageCmd.AddCommand(NewImageRemoveCmd())
imageCmd.AddCommand(NewImageRenameCmd())
imageCmd.AddCommand(NewImageReportCmd())
return imageCmd
imageCmd.AddCommand(NewImageExportCmd())
return imageCmd, nil
}

func NewImageAddCmd() *cobra.Command {
Expand Down Expand Up @@ -657,3 +660,72 @@ func NewImageReportCmd() *cobra.Command {
imageReportCmd.Flags().String("name", "", "the image name")
return imageReportCmd
}

func NewImageExportCmd() *cobra.Command {
imageExportCmd := &cobra.Command{
Use: "export",
Short: "export images",
Long: `Export images.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
formatOption, err := cmd.Flags().GetString("format")
if err != nil {
return err
}
if formatOption != "json" && formatOption != "yaml" {
return fmt.Errorf("format must be json or yaml")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
err := generateCobblerClient()
if err != nil {
return err
}

name, err := cmd.Flags().GetString("name")
if err != nil {
return err
}
formatOption, err := cmd.Flags().GetString("format")
if err != nil {
return err
}

itemNames := make([]string, 0)
if name == "" {
itemNames, err = Client.ListImageNames()
if err != nil {
return err
}
} else {
itemNames = append(itemNames, name)
}

for _, itemName := range itemNames {
image, err := Client.GetImage(itemName, false, false)
if err != nil {
return err
}
if formatOption == "json" {
jsonDocument, err := json.Marshal(image)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), string(jsonDocument))
}
if formatOption == "yaml" {
yamlDocument, err := yaml.Marshal(image)
if err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), "---")
fmt.Fprintln(cmd.OutOrStdout(), string(yamlDocument))
}
}
return nil
},
}
imageExportCmd.Flags().String("name", "", "the image name")
imageExportCmd.Flags().String(exportStringMetadata["format"].Name, exportStringMetadata["format"].DefaultValue, exportStringMetadata["format"].Usage)
return imageExportCmd
}
Loading
Loading
0