Skip to content
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

Support configuration file for export command #55

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
7 changes: 5 additions & 2 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var exportCmd = &cobra.Command{
Run: runExport,
}

var config string
var channels []string
var channelWithChildren []string
var configChannels []string
Expand All @@ -27,6 +28,7 @@ var includeContainers bool
var orgs []uint

func init() {
exportCmd.Flags().StringVar(&config, "config", "", "Location of configuration file")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a nice approach. I'd like it to be a bit more explicit on the user interface level, that we for example can either provide a config or specify it as a set of args. This way we could probably not mix them together with the options. What do you think?

Copy link
Author

@WordlessEcho WordlessEcho Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@avshiliaev Sorry. I didn't catch your point... Can you give me some examples?
Or I guess that you hope the --config could be global flag as same as --serverConfig?

exportCmd.Flags().StringSliceVar(&channels, "channels", nil, "Channels to be exported")
exportCmd.Flags().StringSliceVar(&channelWithChildren, "channel-with-children", nil, "Channels to be exported")
exportCmd.Flags().StringVar(&outputDir, "outputDir", ".", "Location for generated data")
Expand Down Expand Up @@ -63,9 +65,10 @@ func runExport(cmd *cobra.Command, args []string) {
Containers: includeContainers,
Orgs: orgs,
}
entityDumper.SetOptionsByConfig(config, &options)
entityDumper.DumpAllEntities(options)
var versionfile string
versionfile = path.Join(utils.GetAbsPath(outputDir), "version.txt")
versionfile = path.Join(utils.GetAbsPath(options.OutputFolder), "version.txt")
vf, err := os.Open(versionfile)
defer vf.Close()
if os.IsNotExist(err) {
Expand All @@ -78,5 +81,5 @@ func runExport(cmd *cobra.Command, args []string) {
version, product := utils.GetCurrentServerVersion(serverConfig)
vf.WriteString("product_name = " + product + "\n" + "version = " + version + "\n")

log.Info().Msgf("Export done. Directory: %s", outputDir)
log.Info().Msgf("Export done. Directory: %s", options.OutputFolder)
}
3 changes: 1 addition & 2 deletions entityDumper/dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package entityDumper
import (
"bufio"
"compress/gzip"
"os"

"github.com/rs/zerolog/log"
"github.com/uyuni-project/inter-server-sync/schemareader"
"os"
)

func DumpAllEntities(options DumperOptions) {
Expand Down
79 changes: 79 additions & 0 deletions entityDumper/exportConfigDumper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package entityDumper

import (
"encoding/json"
"github.com/rs/zerolog/log"
"github.com/uyuni-project/inter-server-sync/utils"
"io"
"os"
)

func SetOptionsByConfig(configPath string, options *DumperOptions) {
var config ExportConfig

if configPath == "" {
return
}

configFile, err := os.Open(utils.GetAbsPath(configPath))
if err != nil {
log.Panic().Err(err).Msg("failed to open config file")
}
defer func(configFile *os.File) {
err := configFile.Close()
if err != nil {
log.Warn().Err(err).Msg("failed to close config file")
}
}(configFile)

configBytes, err := io.ReadAll(configFile)
if err != nil {
log.Panic().Err(err).Msg("failed to read config file")
}

if err = json.Unmarshal(configBytes, &config); err != nil {
log.Panic().Err(err).Msg("failed to unmarshal config file")
}

// ServerConfig is set by cmd/root.go
// Should we change it anyway?
//if options.ServerConfig == "/etc/rhn/rhn.conf" && config.ServerConfig != "" {
// options.ServerConfig = config.ServerConfig
//}

if options.OutputFolder == "." && config.OutputDir != "" {
options.OutputFolder = config.OutputDir
}

if len(options.ChannelLabels) == 0 {
options.ChannelLabels = config.Channels
}

if len(options.ChannelWithChildrenLabels) == 0 {
options.ChannelWithChildrenLabels = config.ChannelWithChildren
}

if len(options.ConfigLabels) == 0 {
options.ConfigLabels = config.ConfigChannels
}

if config.MetadataOnly {
options.MetadataOnly = config.MetadataOnly
}

if options.StartingDate == "" {
options.StartingDate = config.StartingDate
}

if len(options.Orgs) == 0 {
options.Orgs = config.Orgs
}

if config.IncludeImages {
options.OSImages = config.IncludeImages
}

if config.IncludeContainers {
options.Containers = config.IncludeContainers
}
WordlessEcho marked this conversation as resolved.
Show resolved Hide resolved
}
13 changes: 13 additions & 0 deletions entityDumper/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import (
"github.com/uyuni-project/inter-server-sync/utils"
)

type ExportConfig struct {
ServerConfig string
Channels []string
ChannelWithChildren []string
OutputDir string
MetadataOnly bool
StartingDate string
ConfigChannels []string
IncludeImages bool
IncludeContainers bool
Orgs []uint
}
WordlessEcho marked this conversation as resolved.
Show resolved Hide resolved

type DumperOptions struct {
ServerConfig string
ChannelLabels []string
Expand Down