github.com/cloudwego/kitex@v0.9.0/pkg/utils/config.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  // Package utils contains utils.
    18  package utils
    19  
    20  import (
    21  	"os"
    22  	"path"
    23  )
    24  
    25  // Predefined env variables and default configurations.
    26  const (
    27  	EnvConfDir  = "KITEX_CONF_DIR"
    28  	EnvConfFile = "KITEX_CONF_FILE"
    29  	EnvLogDir   = "KITEX_LOG_DIR"
    30  
    31  	DefaultConfDir  = "conf"
    32  	DefaultConfFile = "kitex.yml"
    33  	DefaultLogDir   = "log"
    34  )
    35  
    36  // GetConfDir gets dir of config file.
    37  func GetConfDir() string {
    38  	if confDir := os.Getenv(EnvConfDir); confDir != "" {
    39  		return confDir
    40  	}
    41  	return DefaultConfDir
    42  }
    43  
    44  // GetConfFile gets config file path.
    45  func GetConfFile() string {
    46  	file := DefaultConfFile
    47  	if confFile := os.Getenv(EnvConfFile); confFile != "" {
    48  		file = confFile
    49  	}
    50  	return path.Join(GetConfDir(), file)
    51  }
    52  
    53  // GetEnvLogDir is to get log dir from env.
    54  func GetEnvLogDir() string {
    55  	return os.Getenv(EnvLogDir)
    56  }
    57  
    58  // GetLogDir gets dir of log file.
    59  // Deprecated: it is suggested to use GetEnvLogDir instead of GetLogDir, and GetEnvLogDir won't return default log dir.
    60  func GetLogDir() string {
    61  	if logDir := os.Getenv(EnvLogDir); logDir != "" {
    62  		return logDir
    63  	}
    64  	return DefaultLogDir
    65  }