github.com/blend/go-sdk@v1.20220411.3/configutil/option.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package configutil 9 10 import ( 11 "bytes" 12 "context" 13 "io" 14 15 "github.com/blend/go-sdk/env" 16 ) 17 18 // Option is a modification of config options. 19 type Option func(*ConfigOptions) error 20 21 // OptLog sets the configutil logger. 22 func OptLog(log Logger) Option { 23 return func(co *ConfigOptions) error { 24 co.Log = log 25 return nil 26 } 27 } 28 29 // OptContext sets the context on the options. 30 func OptContext(ctx context.Context) Option { 31 return func(co *ConfigOptions) error { 32 co.Context = ctx 33 return nil 34 } 35 } 36 37 // OptContents sets config contents on the options. 38 func OptContents(contents ...ConfigContents) Option { 39 return func(co *ConfigOptions) error { 40 co.Contents = contents 41 return nil 42 } 43 } 44 45 // OptAddContent adds contents to the options as a reader. 46 func OptAddContent(ext string, content io.Reader) Option { 47 return func(co *ConfigOptions) error { 48 co.Contents = append(co.Contents, ConfigContents{ 49 Ext: ext, 50 Contents: content, 51 }) 52 return nil 53 } 54 } 55 56 // OptAddContentString adds contents to the options as a string. 57 func OptAddContentString(ext string, contents string) Option { 58 return func(co *ConfigOptions) error { 59 co.Contents = append(co.Contents, ConfigContents{ 60 Ext: ext, 61 Contents: bytes.NewReader([]byte(contents)), 62 }) 63 return nil 64 } 65 } 66 67 // OptAddPaths adds paths to search for the config file. 68 // 69 // These paths will be added after the default paths. 70 func OptAddPaths(paths ...string) Option { 71 return func(co *ConfigOptions) error { 72 co.FilePaths = append(co.FilePaths, paths...) 73 return nil 74 } 75 } 76 77 // OptAddFilePaths is deprecated; use `OptAddPaths` 78 func OptAddFilePaths(paths ...string) Option { 79 return OptAddPaths(paths...) 80 } 81 82 // OptAddPreferredPaths adds paths to search first for the config file. 83 func OptAddPreferredPaths(paths ...string) Option { 84 return func(co *ConfigOptions) error { 85 co.FilePaths = append(paths, co.FilePaths...) 86 return nil 87 } 88 } 89 90 // OptPaths sets paths to search for the config file. 91 func OptPaths(paths ...string) Option { 92 return func(co *ConfigOptions) error { 93 co.FilePaths = paths 94 return nil 95 } 96 } 97 98 // OptUnsetPaths removes default paths from the paths set. 99 func OptUnsetPaths() Option { 100 return func(co *ConfigOptions) error { 101 co.FilePaths = nil 102 return nil 103 } 104 } 105 106 // OptEnv sets the config options environment variables. 107 // If unset, will default to the current global environment variables. 108 func OptEnv(vars env.Vars) Option { 109 return func(co *ConfigOptions) error { 110 co.Env = vars 111 return nil 112 } 113 }