Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds implementation to output to file for provisioning in CLI #487

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
luispresuelVenafi marked this conversation as resolved.
Show resolved Hide resolved
}
if flags.certificateID != "" {
return fmt.Errorf("both -certificate-id and -pickup-id-file options cannot be specified at the same time")
luispresuelVenafi marked this conversation as resolved.
Show resolved Hide resolved
}
}

if flags.keystoreID == "" {
Expand Down