Skip to content

Commit 634e3d8

Browse files
feat(cli): add --json output flag for scriptability (#1346)
* feat(cli): add --json output flag for scriptability Add --json flag to CLI commands across all components for machine-readable output, enabling CI/CD integration and scripting. Commands updated: - nhp-agent: keygen, pubkey - nhp-server: keygen - nhp-ac: keygen - nhp-device: keygen, pubkey - kgc: keygen, sign, verify JSON output format: - keygen: {"privateKey": "...", "publicKey": "..."} - pubkey: {"publicKey": "..."} - sign: {"signature": "..."} - verify: {"valid": true/false} - errors: {"error": "..."} * style: fix gofmt formatting
1 parent 8a472c4 commit 634e3d8

File tree

5 files changed

+187
-30
lines changed

5 files changed

+187
-30
lines changed

endpoints/ac/main/main.go

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

33
import (
4+
"encoding/json"
45
"fmt"
56
"os"
67
"os/signal"
@@ -49,6 +50,7 @@ func main() {
4950
Flags: []cli.Flag{
5051
&cli.BoolFlag{Name: "curve", Value: false, DisableDefaultText: true, Usage: "generate curve25519 keys"},
5152
&cli.BoolFlag{Name: "sm2", Value: false, DisableDefaultText: true, Usage: "generate sm2 keys (default)"},
53+
&cli.BoolFlag{Name: "json", Value: false, DisableDefaultText: true, Usage: "output in JSON format"},
5254
},
5355
Action: func(c *cli.Context) error {
5456
var e core.Ecdh
@@ -59,8 +61,16 @@ func main() {
5961
e = core.NewECDH(eccType)
6062
pub := e.PublicKeyBase64()
6163
priv := e.PrivateKeyBase64()
62-
fmt.Println("Private key: ", priv)
63-
fmt.Println("Public key: ", pub)
64+
if c.Bool("json") {
65+
output := map[string]string{
66+
"privateKey": priv,
67+
"publicKey": pub,
68+
}
69+
json.NewEncoder(os.Stdout).Encode(output)
70+
} else {
71+
fmt.Println("Private key: ", priv)
72+
fmt.Println("Public key: ", pub)
73+
}
6474
return nil
6575
},
6676
}
@@ -77,13 +87,13 @@ func main() {
7787
func printBanner() {
7888
banner := `
7989
` + colorCyan + colorBold + `
80-
____ _ _ _ _ ____
81-
/ __ \ | \ | | | | | _ \
90+
____ _ _ _ _ ____
91+
/ __ \ | \ | | | | | _ \
8292
| | | |_ __ ___ _ __ | \| | |_| | |_) |
83-
| | | | '_ \ / _ \ '_ \| . ' | _ | __/
84-
| |__| | |_) | __/ | | | |\ | | | | |
85-
\____/| .__/ \___|_| |_|_| \_|_| |_|_|
86-
| |
93+
| | | | '_ \ / _ \ '_ \| . ' | _ | __/
94+
| |__| | |_) | __/ | | | |\ | | | | |
95+
\____/| .__/ \___|_| |_|_| \_|_| |_|_|
96+
| |
8797
|_| ` + colorReset + colorDim + `Network-infrastructure Hiding Protocol` + colorReset + `
8898
` + colorPurple + `
8999
⭐ GitHub: ` + colorReset + `https://github.com/OpenNHP/opennhp

endpoints/agent/main/main.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/base64"
5+
"encoding/json"
56
"fmt"
67
"os"
78
"os/signal"
@@ -49,6 +50,7 @@ func main() {
4950
Flags: []cli.Flag{
5051
&cli.BoolFlag{Name: "curve", Value: false, DisableDefaultText: true, Usage: "generate curve25519 keys"},
5152
&cli.BoolFlag{Name: "sm2", Value: false, DisableDefaultText: true, Usage: "generate sm2 keys"},
53+
&cli.BoolFlag{Name: "json", Value: false, DisableDefaultText: true, Usage: "output in JSON format"},
5254
},
5355
Action: func(c *cli.Context) error {
5456
var e core.Ecdh
@@ -59,8 +61,16 @@ func main() {
5961
e = core.NewECDH(eccType)
6062
pub := e.PublicKeyBase64()
6163
priv := e.PrivateKeyBase64()
62-
fmt.Println("Private key: ", priv)
63-
fmt.Println("Public key: ", pub)
64+
if c.Bool("json") {
65+
output := map[string]string{
66+
"privateKey": priv,
67+
"publicKey": pub,
68+
}
69+
json.NewEncoder(os.Stdout).Encode(output)
70+
} else {
71+
fmt.Println("Private key: ", priv)
72+
fmt.Println("Public key: ", pub)
73+
}
6474
return nil
6575
},
6676
}
@@ -71,10 +81,17 @@ func main() {
7181
Flags: []cli.Flag{
7282
&cli.BoolFlag{Name: "curve", Value: false, DisableDefaultText: true, Usage: "get curve25519 key"},
7383
&cli.BoolFlag{Name: "sm2", Value: false, DisableDefaultText: true, Usage: "get sm2 key (default)"},
84+
&cli.BoolFlag{Name: "json", Value: false, DisableDefaultText: true, Usage: "output in JSON format"},
7485
},
7586
Action: func(c *cli.Context) error {
7687
privKey, err := base64.StdEncoding.DecodeString(c.Args().First())
7788
if err != nil {
89+
if c.Bool("json") {
90+
json.NewEncoder(os.Stdout).Encode(map[string]interface{}{
91+
"error": err.Error(),
92+
})
93+
return nil
94+
}
7895
return err
7996
}
8097
eccType := core.ECC_SM2
@@ -83,10 +100,23 @@ func main() {
83100
}
84101
e := core.ECDHFromKey(eccType, privKey)
85102
if e == nil {
86-
return fmt.Errorf("invalid input key")
103+
err := fmt.Errorf("invalid input key")
104+
if c.Bool("json") {
105+
json.NewEncoder(os.Stdout).Encode(map[string]interface{}{
106+
"error": err.Error(),
107+
})
108+
return nil
109+
}
110+
return err
87111
}
88112
pub := e.PublicKeyBase64()
89-
fmt.Println("Public key: ", pub)
113+
if c.Bool("json") {
114+
json.NewEncoder(os.Stdout).Encode(map[string]string{
115+
"publicKey": pub,
116+
})
117+
} else {
118+
fmt.Println("Public key: ", pub)
119+
}
90120
return nil
91121
},
92122
}

endpoints/db/main/main.go

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func initApp() {
130130
Flags: []cli.Flag{
131131
&cli.BoolFlag{Name: "curve", Value: false, DisableDefaultText: true, Usage: "generate curve25519 keys"},
132132
&cli.BoolFlag{Name: "sm2", Value: false, DisableDefaultText: true, Usage: "generate sm2 keys"},
133+
&cli.BoolFlag{Name: "json", Value: false, DisableDefaultText: true, Usage: "output in JSON format"},
133134
},
134135
Action: func(c *cli.Context) error {
135136
var e core.Ecdh
@@ -140,8 +141,16 @@ func initApp() {
140141
e = core.NewECDH(eccType)
141142
pub := e.PublicKeyBase64()
142143
priv := e.PrivateKeyBase64()
143-
fmt.Println("Private key: ", priv)
144-
fmt.Println("Public key: ", pub)
144+
if c.Bool("json") {
145+
output := map[string]string{
146+
"privateKey": priv,
147+
"publicKey": pub,
148+
}
149+
json.NewEncoder(os.Stdout).Encode(output)
150+
} else {
151+
fmt.Println("Private key: ", priv)
152+
fmt.Println("Public key: ", pub)
153+
}
145154
return nil
146155
},
147156
}
@@ -152,10 +161,17 @@ func initApp() {
152161
Flags: []cli.Flag{
153162
&cli.BoolFlag{Name: "curve", Value: false, DisableDefaultText: true, Usage: "get curve25519 key"},
154163
&cli.BoolFlag{Name: "sm2", Value: false, DisableDefaultText: true, Usage: "get sm2 key"},
164+
&cli.BoolFlag{Name: "json", Value: false, DisableDefaultText: true, Usage: "output in JSON format"},
155165
},
156166
Action: func(c *cli.Context) error {
157167
privKey, err := base64.StdEncoding.DecodeString(c.Args().First())
158168
if err != nil {
169+
if c.Bool("json") {
170+
json.NewEncoder(os.Stdout).Encode(map[string]interface{}{
171+
"error": err.Error(),
172+
})
173+
return nil
174+
}
159175
return err
160176
}
161177
cipherType := core.ECC_SM2
@@ -164,10 +180,23 @@ func initApp() {
164180
}
165181
e := core.ECDHFromKey(cipherType, privKey)
166182
if e == nil {
167-
return fmt.Errorf("invalid input key")
183+
err := fmt.Errorf("invalid input key")
184+
if c.Bool("json") {
185+
json.NewEncoder(os.Stdout).Encode(map[string]interface{}{
186+
"error": err.Error(),
187+
})
188+
return nil
189+
}
190+
return err
168191
}
169192
pub := e.PublicKeyBase64()
170-
fmt.Println("Public key: ", pub)
193+
if c.Bool("json") {
194+
json.NewEncoder(os.Stdout).Encode(map[string]string{
195+
"publicKey": pub,
196+
})
197+
} else {
198+
fmt.Println("Public key: ", pub)
199+
}
171200
return nil
172201
},
173202
}

0 commit comments

Comments
 (0)