buildConfig builds an appConfig from the CLI context, respecting config file precedence. If a config file exists, its values are used as the base and only explicitly-set CLI flags override them.
(cCtx *cli.Context)
| 113 | // If a config file exists, its values are used as the base and only explicitly-set |
| 114 | // CLI flags override them. |
| 115 | func buildConfig(cCtx *cli.Context) (*appConfig, error) { |
| 116 | configPath := cCtx.String("config") |
| 117 | |
| 118 | // Try to load config file as the base configuration |
| 119 | var c appConfig |
| 120 | configFileLoaded := false |
| 121 | |
| 122 | resolvedPath, pathErr := resolveConfigPath(configPath) |
| 123 | if pathErr == nil { |
| 124 | if _, err := os.Stat(resolvedPath); err == nil { |
| 125 | fc, err := loadConfigFile(configPath) |
| 126 | if err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | c = *fileConfigToAppConfig(fc) |
| 130 | configFileLoaded = true |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if configFileLoaded { |
| 135 | // Only override config file values with flags that were explicitly set |
| 136 | if cCtx.IsSet("service") { |
| 137 | c.service = cCtx.String("service") |
| 138 | } |
| 139 | if cCtx.IsSet("githost.url") { |
| 140 | c.gitHostURL = cCtx.String("githost.url") |
| 141 | } |
| 142 | if cCtx.IsSet("backupdir") { |
| 143 | c.backupDir = cCtx.String("backupdir") |
| 144 | } |
| 145 | if cCtx.IsSet("ignore-private") { |
| 146 | c.ignorePrivate = cCtx.Bool("ignore-private") |
| 147 | } |
| 148 | if cCtx.IsSet("ignore-fork") { |
| 149 | c.ignoreFork = cCtx.Bool("ignore-fork") |
| 150 | } |
| 151 | if cCtx.IsSet("use-https-clone") { |
| 152 | c.useHTTPSClone = cCtx.Bool("use-https-clone") |
| 153 | } |
| 154 | if cCtx.IsSet("bare") { |
| 155 | c.bare = cCtx.Bool("bare") |
| 156 | } |
| 157 | if cCtx.IsSet("github.repoType") { |
| 158 | c.githubRepoType = cCtx.String("github.repoType") |
| 159 | } |
| 160 | if cCtx.IsSet("github.namespaceWhitelist") { |
| 161 | ns := cCtx.String("github.namespaceWhitelist") |
| 162 | if len(ns) > 0 { |
| 163 | c.githubNamespaceWhitelist = strings.Split(ns, ",") |
| 164 | } |
| 165 | } |
| 166 | if cCtx.IsSet("gitlab.projectVisibility") { |
| 167 | c.gitlabProjectVisibility = cCtx.String("gitlab.projectVisibility") |
| 168 | } |
| 169 | if cCtx.IsSet("gitlab.projectMembershipType") { |
| 170 | c.gitlabProjectMembershipType = cCtx.String("gitlab.projectMembershipType") |
| 171 | } |
| 172 | if cCtx.IsSet("forgejo.repoType") { |