diff --git a/cmd/container_runtime/crio.go b/cmd/container_runtime/crio.go index f355663..f946416 100644 --- a/cmd/container_runtime/crio.go +++ b/cmd/container_runtime/crio.go @@ -38,7 +38,7 @@ func init() { } func NewCrioCommand() *cobra.Command { - var defaultZotConfig *registry.DefaultZotConfig + var defaultZotConfig registry.DefaultZotConfig var generateConfig bool var crioConfigPath string @@ -46,15 +46,14 @@ func NewCrioCommand() *cobra.Command { Use: "crio", Short: "Creates the config file for the crio runtime to fetch the images from the local repository", PersistentPreRunE: func(cmd *cobra.Command, args []string) error { - return SetupContainerRuntimeCommand(cmd, defaultZotConfig, DefaultCrioGenPath) + return SetupContainerRuntimeCommand(cmd, &defaultZotConfig, DefaultCrioGenPath) }, RunE: func(cmd *cobra.Command, args []string) error { log := logger.FromContext(cmd.Context()) if generateConfig { log.Info().Msg("Generating the config file for crio ...") log.Info().Msgf("Fetching crio registry config file form path: %s", crioConfigPath) - // Generate the config file - err := GenerateCrioRegistryConfig(defaultZotConfig, crioConfigPath, log) + err := GenerateCrioRegistryConfig(&defaultZotConfig, crioConfigPath, log) if err != nil { log.Err(err).Msg("Error generating crio registry config") return err @@ -163,7 +162,7 @@ func SetupContainerRuntimeCommand(cmd *cobra.Command, defaultZotConfig *registry defaultZotConfig.RemoteURL = config.GetRemoteRegistryURL() } else { log.Info().Msg("Using default registry for config generation") - defaultZotConfig, err = registry.ReadConfig(config.GetZotConfigPath()) + err = registry.ReadConfig(config.GetZotConfigPath(), defaultZotConfig) if err != nil || defaultZotConfig == nil { return fmt.Errorf("could not read config: %w", err) } diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 0ff9453..0439572 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -50,7 +50,8 @@ func HandleOwnRegistry() error { // LaunchDefaultZotRegistry launches the default Zot registry using the Zot config path func LaunchDefaultZotRegistry() error { - defaultZotConfig, err := registry.ReadConfig(config.GetZotConfigPath()) + var defaultZotConfig registry.DefaultZotConfig + err := registry.ReadConfig(config.GetZotConfigPath(), &defaultZotConfig) if err != nil { return fmt.Errorf("error reading config: %w", err) } diff --git a/registry/default_config.go b/registry/default_config.go index dc25f82..2c9684e 100644 --- a/registry/default_config.go +++ b/registry/default_config.go @@ -32,26 +32,25 @@ func (c *DefaultZotConfig) GetLocalRegistryURL() string { } // ReadConfig reads a JSON file from the specified path and unmarshals it into a Config struct. -func ReadConfig(filePath string) (*DefaultZotConfig, error) { +func ReadConfig(filePath string, zotConfig *DefaultZotConfig) (error) { file, err := os.Open(filePath) if err != nil { - return nil, fmt.Errorf("could not open file: %w", err) + return fmt.Errorf("could not open file: %w", err) } defer file.Close() // Read the file contents bytes, err := io.ReadAll(file) if err != nil { - return nil, fmt.Errorf("could not read file: %w", err) + return fmt.Errorf("could not read file: %w", err) } // Unmarshal the JSON into a Config struct - var config DefaultZotConfig - err = json.Unmarshal(bytes, &config) + err = json.Unmarshal(bytes, &zotConfig) if err != nil { - return nil, fmt.Errorf("could not unmarshal JSON: %w", err) + return fmt.Errorf("could not unmarshal JSON: %w", err) } - return &config, nil + return nil } func (c *DefaultZotConfig) SetZotRemoteURL(url string) {