github.com/pavlo67/common@v0.5.3/common/config/prepare.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"math/rand"
     7  	"os"
     8  	"path/filepath"
     9  	"regexp"
    10  	"runtime/debug"
    11  	"strconv"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/stretchr/testify/require"
    16  
    17  	"github.com/pavlo67/common/common/colorize"
    18  	"github.com/pavlo67/common/common/filelib"
    19  	"github.com/pavlo67/common/common/logger"
    20  	"github.com/pavlo67/common/common/logger/logger_zap"
    21  	"github.com/pavlo67/common/common/serialization"
    22  )
    23  
    24  func ShowVCSInfo() {
    25  	if bi, ok := debug.ReadBuildInfo(); ok && bi != nil {
    26  		for _, s := range bi.Settings {
    27  			if ok, _ := regexp.MatchString(`^vcs\.`, s.Key); ok {
    28  				fmt.Printf("%s\t%s\n", s.Key, s.Value)
    29  			}
    30  		}
    31  		fmt.Print("\n")
    32  	}
    33  }
    34  
    35  func PrepareApp(envPath, logPath string) (Envs, logger.Operator) {
    36  
    37  	rand.Seed(time.Now().UnixNano())
    38  
    39  	// get config --------------------------------------------------------------------
    40  
    41  	configEnv, ok := os.LookupEnv("ENV")
    42  	if !ok {
    43  		configEnv = "local"
    44  	}
    45  
    46  	envs, err := Get(envPath+configEnv+".yaml", serialization.MarshalerYAML)
    47  	if err != nil || envs == nil {
    48  		log.Fatalf("on PrepareApp(%s, %s) got %#v / %s", envPath, configEnv+".yaml", envs, err)
    49  	}
    50  
    51  	// get logger --------------------------------------------------------------------
    52  
    53  	var saveFiles bool
    54  	if err = envs.Value("logger_save_files", &saveFiles); err != nil {
    55  		fmt.Fprintf(os.Stderr, colorize.Red+"on PrepareApp(%s, %s), reading of 'logger_save_files' key produces the error: %s\n"+colorize.Reset, envPath, configEnv+".yaml", err)
    56  	}
    57  
    58  	if logPath == "" {
    59  		if err = envs.Value("logger_path", &logPath); err != nil {
    60  			fmt.Fprintf(os.Stderr, colorize.Red+"on PrepareApp(%s, %s), reading of 'logger_path' key produces the error: %s\n"+colorize.Reset, envPath, configEnv+".yaml", err)
    61  		}
    62  	}
    63  
    64  	logPath, err = filelib.Dir(logPath)
    65  	if err != nil {
    66  		log.Fatalf("on PrepareApp(%s, %s): can't create log path (%s): %s", envPath, configEnv+".yaml", logPath, err)
    67  	}
    68  
    69  	loggerConfig := logger.Config{
    70  		Key:       strconv.FormatInt(time.Now().Unix(), 10),
    71  		BasePath:  logPath,
    72  		SaveFiles: saveFiles,
    73  	}
    74  
    75  	l, err := logger_zap.New(loggerConfig)
    76  	if err != nil {
    77  		log.Fatal(err)
    78  	}
    79  
    80  	return *envs, l
    81  }
    82  
    83  func PrepareTests(t *testing.T, envPath, logFile string) (Envs, logger.Operator) {
    84  
    85  	configEnv := "test"
    86  	os.Setenv("ENV", configEnv)
    87  
    88  	envsPtr, err := Get(envPath+configEnv+".yaml", serialization.MarshalerYAML)
    89  	require.NoError(t, err)
    90  	require.NotNil(t, envsPtr)
    91  
    92  	var loggerFiles []string
    93  	if logFile != "" {
    94  		loggerFiles = append(loggerFiles, logFile)
    95  	}
    96  
    97  	var loggerSaveFiles bool
    98  	if err = envsPtr.Value("logger_save_files", &loggerSaveFiles); err != nil {
    99  		fmt.Fprintf(os.Stderr, colorize.Red+"on PrepareApp(%s, %s), reading of 'logger_save_files' key produces the error: %s\n"+colorize.Reset, envPath, configEnv+".yaml", err)
   100  	}
   101  
   102  	var loggerPath string
   103  	if err = envsPtr.Value("logger_path", &loggerPath); err != nil {
   104  		fmt.Fprintf(os.Stderr, colorize.Red+"on PrepareApp(%s, %s), reading of 'logger_path' key produces the error: %s\n"+colorize.Reset, envPath, configEnv+".yaml", err)
   105  	}
   106  
   107  	logKey := time.Now().Format(time.RFC3339)[:19]
   108  	logPath, err := filelib.Dir(filepath.Join(loggerPath, logKey))
   109  	require.NoError(t, err)
   110  
   111  	l, err := logger_zap.New(logger.Config{
   112  		Key:         logKey,
   113  		BasePath:    logPath,
   114  		SaveFiles:   loggerSaveFiles,
   115  		LogLevel:    logger.TraceLevel,
   116  		OutputPaths: append(loggerFiles, "stdout"),
   117  		ErrorPaths:  append(loggerFiles, "stderr"),
   118  	})
   119  	require.NoError(t, err)
   120  	require.NotNil(t, l)
   121  
   122  	return *envsPtr, l
   123  
   124  }