code.gitea.io/gitea@v1.22.3/modules/setting/config.go (about)

     1  // Copyright 2023 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package setting
     5  
     6  import (
     7  	"sync"
     8  
     9  	"code.gitea.io/gitea/modules/log"
    10  	"code.gitea.io/gitea/modules/setting/config"
    11  )
    12  
    13  type PictureStruct struct {
    14  	DisableGravatar       *config.Value[bool]
    15  	EnableFederatedAvatar *config.Value[bool]
    16  }
    17  
    18  type OpenWithEditorApp struct {
    19  	DisplayName string
    20  	OpenURL     string
    21  }
    22  
    23  type OpenWithEditorAppsType []OpenWithEditorApp
    24  
    25  func (t OpenWithEditorAppsType) ToTextareaString() string {
    26  	ret := ""
    27  	for _, app := range t {
    28  		ret += app.DisplayName + " = " + app.OpenURL + "\n"
    29  	}
    30  	return ret
    31  }
    32  
    33  func DefaultOpenWithEditorApps() OpenWithEditorAppsType {
    34  	return OpenWithEditorAppsType{
    35  		{
    36  			DisplayName: "VS Code",
    37  			OpenURL:     "vscode://vscode.git/clone?url={url}",
    38  		},
    39  		{
    40  			DisplayName: "VSCodium",
    41  			OpenURL:     "vscodium://vscode.git/clone?url={url}",
    42  		},
    43  		{
    44  			DisplayName: "Intellij IDEA",
    45  			OpenURL:     "jetbrains://idea/checkout/git?idea.required.plugins.id=Git4Idea&checkout.repo={url}",
    46  		},
    47  	}
    48  }
    49  
    50  type RepositoryStruct struct {
    51  	OpenWithEditorApps *config.Value[OpenWithEditorAppsType]
    52  }
    53  
    54  type ConfigStruct struct {
    55  	Picture    *PictureStruct
    56  	Repository *RepositoryStruct
    57  }
    58  
    59  var (
    60  	defaultConfig     *ConfigStruct
    61  	defaultConfigOnce sync.Once
    62  )
    63  
    64  func initDefaultConfig() {
    65  	config.SetCfgSecKeyGetter(&cfgSecKeyGetter{})
    66  	defaultConfig = &ConfigStruct{
    67  		Picture: &PictureStruct{
    68  			DisableGravatar:       config.ValueJSON[bool]("picture.disable_gravatar").WithFileConfig(config.CfgSecKey{Sec: "picture", Key: "DISABLE_GRAVATAR"}),
    69  			EnableFederatedAvatar: config.ValueJSON[bool]("picture.enable_federated_avatar").WithFileConfig(config.CfgSecKey{Sec: "picture", Key: "ENABLE_FEDERATED_AVATAR"}),
    70  		},
    71  		Repository: &RepositoryStruct{
    72  			OpenWithEditorApps: config.ValueJSON[OpenWithEditorAppsType]("repository.open-with.editor-apps"),
    73  		},
    74  	}
    75  }
    76  
    77  func Config() *ConfigStruct {
    78  	defaultConfigOnce.Do(initDefaultConfig)
    79  	return defaultConfig
    80  }
    81  
    82  type cfgSecKeyGetter struct{}
    83  
    84  func (c cfgSecKeyGetter) GetValue(sec, key string) (v string, has bool) {
    85  	if key == "" {
    86  		return "", false
    87  	}
    88  	cfgSec, err := CfgProvider.GetSection(sec)
    89  	if err != nil {
    90  		log.Error("Unable to get config section: %q", sec)
    91  		return "", false
    92  	}
    93  	cfgKey := ConfigSectionKey(cfgSec, key)
    94  	if cfgKey == nil {
    95  		return "", false
    96  	}
    97  	return cfgKey.Value(), true
    98  }