Skip to content

Commit

Permalink
feat: add flag to not open the browser automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Huck committed Jul 10, 2024
1 parent 09f21ce commit 0944848
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 29 deletions.
22 changes: 15 additions & 7 deletions cmd/calendarsync/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (
)

const (
flagLogLevel = "log-level"
flagConfigFilePath = "config"
flagStorageEncryptionKey = "storage-encryption-key"
flagClean = "clean"
flagDryRun = "dry-run"
flagPort = "port"
flagVersion = "version"
flagLogLevel = "log-level"
flagConfigFilePath = "config"
flagStorageEncryptionKey = "storage-encryption-key"
flagClean = "clean"
flagDryRun = "dry-run"
flagPort = "port"
flagOpenBrowserAutomatically = "open-browser"
flagVersion = "version"
)

var (
Expand Down Expand Up @@ -51,6 +52,11 @@ func main() {
Name: flagStorageEncryptionKey,
Usage: "encryption string to be used for encrypting the local auth-storage file. NOTE: This option is deprecated. Please use the CALENDARSYNC_ENCRYPTION_KEY env variable. The flag will be removed in later versions",
},
&cli.BoolFlag{
Name: flagOpenBrowserAutomatically,
Usage: "opens the browser automatically for the authentication process",
Value: true,
},
&cli.BoolFlag{
Name: flagClean,
Usage: "cleans your sink calendar from all the synced events",
Expand Down Expand Up @@ -149,6 +155,7 @@ func Run(c *cli.Context) error {
sourceAdapter, err := adapter.NewSourceAdapterFromConfig(
c.Context,
sourceBindAuthPort,
c.Bool(flagOpenBrowserAutomatically),
config.NewAdapterConfig(cfg.Source.Adapter),
storage,
sourceLogger,
Expand All @@ -163,6 +170,7 @@ func Run(c *cli.Context) error {
sinkAdapter, err := adapter.NewSinkAdapterFromConfig(
c.Context,
sinkBindAuthPort,
c.Bool(flagOpenBrowserAutomatically),
config.NewAdapterConfig(cfg.Sink.Adapter),
storage,
sinkLogger,
Expand Down
20 changes: 11 additions & 9 deletions internal/adapter/google/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ type GoogleCalendarClient interface {
InitGoogleCalendarClient(calId string, log *log.Logger) error
}

// TODO: add filter mechanism to clients for selective sync

// CalendarAPI is our Google Calendar client wrapper which adapts the base api to the needs of CalendarSync.
type CalendarAPI struct {
gcalClient GoogleCalendarClient
Expand Down Expand Up @@ -106,21 +104,25 @@ func (c *CalendarAPI) SetupOauth2(ctx context.Context, credentials auth.Credenti
// The given config is presumably unknown and is validated and loaded in order to construct a valid
// CalendarAPI struct.
// If anything fails, an error is returned and the CalendarAPI should be considered non-functional.
func (c *CalendarAPI) Initialize(ctx context.Context, config map[string]interface{}) error {
func (c *CalendarAPI) Initialize(ctx context.Context, openBrowser bool, config map[string]interface{}) error {
if !c.authenticated {
c.oAuthUrl = c.oAuthHandler.Configuration().AuthCodeURL("state", oauth2.AccessTypeOffline)
c.logger.Infof("opening browser window for authentication of %s\n", c.Name())
err := browser.OpenURL(c.oAuthUrl)
if err != nil {
c.logger.Infof("browser did not open, please authenticate adapter %s:\n\n %s\n\n\n", c.Name(), c.oAuthUrl)
if openBrowser {
c.logger.Infof("opening browser window for authentication of %s\n", c.Name())
err := browser.OpenURL(c.oAuthUrl)
if err != nil {
c.logger.Infof("browser did not open, please authenticate adapter %s:\n\n %s\n\n\n", c.Name(), c.oAuthUrl)
}
} else {
c.logger.Infof("Please authenticate adapter %s:\n\n %s\n\n\n", c.Name(), c.oAuthUrl)
}

if err := c.oAuthHandler.Listen(ctx); err != nil {
return err
}

c.oAuthToken = c.oAuthHandler.Token()
_, err = c.storage.WriteCalendarAuth(auth.CalendarAuth{
_, err := c.storage.WriteCalendarAuth(auth.CalendarAuth{
CalendarID: c.calendarID,
OAuth2: auth.OAuth2Object{
AccessToken: c.oAuthToken.AccessToken,
Expand Down Expand Up @@ -158,7 +160,7 @@ func (c *CalendarAPI) Initialize(ctx context.Context, config map[string]interfac
return fmt.Errorf("failed to remove authentication for calendar %s: %w", c.calendarID, err)
}
c.authenticated = false
err = c.Initialize(ctx, config)
err = c.Initialize(ctx, openBrowser, config)
if err != nil {
return fmt.Errorf("couldn't reinitialize calendar after expired refresh token: %w", err)
}
Expand Down
1 change: 0 additions & 1 deletion internal/adapter/google/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ func (g *GCalClient) DeleteEvent(ctx context.Context, event models.Event) error
return err
}
return nil

}

// loadPages recursively loads all pages starting with the given nextPageToken.
Expand Down
17 changes: 11 additions & 6 deletions internal/adapter/outlook_http/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,20 +161,25 @@ func (c *CalendarAPI) SetupOauth2(ctx context.Context, credentials auth.Credenti
return nil
}

func (c *CalendarAPI) Initialize(ctx context.Context, config map[string]interface{}) error {
func (c *CalendarAPI) Initialize(ctx context.Context, openBrowser bool, config map[string]interface{}) error {
if !c.authenticated {
c.oAuthUrl = c.oAuthHandler.Configuration().AuthCodeURL("state", oauth2.AccessTypeOffline)
c.logger.Infof("opening browser window for authentication of %s\n", c.Name())
err := browser.OpenURL(c.oAuthUrl)
if err != nil {
c.logger.Infof("browser did not open, please authenticate adapter %s:\n\n %s\n\n\n", c.Name(), c.oAuthUrl)

if openBrowser {
c.logger.Infof("opening browser window for authentication of %s\n", c.Name())
err := browser.OpenURL(c.oAuthUrl)
if err != nil {
c.logger.Infof("browser did not open, please authenticate adapter %s:\n\n %s\n\n\n", c.Name(), c.oAuthUrl)
}
} else {
c.logger.Infof("Please authenticate adapter %s:\n\n %s\n\n\n", c.Name(), c.oAuthUrl)
}
if err := c.oAuthHandler.Listen(ctx); err != nil {
return err
}

c.oAuthToken = c.oAuthHandler.Token()
_, err = c.storage.WriteCalendarAuth(auth.CalendarAuth{
_, err := c.storage.WriteCalendarAuth(auth.CalendarAuth{
CalendarID: c.calendarID,
OAuth2: auth.OAuth2Object{
AccessToken: c.oAuthToken.AccessToken,
Expand Down
2 changes: 1 addition & 1 deletion internal/adapter/port/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type LogSetter interface {
// Configurable is an interface which defines how arbitrary configuration data can be passed
// to a struct which implements this interface. Clients should be configurable.
type Configurable interface {
Initialize(ctx context.Context, config map[string]interface{}) error
Initialize(ctx context.Context, openBrowser bool, config map[string]interface{}) error
}

type OAuth2Adapter interface {
Expand Down
4 changes: 2 additions & 2 deletions internal/adapter/sink_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func SinkClientFactory(typ Type) (sync.Sink, error) {
}
}

func NewSinkAdapterFromConfig(ctx context.Context, bindPort uint, config ConfigReader, storage auth.Storage, logger *log.Logger) (*SinkAdapter, error) {
func NewSinkAdapterFromConfig(ctx context.Context, bindPort uint, openBrowser bool, config ConfigReader, storage auth.Storage, logger *log.Logger) (*SinkAdapter, error) {
client, err := SinkClientFactory(Type(config.Adapter().Type))
if err != nil {
return nil, err
Expand Down Expand Up @@ -66,7 +66,7 @@ func NewSinkAdapterFromConfig(ctx context.Context, bindPort uint, config ConfigR

// configure adapter client if possible
if c, ok := client.(port.Configurable); ok {
if err := c.Initialize(ctx, config.Adapter().Config); err != nil {
if err := c.Initialize(ctx, openBrowser, config.Adapter().Config); err != nil {
return nil, fmt.Errorf("unable to Initialize adapter %s: %w", config.Adapter().Type, err)
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/adapter/source_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type SourceAdapter struct {
logger *log.Logger
}

func NewSourceAdapterFromConfig(ctx context.Context, bindPort uint, config ConfigReader, storage auth.Storage, logger *log.Logger) (*SourceAdapter, error) {
func NewSourceAdapterFromConfig(ctx context.Context, bindPort uint, openBrowser bool, config ConfigReader, storage auth.Storage, logger *log.Logger) (*SourceAdapter, error) {
var client sync.Source
client, err := SourceClientFactory(Type(config.Adapter().Type))
if err != nil {
Expand Down Expand Up @@ -70,7 +70,7 @@ func NewSourceAdapterFromConfig(ctx context.Context, bindPort uint, config Confi

// configure adapter client if possible
if c, ok := client.(port.Configurable); ok {
if err := c.Initialize(ctx, config.Adapter().Config); err != nil {
if err := c.Initialize(ctx, openBrowser, config.Adapter().Config); err != nil {
return nil, fmt.Errorf("unable to initialize adapter %s: %w", config.Adapter().Type, err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/adapter/zep/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (zep *CalendarAPI) Name() string {
return "ZEP CalDav API"
}

func (zep *CalendarAPI) Initialize(ctx context.Context, config map[string]interface{}) error {
func (zep *CalendarAPI) Initialize(ctx context.Context, openBrowser bool, config map[string]interface{}) error {
if _, ok := config[usernameKey]; !ok {
return fmt.Errorf("missing config key: %s", usernameKey)
}
Expand Down

0 comments on commit 0944848

Please sign in to comment.