| 1 |
package jose |
| 2 |
|
| 3 |
import ( |
| 4 |
"github.com/smallstep/cli/ui" |
| 5 |
"github.com/smallstep/cli/utils" |
| 6 |
) |
| 7 |
|
| 8 |
type context struct { |
| 9 |
use, alg, kid string |
| 10 |
subtle, insecure bool |
| 11 |
noDefaults bool |
| 12 |
password []byte |
| 13 |
uiOptions []ui.Option |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
func (ctx *context) apply(opts ...Option) (*context, error) { |
| 19 |
for _, opt := range opts { |
| 20 |
if err := opt(ctx); err != nil { |
| 21 |
return nil, err |
| 22 |
} |
| 23 |
} |
| 24 |
return ctx, nil |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
type Option func(ctx *context) error |
| 29 |
|
| 30 |
|
| 31 |
func WithUse(use string) Option { |
| 32 |
return func(ctx *context) error { |
| 33 |
ctx.use = use |
| 34 |
return nil |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
func WithAlg(alg string) Option { |
| 40 |
return func(ctx *context) error { |
| 41 |
ctx.alg = alg |
| 42 |
return nil |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
func WithKid(kid string) Option { |
| 48 |
return func(ctx *context) error { |
| 49 |
ctx.kid = kid |
| 50 |
return nil |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
func WithSubtle(subtle bool) Option { |
| 56 |
return func(ctx *context) error { |
| 57 |
ctx.subtle = subtle |
| 58 |
return nil |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
func WithInsecure(insecure bool) Option { |
| 64 |
return func(ctx *context) error { |
| 65 |
ctx.insecure = insecure |
| 66 |
return nil |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
func WithNoDefaults(val bool) Option { |
| 73 |
return func(ctx *context) error { |
| 74 |
ctx.noDefaults = val |
| 75 |
return nil |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
func WithPassword(pass []byte) Option { |
| 81 |
return func(ctx *context) error { |
| 82 |
ctx.password = pass |
| 83 |
return nil |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
func WithPasswordFile(filename string) Option { |
| 89 |
return func(ctx *context) error { |
| 90 |
b, err := utils.ReadPasswordFromFile(filename) |
| 91 |
if err != nil { |
| 92 |
return err |
| 93 |
} |
| 94 |
ctx.password = b |
| 95 |
return nil |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
func WithUIOptions(opts ...ui.Option) Option { |
| 101 |
return func(ctx *context) error { |
| 102 |
ctx.uiOptions = opts |
| 103 |
return nil |
| 104 |
} |
| 105 |
} |