66 "fmt"
77 "strings"
88
9+ "github.com/stackitcloud/stackit-cli/internal/pkg/utils"
10+
911 "github.com/stackitcloud/stackit-cli/internal/pkg/args"
1012 "github.com/stackitcloud/stackit-cli/internal/pkg/examples"
1113 "github.com/stackitcloud/stackit-cli/internal/pkg/flags"
@@ -69,7 +71,11 @@ func NewCmd(p *print.Printer) *cobra.Command {
6971 return fmt .Errorf ("read key pair: %w" , err )
7072 }
7173
72- return outputResult (p , model .OutputFormat , model .PublicKey , resp )
74+ if keypair := resp ; keypair != nil {
75+ return outputResult (p , model .OutputFormat , model .PublicKey , * keypair )
76+ }
77+ p .Outputln ("No keypair found." )
78+ return nil
7379 },
7480 }
7581 configureFlags (cmd )
@@ -107,7 +113,7 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APICli
107113 return apiClient .GetKeyPair (ctx , model .KeyPairName )
108114}
109115
110- func outputResult (p * print.Printer , outputFormat string , showOnlyPublicKey bool , keyPair * iaas.Keypair ) error {
116+ func outputResult (p * print.Printer , outputFormat string , showOnlyPublicKey bool , keyPair iaas.Keypair ) error {
111117 switch outputFormat {
112118 case print .JSONOutputFormat :
113119 details , err := json .MarshalIndent (keyPair , "" , " " )
@@ -145,10 +151,10 @@ func outputResult(p *print.Printer, outputFormat string, showOnlyPublicKey bool,
145151 return nil
146152 }
147153 table := tables .NewTable ()
148- table .AddRow ("KEY PAIR NAME" , * keyPair .Name )
154+ table .AddRow ("KEY PAIR NAME" , utils . PtrString ( keyPair .Name ) )
149155 table .AddSeparator ()
150156
151- if * keyPair .Labels != nil && len (* keyPair .Labels ) > 0 {
157+ if keyPair .Labels != nil && len (* keyPair .Labels ) > 0 {
152158 var labels []string
153159 for key , value := range * keyPair .Labels {
154160 labels = append (labels , fmt .Sprintf ("%s: %s" , key , value ))
@@ -157,17 +163,21 @@ func outputResult(p *print.Printer, outputFormat string, showOnlyPublicKey bool,
157163 table .AddSeparator ()
158164 }
159165
160- table .AddRow ("FINGERPRINT" , * keyPair .Fingerprint )
166+ table .AddRow ("FINGERPRINT" , utils . PtrString ( keyPair .Fingerprint ) )
161167 table .AddSeparator ()
162168
163- truncatedPublicKey := (* keyPair .PublicKey )[:maxLengthPublicKey ] + "..."
169+ truncatedPublicKey := ""
170+ if keyPair .PublicKey != nil {
171+ truncatedPublicKey = (* keyPair .PublicKey )[:maxLengthPublicKey ] + "..."
172+ }
173+
164174 table .AddRow ("PUBLIC KEY" , truncatedPublicKey )
165175 table .AddSeparator ()
166176
167- table .AddRow ("CREATED AT" , * keyPair .CreatedAt )
177+ table .AddRow ("CREATED AT" , utils . PtrString ( keyPair .CreatedAt ) )
168178 table .AddSeparator ()
169179
170- table .AddRow ("UPDATED AT" , * keyPair .UpdatedAt )
180+ table .AddRow ("UPDATED AT" , utils . PtrString ( keyPair .UpdatedAt ) )
171181 table .AddSeparator ()
172182
173183 p .Outputln (table .Render ())
0 commit comments