Created
January 14, 2015 07:58
-
-
Save poga/bf0534486199c8e778cb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
type Service struct { | |
ErrorLevel int | |
AWSKey string | |
AWSSecret string | |
AfterHook func() | |
} | |
func (s *Service) Config(opts ...option) { | |
for _, opt := range opts { | |
opt(s) | |
} | |
} | |
type option func(*Service) | |
const ( | |
DEBUG = iota | |
INFO | |
WARN | |
FATAL | |
PANIC | |
) | |
func LogLevel(level int) option { | |
return func(s *Service) { | |
s.ErrorLevel = level | |
} | |
} | |
func AfterComplete(f func()) option { | |
return func(s *Service) { | |
s.AfterHook = f | |
} | |
} | |
type S3 struct { | |
Key string | |
Secret string | |
} | |
func Backend(opt S3) option { | |
return func(s *Service) { | |
s.AWSKey = opt.Key | |
s.AWSSecret = opt.Secret | |
} | |
} | |
func ConfigService(s *Service) { | |
s.Config( | |
LogLevel(WARN), | |
Backend(S3{ | |
Key: "API_KEY", | |
Secret: "SUPER SECRET", | |
}), | |
AfterComplete(func() { | |
fmt.Println("service ready") | |
}), | |
) | |
} | |
func main() { | |
s := Service{} | |
ConfigService(&s) | |
fmt.Printf("%v", s) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment