config: added new configuration for oauth cycle

This commit is contained in:
Felipe M 2022-08-15 12:06:33 +02:00
parent fbc2e7a1af
commit a53edf0aa6
Signed by: fmartingr
GPG Key ID: 716BC147715E716F
2 changed files with 30 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package config
import (
"bufio"
"context"
"fmt"
"os"
"strings"
"time"
@ -61,7 +62,10 @@ type Config struct {
}
LogLevel string `env:"LOG_LEVEL,default=info"`
Notion struct {
IntegrationToken string `env:"NOTION_INTEGRATION_TOKEN,required"`
IntegrationType string `env:"NOTION_INTEGRATION_TYPE,required"`
IntegrationToken string `env:"NOTION_INTEGRATION_TOKEN"`
OAuthID string `env:"NOTION_OAUTH_ID"`
OAuthSecret string `env:"NOTION_OAUTH_SECRET"`
MaxPagination int `env:"NOTION_MAX_PAGINATION,default=2"`
Client *notion.NotionClient // Must be manually set up
}
@ -82,6 +86,22 @@ type Config struct {
}
}
func (c Config) Validate() error {
if c.Notion.IntegrationType != notion.IntegrationTypeInternal && c.Notion.IntegrationType != notion.IntegrationTypePublic {
return fmt.Errorf("Notion's integration type should be %s or %s", notion.IntegrationTypeInternal, notion.IntegrationTypePublic)
}
if c.Notion.IntegrationType == notion.IntegrationTypeInternal && c.Notion.IntegrationToken == "" {
return fmt.Errorf("Notion internal integration requires setting up the integration token")
}
if c.Notion.IntegrationType == notion.IntegrationTypePublic && c.Notion.OAuthID == "" && c.Notion.OAuthSecret == "" {
return fmt.Errorf("Notion's public integration requires setting up the OAuth credentials")
}
return nil
}
func ParseServerConfiguration(ctx context.Context, logger *zap.Logger) *Config {
var cfg Config
@ -95,5 +115,9 @@ func ParseServerConfiguration(ctx context.Context, logger *zap.Logger) *Config {
logger.Fatal("Error parsing configuration: %s", zap.Error(err))
}
if err := cfg.Validate(); err != nil {
logger.Fatal("Error validating configuration: %s", zap.Error(err))
}
return &cfg
}

View File

@ -8,6 +8,11 @@ import (
"go.uber.org/zap"
)
const (
IntegrationTypeInternal = "internal"
IntegrationTypePublic = "public"
)
type NotionClient struct {
Client *notion.Client
logger *zap.Logger