MCPcopy Index your code
hub / github.com/smallstep/cli / formatAction

Function formatAction

command/crypto/key/format.go:155–286  ·  view source on GitHub ↗
(ctx *cli.Context)

Source from the content-addressed store, hash-verified

153}
154
155func formatAction(ctx *cli.Context) error {
156 if err := errs.MinMaxNumberOfArguments(ctx, 0, 1); err != nil {
157 return err
158 }
159
160 var keyFile string
161 switch ctx.NArg() {
162 case 0:
163 keyFile = "-"
164 case 1:
165 keyFile = ctx.Args().First()
166 default:
167 return errs.TooManyArguments(ctx)
168 }
169
170 var (
171 out = ctx.String("out")
172 toPEM = ctx.Bool("pem")
173 toDER = ctx.Bool("der")
174 toSSH = ctx.Bool("ssh")
175 toJWK = ctx.Bool("jwk")
176 noPassword = ctx.Bool("no-password")
177 insecure = ctx.Bool("insecure")
178 key interface{}
179 ob []byte
180 )
181
182 switch {
183 case toPEM && toDER:
184 return errs.IncompatibleFlagWithFlag(ctx, "pem", "der")
185 case toPEM && toSSH:
186 return errs.IncompatibleFlagWithFlag(ctx, "pem", "ssh")
187 case toPEM && toJWK:
188 return errs.IncompatibleFlagWithFlag(ctx, "pem", "jwk")
189 case toDER && toSSH:
190 return errs.IncompatibleFlagWithFlag(ctx, "der", "ssh")
191 case toDER && toJWK:
192 return errs.IncompatibleFlagWithFlag(ctx, "der", "jwk")
193 case toSSH && toJWK:
194 return errs.IncompatibleFlagWithFlag(ctx, "ssh", "jwk")
195 }
196
197 // --no-password requires --insecure
198 if noPassword && !insecure {
199 return errs.RequiredInsecureFlag(ctx, "no-password")
200 }
201
202 b, err := utils.ReadFile(keyFile)
203 if err != nil {
204 return errs.FileError(err, keyFile)
205 }
206
207 switch {
208 case bytes.HasPrefix(b, []byte("-----BEGIN ")): // PEM format:
209 opts := []pemutil.Options{pemutil.WithFilename(keyFile)}
210 if passFile := ctx.String("password-file"); passFile != "" {
211 opts = append(opts, pemutil.WithPasswordFile(passFile))
212 }

Callers

nothing calls this directly

Calls 9

ReadFileFunction · 0.92
isSSHPublicKeyFunction · 0.85
isJWKFunction · 0.85
parseJWKFunction · 0.85
convertToPEMFunction · 0.85
convertToDERFunction · 0.85
convertToSSHFunction · 0.85
convertToJWKFunction · 0.85
StringMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…