11package kube
22
33import (
4+ "bytes"
45 "encoding/base64"
5- "log"
66 "os"
77 "path/filepath"
88 "text/template"
99
10- "github.com/akitasoftware/akita-cli/printer "
10+ "github.com/akitasoftware/akita-cli/telemetry "
1111
1212 "github.com/akitasoftware/akita-cli/cmd/internal/cmderr"
13+ "github.com/akitasoftware/akita-cli/printer"
1314 "github.com/pkg/errors"
1415 "github.com/spf13/cobra"
1516)
@@ -30,14 +31,22 @@ var secretCmd = &cobra.Command{
3031 return err
3132 }
3233
33- err = handleSecretGeneration (namespaceFlag , key , secret , outputFlag )
34+ output , err : = handleSecretGeneration (namespaceFlag , key , secret , outputFlag )
3435 if err != nil {
3536 return err
3637 }
3738
38- printer .Infoln ("Generated Kubernetes secret config to " , outputFlag )
39+ // Output the generated secret to the console
40+ printer .RawOutput (output )
41+
3942 return nil
4043 },
44+ // Override the parent command's PersistentPreRun to prevent any logs from being printed.
45+ // This is necessary because the secret command is intended to be used in a pipeline
46+ PersistentPreRun : func (cmd * cobra.Command , args []string ) {
47+ // Initialize the telemetry client, but do not allow any logs to be printed
48+ telemetry .Init (false )
49+ },
4150}
4251
4352// Represents the input used by secretTemplate
@@ -47,8 +56,23 @@ type secretTemplateInput struct {
4756 APISecret string
4857}
4958
59+ func initSecretTemplate () error {
60+ var err error
61+
62+ secretTemplate , err = template .ParseFS (templateFS , "template/akita-secret.tmpl" )
63+ if err != nil {
64+ return cmderr.AkitaErr {Err : errors .Wrap (err , "failed to parse secret template" )}
65+ }
66+
67+ return nil
68+ }
69+
5070// Generates a Kubernetes secret config file for Akita
51- func handleSecretGeneration (namespace , key , secret , output string ) error {
71+ // On success, the generated output is returned as a string.
72+ func handleSecretGeneration (namespace , key , secret , output string ) (string , error ) {
73+ if err := initSecretTemplate (); err != nil {
74+ return "" , err
75+ }
5276
5377 input := secretTemplateInput {
5478 Namespace : namespace ,
@@ -58,17 +82,24 @@ func handleSecretGeneration(namespace, key, secret, output string) error {
5882
5983 secretFile , err := createSecretFile (output )
6084 if err != nil {
61- return cmderr.AkitaErr {Err : errors .Wrap (err , "failed to create output file" )}
85+ return "" , cmderr.AkitaErr {Err : errors .Wrap (err , "failed to create output file" )}
6286 }
6387
6488 defer secretFile .Close ()
6589
66- err = secretTemplate .Execute (secretFile , input )
90+ buf := new (bytes.Buffer )
91+
92+ err = secretTemplate .Execute (buf , input )
6793 if err != nil {
68- return cmderr.AkitaErr {Err : errors .Wrap (err , "failed to generate template" )}
94+ return "" , cmderr.AkitaErr {Err : errors .Wrap (err , "failed to generate template" )}
6995 }
7096
71- return nil
97+ _ , err = secretFile .Write (buf .Bytes ())
98+ if err != nil {
99+ return "" , cmderr.AkitaErr {Err : errors .Wrap (err , "failed to read generated secret file" )}
100+ }
101+
102+ return buf .String (), nil
72103}
73104
74105// Creates a file at the give path to be used for storing of the generated Secret config
0 commit comments