Skip to content

Commit 6a52694

Browse files
committed
Use cmd instead of fmt in signer commands
1 parent 12eea40 commit 6a52694

File tree

5 files changed

+40
-41
lines changed

5 files changed

+40
-41
lines changed

client/cmd/signer/artifactkey.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The artifact key will be used to sign software artifacts/updates.`,
3232
return fmt.Errorf("--expiration must be a positive duration (e.g., 720h, 365d, 8760h)")
3333
}
3434

35-
if err := handleCreateArtifactKey(createArtifactKeyRootPrivKeyFile, createArtifactKeyPrivKeyFile, createArtifactKeyPubKeyFile, createArtifactKeyExpiration); err != nil {
35+
if err := handleCreateArtifactKey(cmd, createArtifactKeyRootPrivKeyFile, createArtifactKeyPrivKeyFile, createArtifactKeyPubKeyFile, createArtifactKeyExpiration); err != nil {
3636
return fmt.Errorf("failed to create artifact key: %w", err)
3737
}
3838
return nil
@@ -49,7 +49,7 @@ This command is typically used to distribute or authorize a set of valid artifac
4949
return fmt.Errorf("at least one --artifact-pub-key-file must be provided")
5050
}
5151

52-
if err := handleBundlePubKeys(bundlePubKeysRootPrivKeyFile, bundlePubKeysPubKeyFiles, bundlePubKeysFile); err != nil {
52+
if err := handleBundlePubKeys(cmd, bundlePubKeysRootPrivKeyFile, bundlePubKeysPubKeyFiles, bundlePubKeysFile); err != nil {
5353
return fmt.Errorf("failed to bundle public keys: %w", err)
5454
}
5555
return nil
@@ -94,8 +94,8 @@ func init() {
9494
}
9595
}
9696

97-
func handleCreateArtifactKey(rootPrivKeyFile, artifactPrivKeyFile, artifactPubKeyFile string, expiration time.Duration) error {
98-
fmt.Println("🔑 Creating new artifact signing key...")
97+
func handleCreateArtifactKey(cmd *cobra.Command, rootPrivKeyFile, artifactPrivKeyFile, artifactPubKeyFile string, expiration time.Duration) error {
98+
cmd.Println("🔑 Creating new artifact signing key...")
9999

100100
privKeyPEM, err := os.ReadFile(rootPrivKeyFile)
101101
if err != nil {
@@ -125,13 +125,13 @@ func handleCreateArtifactKey(rootPrivKeyFile, artifactPrivKeyFile, artifactPubKe
125125
return fmt.Errorf("write signature file (%s): %w", signatureFile, err)
126126
}
127127

128-
fmt.Printf("✅ Artifact key created successfully.\n")
129-
fmt.Printf("%s\n", artifactKey.String())
128+
cmd.Printf("✅ Artifact key created successfully.\n")
129+
cmd.Printf("%s\n", artifactKey.String())
130130
return nil
131131
}
132132

133-
func handleBundlePubKeys(rootPrivKeyFile string, artifactPubKeyFiles []string, bundlePubKeysFile string) error {
134-
fmt.Println("📦 Bundling public keys into signed package...")
133+
func handleBundlePubKeys(cmd *cobra.Command, rootPrivKeyFile string, artifactPubKeyFiles []string, bundlePubKeysFile string) error {
134+
cmd.Println("📦 Bundling public keys into signed package...")
135135

136136
privKeyPEM, err := os.ReadFile(rootPrivKeyFile)
137137
if err != nil {
@@ -171,6 +171,6 @@ func handleBundlePubKeys(rootPrivKeyFile string, artifactPubKeyFiles []string, b
171171
return fmt.Errorf("write signature file (%s): %w", signatureFile, err)
172172
}
173173

174-
fmt.Printf("✅ Bundle created with %d public keys.\n", len(artifactPubKeyFiles))
174+
cmd.Printf("✅ Bundle created with %d public keys.\n", len(artifactPubKeyFiles))
175175
return nil
176176
}

client/cmd/signer/artifactsign.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var signArtifactCmd = &cobra.Command{
2525
This command produces a detached signature that can be verified using the corresponding artifact public key.`,
2626
SilenceUsage: true,
2727
RunE: func(cmd *cobra.Command, args []string) error {
28-
if err := handleSignArtifact(signArtifactRootPrivKeyFile, signArtifactArtifactFile); err != nil {
28+
if err := handleSignArtifact(cmd, signArtifactRootPrivKeyFile, signArtifactArtifactFile); err != nil {
2929
return fmt.Errorf("failed to sign artifact: %w", err)
3030
}
3131
return nil
@@ -38,7 +38,7 @@ var verifyArtifactCmd = &cobra.Command{
3838
Long: `Verify a software artifact signature using the artifact's public key.`,
3939
SilenceUsage: true,
4040
RunE: func(cmd *cobra.Command, args []string) error {
41-
if err := handleVerifyArtifact(verifyArtifactPubKeyFile, verifyArtifactFile, verifyArtifactSignatureFile); err != nil {
41+
if err := handleVerifyArtifact(cmd, verifyArtifactPubKeyFile, verifyArtifactFile, verifyArtifactSignatureFile); err != nil {
4242
return fmt.Errorf("failed to verify artifact: %w", err)
4343
}
4444
return nil
@@ -75,8 +75,8 @@ func init() {
7575
}
7676
}
7777

78-
func handleSignArtifact(privKeyFile, artifactFile string) error {
79-
fmt.Println("🖋️ Signing artifact...")
78+
func handleSignArtifact(cmd *cobra.Command, privKeyFile, artifactFile string) error {
79+
cmd.Println("🖋️ Signing artifact...")
8080

8181
privKeyPEM, err := os.ReadFile(privKeyFile)
8282
if err != nil {
@@ -103,13 +103,13 @@ func handleSignArtifact(privKeyFile, artifactFile string) error {
103103
return fmt.Errorf("write signature file (%s): %w", sigFile, err)
104104
}
105105

106-
fmt.Printf("✅ Artifact signed successfully.\n")
107-
fmt.Printf("Signature file: %s\n", sigFile)
106+
cmd.Printf("✅ Artifact signed successfully.\n")
107+
cmd.Printf("Signature file: %s\n", sigFile)
108108
return nil
109109
}
110110

111-
func handleVerifyArtifact(pubKeyFile, artifactFile, signatureFile string) error {
112-
fmt.Println("🔍 Verifying artifact...")
111+
func handleVerifyArtifact(cmd *cobra.Command, pubKeyFile, artifactFile, signatureFile string) error {
112+
cmd.Println("🔍 Verifying artifact...")
113113

114114
// Read artifact public key
115115
pubKeyPEM, err := os.ReadFile(pubKeyFile)
@@ -144,9 +144,9 @@ func handleVerifyArtifact(pubKeyFile, artifactFile, signatureFile string) error
144144
return fmt.Errorf("artifact verification failed: %w", err)
145145
}
146146

147-
fmt.Println("✅ Artifact signature is valid")
148-
fmt.Printf("Artifact: %s\n", artifactFile)
149-
fmt.Printf("Signed by key: %s\n", signature.KeyID)
150-
fmt.Printf("Signature timestamp: %s\n", signature.Timestamp.Format("2006-01-02 15:04:05 MST"))
147+
cmd.Println("✅ Artifact signature is valid")
148+
cmd.Printf("Artifact: %s\n", artifactFile)
149+
cmd.Printf("Signed by key: %s\n", signature.KeyID)
150+
cmd.Printf("Signature timestamp: %s\n", signature.Timestamp.Format("2006-01-02 15:04:05 MST"))
151151
return nil
152152
}

client/cmd/signer/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"fmt"
54
"os"
65

76
"github.com/spf13/cobra"
@@ -16,7 +15,7 @@ root keys, artifact keys, and revocation lists securely.`,
1615

1716
func main() {
1817
if err := rootCmd.Execute(); err != nil {
19-
fmt.Println(err)
18+
rootCmd.Println(err)
2019
os.Exit(1)
2120
}
2221
}

client/cmd/signer/revocation.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var createRevocationListCmd = &cobra.Command{
2828
Short: "Create a new revocation list signed by the private root key",
2929
SilenceUsage: true,
3030
RunE: func(cmd *cobra.Command, args []string) error {
31-
return handleCreateRevocationList(revocationListFile, privateRootKeyFile)
31+
return handleCreateRevocationList(cmd, revocationListFile, privateRootKeyFile)
3232
},
3333
}
3434

@@ -37,7 +37,7 @@ var extendRevocationListCmd = &cobra.Command{
3737
Short: "Extend an existing revocation list with a given key ID",
3838
SilenceUsage: true,
3939
RunE: func(cmd *cobra.Command, args []string) error {
40-
return handleExtendRevocationList(keyID, revocationListFile, privateRootKeyFile)
40+
return handleExtendRevocationList(cmd, keyID, revocationListFile, privateRootKeyFile)
4141
},
4242
}
4343

@@ -46,7 +46,7 @@ var verifyRevocationListCmd = &cobra.Command{
4646
Short: "Verify a revocation list signature using the public root key",
4747
SilenceUsage: true,
4848
RunE: func(cmd *cobra.Command, args []string) error {
49-
return handleVerifyRevocationList(revocationListFile, signatureFile, publicRootKeyFile)
49+
return handleVerifyRevocationList(cmd, revocationListFile, signatureFile, publicRootKeyFile)
5050
},
5151
}
5252

@@ -93,7 +93,7 @@ func init() {
9393
}
9494
}
9595

96-
func handleCreateRevocationList(revocationListFile string, privateRootKeyFile string) error {
96+
func handleCreateRevocationList(cmd *cobra.Command, revocationListFile string, privateRootKeyFile string) error {
9797
privKeyPEM, err := os.ReadFile(privateRootKeyFile)
9898
if err != nil {
9999
return fmt.Errorf("failed to read private root key file: %w", err)
@@ -113,11 +113,11 @@ func handleCreateRevocationList(revocationListFile string, privateRootKeyFile st
113113
return fmt.Errorf("failed to write output files: %w", err)
114114
}
115115

116-
fmt.Println("✅ Revocation list created successfully")
116+
cmd.Println("✅ Revocation list created successfully")
117117
return nil
118118
}
119119

120-
func handleExtendRevocationList(keyID, revocationListFile, privateRootKeyFile string) error {
120+
func handleExtendRevocationList(cmd *cobra.Command, keyID, revocationListFile, privateRootKeyFile string) error {
121121
privKeyPEM, err := os.ReadFile(privateRootKeyFile)
122122
if err != nil {
123123
return fmt.Errorf("failed to read private root key file: %w", err)
@@ -152,11 +152,11 @@ func handleExtendRevocationList(keyID, revocationListFile, privateRootKeyFile st
152152
return fmt.Errorf("failed to write output files: %w", err)
153153
}
154154

155-
fmt.Println("✅ Revocation list extended successfully")
155+
cmd.Println("✅ Revocation list extended successfully")
156156
return nil
157157
}
158158

159-
func handleVerifyRevocationList(revocationListFile, signatureFile, publicRootKeyFile string) error {
159+
func handleVerifyRevocationList(cmd *cobra.Command, revocationListFile, signatureFile, publicRootKeyFile string) error {
160160
// Read revocation list file
161161
rlBytes, err := os.ReadFile(revocationListFile)
162162
if err != nil {
@@ -194,15 +194,15 @@ func handleVerifyRevocationList(revocationListFile, signatureFile, publicRootKey
194194
}
195195

196196
// Display results
197-
fmt.Println("✅ Revocation list signature is valid")
198-
fmt.Printf("Last Updated: %s\n", rl.LastUpdated.Format(time.RFC3339))
199-
fmt.Printf("Expires At: %s\n", rl.ExpiresAt.Format(time.RFC3339))
200-
fmt.Printf("Number of revoked keys: %d\n", len(rl.Revoked))
197+
cmd.Println("✅ Revocation list signature is valid")
198+
cmd.Printf("Last Updated: %s\n", rl.LastUpdated.Format(time.RFC3339))
199+
cmd.Printf("Expires At: %s\n", rl.ExpiresAt.Format(time.RFC3339))
200+
cmd.Printf("Number of revoked keys: %d\n", len(rl.Revoked))
201201

202202
if len(rl.Revoked) > 0 {
203-
fmt.Println("\nRevoked Keys:")
203+
cmd.Println("\nRevoked Keys:")
204204
for keyID, revokedTime := range rl.Revoked {
205-
fmt.Printf(" - %s (revoked at: %s)\n", keyID, revokedTime.Format(time.RFC3339))
205+
cmd.Printf(" - %s (revoked at: %s)\n", keyID, revokedTime.Format(time.RFC3339))
206206
}
207207
}
208208

client/cmd/signer/rootkey.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var createRootKeyCmd = &cobra.Command{
2828
}
2929

3030
// Run main logic
31-
if err := handleGenerateRootKey(privKeyFile, pubKeyFile, rootExpiration); err != nil {
31+
if err := handleGenerateRootKey(cmd, privKeyFile, pubKeyFile, rootExpiration); err != nil {
3232
return fmt.Errorf("failed to generate root key: %w", err)
3333
}
3434
return nil
@@ -52,7 +52,7 @@ func init() {
5252
}
5353
}
5454

55-
func handleGenerateRootKey(privKeyFile, pubKeyFile string, expiration time.Duration) error {
55+
func handleGenerateRootKey(cmd *cobra.Command, privKeyFile, pubKeyFile string, expiration time.Duration) error {
5656
rk, privPEM, pubPEM, err := reposign.GenerateRootKey(expiration)
5757
if err != nil {
5858
return fmt.Errorf("generate root key: %w", err)
@@ -68,7 +68,7 @@ func handleGenerateRootKey(privKeyFile, pubKeyFile string, expiration time.Durat
6868
return fmt.Errorf("write public key file (%s): %w", pubKeyFile, err)
6969
}
7070

71-
fmt.Printf("%s\n\n", rk.String())
72-
fmt.Printf("✅ Root key pair generated successfully.\n")
71+
cmd.Printf("%s\n\n", rk.String())
72+
cmd.Printf("✅ Root key pair generated successfully.\n")
7373
return nil
7474
}

0 commit comments

Comments
 (0)