Skip to content

Commit

Permalink
Merge pull request #487 from Venafi/VC-32826-provision-vcert-cli-2
Browse files Browse the repository at this point in the history
Adds implementation to output to file for provisioning in CLI
  • Loading branch information
luispresuelVenafi authored May 24, 2024
2 parents a36630c + 8daeafc commit 16f745f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/vcert/cmdCloudKeystores.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func doCommandProvisionCloudKeystore(c *cli.Context) error {
result.MachineIdentityId = metadata.GetMachineIdentityMetadata().GetID()
result.MachineIdentityActionType = metadata.GetMachineIdentityMetadata().GetActionType()

err = result.Flush(flags.provisionFormat)
err = result.Flush(flags.provisionFormat, flags.provisionOutputFile)

if err != nil {
return fmt.Errorf("failed to output the results: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion cmd/vcert/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ var (
flagKeystoreName,
flagKeystoreID,
flagProvisionFormat,
flagProvisionOutputFile, // TODO: implement this flag
flagProvisionOutputFile,
)

commonCredFlags = []cli.Flag{flagConfig, flagProfile, flagUrl, flagToken, flagTrustBundle}
Expand Down
15 changes: 14 additions & 1 deletion cmd/vcert/result_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,13 +447,18 @@ func outputJSON(resp interface{}) error {
return err
}

func (r *ProvisioningResult) Flush(format string) error {
func (r *ProvisioningResult) Flush(format string, filePath string) error {

result, err := r.Format(format)
if err != nil {
return err
}

if filePath != "" {
err = r.WriteFile(result, filePath)
return err
}

_, err = fmt.Fprint(os.Stdout, result)
if err != nil {
return fmt.Errorf("failed to print provisioning result to STDOUT: %w", err)
Expand All @@ -462,6 +467,14 @@ func (r *ProvisioningResult) Flush(format string) error {
return nil
}

func (r *ProvisioningResult) WriteFile(result string, filePath string) error {
err := os.WriteFile(filePath, []byte(result), 0600)
if err != nil {
return err
}
return nil
}

func (r *ProvisioningResult) Format(format string) (string, error) {
result := ""
switch strings.ToLower(format) {
Expand Down
13 changes: 11 additions & 2 deletions cmd/vcert/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,8 +732,17 @@ func validateProvisionFlags(commandName string) error {
return fmt.Errorf("unexpected output format: %s", flags.format)
}

if flags.certificateID == "" && flags.provisionPickupID == "" {
return fmt.Errorf("please, provide any of certificate-id or pickup-id")
if flags.certificateID == "" && flags.provisionPickupID == "" && flags.pickupIDFile == "" {
return fmt.Errorf("please, provide any of --certificate-id or --pickup-id or --pickup-id-file")
}

if flags.pickupIDFile != "" {
if flags.pickupID != "" {
return fmt.Errorf("both --pickup-id and --pickup-id-file options cannot be specified at the same time")
}
if flags.certificateID != "" {
return fmt.Errorf("both --certificate-id and --pickup-id-file options cannot be specified at the same time")
}
}

if flags.keystoreID == "" {
Expand Down

0 comments on commit 16f745f

Please sign in to comment.