<commit_before>package main

import (
	"time"

	"github.com/banzaicloud/bank-vaults/pkg/vault"
	"github.com/fsnotify/fsnotify"
	"github.com/hashicorp/vault/api"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

const cfgVaultConfigFile = "vault-config-file"

var configureCmd = &cobra.Command{
	Use:   "configure",
	Short: "Configures a Hashicorp Vault based on a YAML/JSON configuration file",
	Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command.`,
	Run: func(cmd *cobra.Command, args []string) {
		appConfig.BindPFlag(cfgUnsealPeriod, cmd.PersistentFlags().Lookup(cfgUnsealPeriod))
		appConfig.BindPFlag(cfgVaultConfigFile, cmd.PersistentFlags().Lookup(cfgVaultConfigFile))

		unsealConfig.unsealPeriod = appConfig.GetDuration(cfgUnsealPeriod)
		vaultConfigFile := appConfig.GetString(cfgVaultConfigFile)

		store, err := kvStoreForConfig(appConfig)

		if err != nil {
			logrus.Fatalf("error creating kv store: %s", err.Error())
		}

		cl, err := api.NewClient(nil)

		if err != nil {
			logrus.Fatalf("error connecting to vault: %s", err.Error())
		}

		if err != nil {
			logrus.Fatalf("error building vault config: %s", err.Error())
		}

		vaultConfig, err := vaultConfigForConfig(appConfig)

		v, err := vault.New(store, cl, vaultConfig)

		if err != nil {
			logrus.Fatalf("error creating vault helper: %s", err.Error())
		}

		c := make(chan fsnotify.Event, 1)
		viper.SetConfigFile(vaultConfigFile)
		viper.WatchConfig()
		viper.OnConfigChange(func(e fsnotify.Event) {
			c <- e
		})
		err = viper.ReadInConfig()
		if err != nil {
			logrus.Fatalf("error reading vault config file: %s", err.Error())
		}

		c <- fsnotify.Event{Name: "Initial", Op: fsnotify.Create}

		for e := range c {
			logrus.Infoln("New config file change", e.String())
			func() {
				for {
					logrus.Infof("checking if vault is sealed...")
					sealed, err := v.Sealed()
					if err != nil {
						logrus.Errorf("error checking if vault is sealed: %s", err.Error())
						return
					}

					// If vault is not sealed, we stop here and wait another unsealPeriod
					if sealed {
						logrus.Infof("vault is sealed, waiting %s before trying again...", unsealConfig.unsealPeriod)
						time.Sleep(unsealConfig.unsealPeriod)
						continue
					}
					logrus.Infof("vault is not sealed, configuring...")

					if err = v.Configure(); err != nil {
						logrus.Errorf("error configuring vault: %s", err.Error())
						return
					}

					logrus.Infof("successfully configured vault")
					return
				}
			}()
		}
	},
}<commit_msg>Add possibility to retry if Vault is not yet initialized<commit_after>package main

import (
	"time"

	"github.com/banzaicloud/bank-vaults/pkg/vault"
	"github.com/fsnotify/fsnotify"
	"github.com/hashicorp/vault/api"
	"github.com/sirupsen/logrus"
	"github.com/spf13/cobra"
	"github.com/spf13/viper"
)

const cfgVaultConfigFile = "vault-config-file"

var configureCmd = &cobra.Command{
	Use:   "configure",
	Short: "Configures a Hashicorp Vault based on a YAML/JSON configuration file",
	Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command.`,
	Run: func(cmd *cobra.Command, args []string) {
		appConfig.BindPFlag(cfgUnsealPeriod, cmd.PersistentFlags().Lookup(cfgUnsealPeriod))
		appConfig.BindPFlag(cfgVaultConfigFile, cmd.PersistentFlags().Lookup(cfgVaultConfigFile))

		unsealConfig.unsealPeriod = appConfig.GetDuration(cfgUnsealPeriod)
		vaultConfigFile := appConfig.GetString(cfgVaultConfigFile)

		store, err := kvStoreForConfig(appConfig)

		if err != nil {
			logrus.Fatalf("error creating kv store: %s", err.Error())
		}

		cl, err := api.NewClient(nil)

		if err != nil {
			logrus.Fatalf("error connecting to vault: %s", err.Error())
		}

		if err != nil {
			logrus.Fatalf("error building vault config: %s", err.Error())
		}

		vaultConfig, err := vaultConfigForConfig(appConfig)

		v, err := vault.New(store, cl, vaultConfig)

		if err != nil {
			logrus.Fatalf("error creating vault helper: %s", err.Error())
		}

		c := make(chan fsnotify.Event, 1)
		viper.SetConfigFile(vaultConfigFile)
		viper.WatchConfig()
		viper.OnConfigChange(func(e fsnotify.Event) {
			c <- e
		})
		err = viper.ReadInConfig()
		if err != nil {
			logrus.Fatalf("error reading vault config file: %s", err.Error())
		}

		c <- fsnotify.Event{Name: "Initial", Op: fsnotify.Create}

		for e := range c {
			logrus.Infoln("New config file change", e.String())
			func() {
				for {
					logrus.Infof("checking if vault is sealed...")
					sealed, err := v.Sealed()
					if err != nil {
						logrus.Errorf("error checking if vault is sealed: %s, waiting %s before trying again...", err.Error(), unsealConfig.unsealPeriod)
						time.Sleep(unsealConfig.unsealPeriod)
						continue
					}

					// If vault is not sealed, we stop here and wait another unsealPeriod
					if sealed {
						logrus.Infof("vault is sealed, waiting %s before trying again...", unsealConfig.unsealPeriod)
						time.Sleep(unsealConfig.unsealPeriod)
						continue
					}
					logrus.Infof("vault is not sealed, configuring...")

					if err = v.Configure(); err != nil {
						logrus.Errorf("error configuring vault: %s", err.Error())
						return
					}

					logrus.Infof("successfully configured vault")
					return
				}
			}()
		}
	},
}
{'fraction_non_alphanumeric': 0.13116279069767442, 'fraction_numerical': 0.0018604651162790699, 'mean_word_length': 3.5753191489361704, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3716585', 'n_tokens_mistral': 2135, 'n_tokens_neox': 1818, 'n_words': 442}