split cmd
cmd.go contient maintenant juste les actual commandes. Les fonctionalités liées à la configuration sont dans config.go, et les fonctionalités liées au templating est dans template.go.
This commit is contained in:
parent
cdd526a6f3
commit
780d493dc1
4 changed files with 185 additions and 175 deletions
176
cmd.go
176
cmd.go
|
@ -3,13 +3,10 @@ package main
|
|||
import (
|
||||
"context"
|
||||
"crypto/subtle"
|
||||
"embed"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/labstack/echo/v4"
|
||||
|
@ -18,6 +15,21 @@ import (
|
|||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "bottin",
|
||||
Short: "Bottin étudiant de l'AGECEM",
|
||||
}
|
||||
|
||||
// execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// apiCmd represents the api command
|
||||
var apiCmd = &cobra.Command{
|
||||
Use: "api",
|
||||
|
@ -98,118 +110,6 @@ var apiCmd = &cobra.Command{
|
|||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(apiCmd)
|
||||
|
||||
// api.key
|
||||
apiCmd.Flags().String(FlagAPIKey, DefaultAPIKey, DescriptionAPIKey)
|
||||
if err := viper.BindPFlag(ViperAPIKey, apiCmd.Flags().Lookup(FlagAPIKey)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// api.port
|
||||
apiCmd.Flags().Int(FlagAPIPort, DefaultAPIPort, DescriptionAPIPort)
|
||||
if err := viper.BindPFlag(ViperAPIPort, apiCmd.Flags().Lookup(FlagAPIPort)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.database
|
||||
apiCmd.Flags().String(FlagDBDatabase, DefaultDBDatabase, DescriptionDBDatabase)
|
||||
if err := viper.BindPFlag(ViperDBDatabase, apiCmd.Flags().Lookup(FlagDBDatabase)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.sslmode
|
||||
apiCmd.Flags().String(FlagDBSSLMode, DefaultDBSSLMode, DescriptionDBSSLMode)
|
||||
if err := viper.BindPFlag(ViperDBSSLMode, apiCmd.Flags().Lookup(FlagDBSSLMode)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.host
|
||||
apiCmd.Flags().String(FlagDBHost, DefaultDBHost, DescriptionDBHost)
|
||||
if err := viper.BindPFlag(ViperDBHost, apiCmd.Flags().Lookup(FlagDBHost)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.password
|
||||
apiCmd.Flags().String(FlagDBPassword, DefaultDBPassword, DescriptionDBPassword)
|
||||
if err := viper.BindPFlag(ViperDBPassword, apiCmd.Flags().Lookup(FlagDBPassword)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.port
|
||||
apiCmd.Flags().Int(FlagDBPort, DefaultDBPort, DescriptionDBPort)
|
||||
if err := viper.BindPFlag(ViperDBPort, apiCmd.Flags().Lookup(FlagDBPort)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.user
|
||||
apiCmd.Flags().String(FlagDBUser, DefaultDBUser, DescriptionDBUser)
|
||||
if err := viper.BindPFlag(ViperDBUser, apiCmd.Flags().Lookup(FlagDBUser)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
var cfgFile string
|
||||
|
||||
// rootCmd represents the base command when called without any subcommands
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "bottin",
|
||||
Short: "Bottin étudiant de l'AGECEM",
|
||||
}
|
||||
|
||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||
func Execute() {
|
||||
err := rootCmd.Execute()
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bottin.yaml)")
|
||||
}
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
// Search config in home directory with name ".bottin" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName(".bottin")
|
||||
}
|
||||
|
||||
viper.SetEnvPrefix("BOTTIN")
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
|
||||
//go:embed templates/*
|
||||
var templatesFS embed.FS
|
||||
|
||||
type Template struct {
|
||||
templates *template.Template
|
||||
}
|
||||
|
||||
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
||||
return t.templates.ExecuteTemplate(w, name, data)
|
||||
}
|
||||
|
||||
// webCmd represents the web command
|
||||
var webCmd = &cobra.Command{
|
||||
Use: "web",
|
||||
|
@ -270,49 +170,3 @@ var webCmd = &cobra.Command{
|
|||
fmt.Sprintf(":%d", cfg.Web.Port)))
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(webCmd)
|
||||
|
||||
// web.api.host
|
||||
webCmd.Flags().String(FlagWebAPIHost, DefaultWebAPIHost, DescriptionWebAPIHost)
|
||||
if err := viper.BindPFlag(ViperWebAPIHost, webCmd.Flags().Lookup(FlagWebAPIHost)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.api.key
|
||||
webCmd.Flags().String(FlagWebAPIKey, DefaultWebAPIKey, DescriptionWebAPIKey)
|
||||
if err := viper.BindPFlag(ViperWebAPIKey, webCmd.Flags().Lookup(FlagWebAPIKey)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.api.protocol
|
||||
webCmd.Flags().String(FlagWebAPIProtocol, DefaultWebAPIProtocol, DescriptionWebAPIProtocol)
|
||||
if err := viper.BindPFlag(ViperWebAPIProtocol, webCmd.Flags().Lookup(FlagWebAPIProtocol)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.api.port
|
||||
webCmd.Flags().Int(FlagWebAPIPort, DefaultWebAPIPort, DescriptionWebAPIPort)
|
||||
if err := viper.BindPFlag(ViperWebAPIPort, webCmd.Flags().Lookup(FlagWebAPIPort)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.password
|
||||
webCmd.Flags().String(FlagWebPassword, DefaultWebPassword, DescriptionWebPassword)
|
||||
if err := viper.BindPFlag(ViperWebPassword, webCmd.Flags().Lookup(FlagWebPassword)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.port
|
||||
webCmd.Flags().Int(FlagWebPort, DefaultWebPort, DescriptionWebPort)
|
||||
if err := viper.BindPFlag(ViperWebPort, webCmd.Flags().Lookup(FlagWebPort)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.user
|
||||
webCmd.Flags().String(FlagWebUser, DefaultWebUser, DescriptionWebUser)
|
||||
if err := viper.BindPFlag(ViperWebUser, webCmd.Flags().Lookup(FlagWebUser)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
143
config.go
143
config.go
|
@ -1,6 +1,14 @@
|
|||
package main
|
||||
|
||||
//TODO move flag declarations here
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const (
|
||||
ViperAPIPort string = "api.port"
|
||||
|
@ -125,3 +133,136 @@ func DefaultConfig() (cfg Config) {
|
|||
cfg.Web.API.Protocol = DefaultWebAPIProtocol
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
// rootCmd
|
||||
|
||||
cobra.OnInitialize(initConfig)
|
||||
|
||||
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.bottin.yaml)")
|
||||
|
||||
// apiCmd
|
||||
|
||||
rootCmd.AddCommand(apiCmd)
|
||||
|
||||
// api.key
|
||||
apiCmd.Flags().String(FlagAPIKey, DefaultAPIKey, DescriptionAPIKey)
|
||||
if err := viper.BindPFlag(ViperAPIKey, apiCmd.Flags().Lookup(FlagAPIKey)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// api.port
|
||||
apiCmd.Flags().Int(FlagAPIPort, DefaultAPIPort, DescriptionAPIPort)
|
||||
if err := viper.BindPFlag(ViperAPIPort, apiCmd.Flags().Lookup(FlagAPIPort)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.database
|
||||
apiCmd.Flags().String(FlagDBDatabase, DefaultDBDatabase, DescriptionDBDatabase)
|
||||
if err := viper.BindPFlag(ViperDBDatabase, apiCmd.Flags().Lookup(FlagDBDatabase)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.sslmode
|
||||
apiCmd.Flags().String(FlagDBSSLMode, DefaultDBSSLMode, DescriptionDBSSLMode)
|
||||
if err := viper.BindPFlag(ViperDBSSLMode, apiCmd.Flags().Lookup(FlagDBSSLMode)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.host
|
||||
apiCmd.Flags().String(FlagDBHost, DefaultDBHost, DescriptionDBHost)
|
||||
if err := viper.BindPFlag(ViperDBHost, apiCmd.Flags().Lookup(FlagDBHost)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.password
|
||||
apiCmd.Flags().String(FlagDBPassword, DefaultDBPassword, DescriptionDBPassword)
|
||||
if err := viper.BindPFlag(ViperDBPassword, apiCmd.Flags().Lookup(FlagDBPassword)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.port
|
||||
apiCmd.Flags().Int(FlagDBPort, DefaultDBPort, DescriptionDBPort)
|
||||
if err := viper.BindPFlag(ViperDBPort, apiCmd.Flags().Lookup(FlagDBPort)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// db.user
|
||||
apiCmd.Flags().String(FlagDBUser, DefaultDBUser, DescriptionDBUser)
|
||||
if err := viper.BindPFlag(ViperDBUser, apiCmd.Flags().Lookup(FlagDBUser)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// WebCmd
|
||||
rootCmd.AddCommand(webCmd)
|
||||
|
||||
// web.api.host
|
||||
webCmd.Flags().String(FlagWebAPIHost, DefaultWebAPIHost, DescriptionWebAPIHost)
|
||||
if err := viper.BindPFlag(ViperWebAPIHost, webCmd.Flags().Lookup(FlagWebAPIHost)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.api.key
|
||||
webCmd.Flags().String(FlagWebAPIKey, DefaultWebAPIKey, DescriptionWebAPIKey)
|
||||
if err := viper.BindPFlag(ViperWebAPIKey, webCmd.Flags().Lookup(FlagWebAPIKey)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.api.protocol
|
||||
webCmd.Flags().String(FlagWebAPIProtocol, DefaultWebAPIProtocol, DescriptionWebAPIProtocol)
|
||||
if err := viper.BindPFlag(ViperWebAPIProtocol, webCmd.Flags().Lookup(FlagWebAPIProtocol)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.api.port
|
||||
webCmd.Flags().Int(FlagWebAPIPort, DefaultWebAPIPort, DescriptionWebAPIPort)
|
||||
if err := viper.BindPFlag(ViperWebAPIPort, webCmd.Flags().Lookup(FlagWebAPIPort)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.password
|
||||
webCmd.Flags().String(FlagWebPassword, DefaultWebPassword, DescriptionWebPassword)
|
||||
if err := viper.BindPFlag(ViperWebPassword, webCmd.Flags().Lookup(FlagWebPassword)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.port
|
||||
webCmd.Flags().Int(FlagWebPort, DefaultWebPort, DescriptionWebPort)
|
||||
if err := viper.BindPFlag(ViperWebPort, webCmd.Flags().Lookup(FlagWebPort)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// web.user
|
||||
webCmd.Flags().String(FlagWebUser, DefaultWebUser, DescriptionWebUser)
|
||||
if err := viper.BindPFlag(ViperWebUser, webCmd.Flags().Lookup(FlagWebUser)); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
var cfgFile string
|
||||
|
||||
// initConfig reads in config file and ENV variables if set.
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
// Use config file from the flag.
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
// Find home directory.
|
||||
home, err := os.UserHomeDir()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
// Search config in home directory with name ".bottin" (without extension).
|
||||
viper.AddConfigPath(home)
|
||||
viper.SetConfigType("yaml")
|
||||
viper.SetConfigName(".bottin")
|
||||
}
|
||||
|
||||
viper.SetEnvPrefix("BOTTIN")
|
||||
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
viper.AutomaticEnv() // read in environment variables that match
|
||||
|
||||
// If a config file is found, read it in.
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
}
|
||||
|
|
21
main.go
21
main.go
|
@ -1,23 +1,18 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
)
|
||||
|
||||
func main() {
|
||||
//TODO
|
||||
/*
|
||||
if err := Run(context.Background(), Config{}, nil, os.Stdout); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
/* TODO
|
||||
if err := Run(context.Background(), Config{}, nil, os.Stdout); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
*/
|
||||
|
||||
// Handle the command-line
|
||||
Execute()
|
||||
// Handle the command-line via cobra and viper
|
||||
execute()
|
||||
}
|
||||
|
||||
/* TODO
|
||||
func Run(ctx context.Context, config Config, args []string, stdout io.Writer) error {
|
||||
return fmt.Errorf("not implemented")
|
||||
}
|
||||
*/
|
||||
|
|
20
template.go
Normal file
20
template.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"html/template"
|
||||
"io"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
//go:embed templates/*
|
||||
var templatesFS embed.FS
|
||||
|
||||
type Template struct {
|
||||
templates *template.Template
|
||||
}
|
||||
|
||||
func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
||||
return t.templates.ExecuteTemplate(w, name, data)
|
||||
}
|
Reference in a new issue