github.com/gogf/gf/v2@v2.7.4/util/gmode/gmode.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // Package gmode provides release mode management for project.
     8  //
     9  // It uses string to mark the mode instead of integer, which is convenient for configuration.
    10  package gmode
    11  
    12  import (
    13  	"github.com/gogf/gf/v2/debug/gdebug"
    14  	"github.com/gogf/gf/v2/internal/command"
    15  	"github.com/gogf/gf/v2/os/gfile"
    16  )
    17  
    18  const (
    19  	NOT_SET       = "not-set"
    20  	DEVELOP       = "develop"
    21  	TESTING       = "testing"
    22  	STAGING       = "staging"
    23  	PRODUCT       = "product"
    24  	commandEnvKey = "gf.gmode"
    25  )
    26  
    27  var (
    28  	// Note that `currentMode` is not concurrent safe.
    29  	currentMode = NOT_SET
    30  )
    31  
    32  // Set sets the mode for current application.
    33  func Set(mode string) {
    34  	currentMode = mode
    35  }
    36  
    37  // SetDevelop sets current mode DEVELOP for current application.
    38  func SetDevelop() {
    39  	Set(DEVELOP)
    40  }
    41  
    42  // SetTesting sets current mode TESTING for current application.
    43  func SetTesting() {
    44  	Set(TESTING)
    45  }
    46  
    47  // SetStaging sets current mode STAGING for current application.
    48  func SetStaging() {
    49  	Set(STAGING)
    50  }
    51  
    52  // SetProduct sets current mode PRODUCT for current application.
    53  func SetProduct() {
    54  	Set(PRODUCT)
    55  }
    56  
    57  // Mode returns current application mode set.
    58  func Mode() string {
    59  	// If current mode is not set, do this auto check.
    60  	if currentMode == NOT_SET {
    61  		if v := command.GetOptWithEnv(commandEnvKey); v != "" {
    62  			// Mode configured from command argument of environment.
    63  			currentMode = v
    64  		} else {
    65  			// If there are source codes found, it's in develop mode, or else in product mode.
    66  			if gfile.Exists(gdebug.CallerFilePath()) {
    67  				currentMode = DEVELOP
    68  			} else {
    69  				currentMode = PRODUCT
    70  			}
    71  		}
    72  	}
    73  	return currentMode
    74  }
    75  
    76  // IsDevelop checks and returns whether current application is running in DEVELOP mode.
    77  func IsDevelop() bool {
    78  	return Mode() == DEVELOP
    79  }
    80  
    81  // IsTesting checks and returns whether current application is running in TESTING mode.
    82  func IsTesting() bool {
    83  	return Mode() == TESTING
    84  }
    85  
    86  // IsStaging checks and returns whether current application is running in STAGING mode.
    87  func IsStaging() bool {
    88  	return Mode() == STAGING
    89  }
    90  
    91  // IsProduct checks and returns whether current application is running in PRODUCT mode.
    92  func IsProduct() bool {
    93  	return Mode() == PRODUCT
    94  }