Skip to content

Commit e1e5dd9

Browse files
authored
Refactor: extract isLocalServerHTTPS() (#43)
1 parent e5d668f commit e1e5dd9

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

oauth2cli.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ type Config struct {
105105
LocalServerPort []int
106106
}
107107

108+
func (c *Config) isLocalServerHTTPS() bool {
109+
return c.LocalServerCertFile != "" && c.LocalServerKeyFile != ""
110+
}
111+
108112
func (c *Config) validateAndSetDefaults() error {
109113
if (c.LocalServerCertFile != "" && c.LocalServerKeyFile == "") ||
110114
(c.LocalServerCertFile == "" && c.LocalServerKeyFile != "") {

server.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,16 @@ func receiveCodeViaLocalServer(ctx context.Context, c *Config) (string, error) {
5353
})
5454
eg.Go(func() error {
5555
defer close(respCh)
56-
if c.LocalServerCertFile != "" && c.LocalServerKeyFile != "" {
57-
c.Logf("oauth2cli: starting HTTPS server at %s", l.Addr())
56+
c.Logf("oauth2cli: starting a local server at %s", l.Addr())
57+
defer c.Logf("oauth2cli: stopped the local server at %s", l.Addr())
58+
if c.isLocalServerHTTPS() {
5859
if err := server.ServeTLS(l, c.LocalServerCertFile, c.LocalServerKeyFile); err != nil && err != http.ErrServerClosed {
5960
return fmt.Errorf("could not start HTTPS server: %w", err)
6061
}
61-
c.Logf("oauth2cli: stopped HTTPS server at %s", l.Addr())
62-
} else {
63-
c.Logf("oauth2cli: starting HTTP server at %s", l.Addr())
64-
if err := server.Serve(l); err != nil && err != http.ErrServerClosed {
65-
return fmt.Errorf("could not start HTTP server: %w", err)
66-
}
67-
c.Logf("oauth2cli: stopped HTTP server at %s", l.Addr())
62+
return nil
63+
}
64+
if err := server.Serve(l); err != nil && err != http.ErrServerClosed {
65+
return fmt.Errorf("could not start HTTP server: %w", err)
6866
}
6967
return nil
7068
})

0 commit comments

Comments
 (0)