2022-09-16 04:21:05 +00:00
package main
import (
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/authelia/authelia/v4/internal/utils"
)
var rootCmd * cobra . Command
func init ( ) {
rootCmd = newRootCmd ( )
}
func newRootCmd ( ) * cobra . Command {
cmd := & cobra . Command {
Use : cmdUseRoot ,
Short : "Authelia's generator tooling" ,
RunE : rootSubCommandsRunE ,
DisableAutoGenTag : true ,
}
cmd . PersistentFlags ( ) . StringP ( cmdFlagCwd , "C" , "" , "Sets the CWD for git commands" )
cmd . PersistentFlags ( ) . StringP ( cmdFlagRoot , "d" , dirCurrent , "The repository root" )
2023-01-26 03:17:50 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagWeb , dirWeb , "The repository web directory in relation to the root directory" )
2022-09-16 04:21:05 +00:00
cmd . PersistentFlags ( ) . StringSliceP ( cmdFlagExclude , "X" , nil , "Sets the names of excluded generators" )
cmd . PersistentFlags ( ) . String ( cmdFlagFeatureRequest , fileGitHubIssueTemplateFR , "Sets the path of the feature request issue template file" )
cmd . PersistentFlags ( ) . String ( cmdFlagBugReport , fileGitHubIssueTemplateBR , "Sets the path of the bug report issue template file" )
cmd . PersistentFlags ( ) . Int ( cmdFlagVersions , 5 , "the maximum number of minor versions to list in output templates" )
cmd . PersistentFlags ( ) . String ( cmdFlagDirLocales , dirLocales , "The locales directory in relation to the root" )
2023-01-26 03:17:50 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagFileWebI18N , fileWebI18NIndex , "The i18n typescript configuration file in relation to the web directory" )
cmd . PersistentFlags ( ) . String ( cmdFlagFileWebPackage , fileWebPackage , "The node package configuration file in relation to the web directory" )
2022-09-16 04:21:05 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagDocsDataLanguages , fileDocsDataLanguages , "The languages docs data file in relation to the docs data folder" )
2022-10-22 11:19:32 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagDocsDataMisc , fileDocsDataMisc , "The misc docs data file in relation to the docs data folder" )
2022-09-16 04:21:05 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagDocsCLIReference , dirDocsCLIReference , "The directory to store the markdown in" )
2022-10-22 11:19:32 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagDocs , dirDocs , "The directory with the docs" )
2022-09-16 04:21:05 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagDocsContent , dirDocsContent , "The directory with the docs content" )
2022-10-22 11:19:32 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagDocsData , dirDocsData , "The directory with the docs data" )
2022-09-16 04:21:05 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagFileConfigKeys , fileCodeConfigKeys , "Sets the path of the keys file" )
2022-10-22 11:19:32 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagDocsDataKeys , fileDocsDataConfigKeys , "Sets the path of the docs keys file" )
2022-09-16 04:21:05 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagPackageConfigKeys , pkgConfigSchema , "Sets the package name of the keys file" )
cmd . PersistentFlags ( ) . String ( cmdFlagFileScriptsGen , fileScriptsGen , "Sets the path of the authelia-scripts gen file" )
2022-10-22 11:19:32 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagFileServerGenerated , fileServerGenerated , "Sets the path of the server generated file" )
2022-09-16 04:21:05 +00:00
cmd . PersistentFlags ( ) . String ( cmdFlagPackageScriptsGen , pkgScriptsGen , "Sets the package name of the authelia-scripts gen file" )
cmd . PersistentFlags ( ) . String ( cmdFlagFileConfigCommitLint , fileCICommitLintConfig , "The commit lint javascript configuration file in relation to the root" )
cmd . PersistentFlags ( ) . String ( cmdFlagFileDocsCommitMsgGuidelines , fileDocsCommitMessageGuidelines , "The commit message guidelines documentation file in relation to the root" )
cmd . AddCommand ( newCodeCmd ( ) , newDocsCmd ( ) , newGitHubCmd ( ) , newLocalesCmd ( ) , newCommitLintCmd ( ) )
return cmd
}
func rootSubCommandsRunE ( cmd * cobra . Command , args [ ] string ) ( err error ) {
var exclude [ ] string
if exclude , err = cmd . Flags ( ) . GetStringSlice ( cmdFlagExclude ) ; err != nil {
return err
}
2023-05-30 09:52:50 +00:00
subCmds := sortCmds ( cmd )
for _ , subCmd := range subCmds {
if subCmd . Use == cmdUseCompletion || strings . HasPrefix ( subCmd . Use , "help " ) || utils . IsStringSliceContainsAny ( [ ] string { resolveCmdName ( subCmd ) , subCmd . Use } , exclude ) {
continue
}
rootCmd . SetArgs ( rootCmdGetArgs ( subCmd , args ) )
if err = rootCmd . Execute ( ) ; err != nil {
return err
}
}
return nil
}
func sortCmds ( cmd * cobra . Command ) [ ] * cobra . Command {
2022-09-16 04:21:05 +00:00
subCmds := cmd . Commands ( )
switch cmd . Use {
case cmdUseRoot :
sort . Slice ( subCmds , func ( i , j int ) bool {
switch subCmds [ j ] . Use {
case cmdUseDocs :
// Ensure `docs` subCmd is last.
return true
default :
return subCmds [ i ] . Use < subCmds [ j ] . Use
}
} )
case cmdUseDocs :
sort . Slice ( subCmds , func ( i , j int ) bool {
switch subCmds [ j ] . Use {
case cmdUseDocsDate :
// Ensure `date` subCmd is last.
return true
default :
return subCmds [ i ] . Use < subCmds [ j ] . Use
}
} )
default :
sort . Slice ( subCmds , func ( i , j int ) bool {
return subCmds [ i ] . Use < subCmds [ j ] . Use
} )
}
2023-05-30 09:52:50 +00:00
return subCmds
2022-09-16 04:21:05 +00:00
}
func resolveCmdName ( cmd * cobra . Command ) string {
parent := cmd . Parent ( )
if parent != nil && parent . Use != cmd . Use && parent . Use != cmdUseRoot {
return resolveCmdName ( parent ) + "." + cmd . Use
}
return cmd . Use
}
func rootCmdGetArgs ( cmd * cobra . Command , args [ ] string ) [ ] string {
for {
2023-05-30 09:52:50 +00:00
if cmd == nil || cmd == rootCmd {
2022-09-16 04:21:05 +00:00
break
}
args = append ( [ ] string { cmd . Use } , args ... )
cmd = cmd . Parent ( )
}
return args
}