Migrated command line commands and flags handling to cobra framework.
This commit is contained in:
7
go.mod
7
go.mod
@@ -2,13 +2,18 @@ module koszewscy.waw.pl/slawek/lab-ca
|
|||||||
|
|
||||||
go 1.24.5
|
go 1.24.5
|
||||||
|
|
||||||
require github.com/hashicorp/hcl/v2 v2.24.0
|
require (
|
||||||
|
github.com/hashicorp/hcl/v2 v2.24.0
|
||||||
|
github.com/spf13/cobra v1.9.1
|
||||||
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/agext/levenshtein v1.2.1 // indirect
|
github.com/agext/levenshtein v1.2.1 // indirect
|
||||||
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
|
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
|
||||||
github.com/google/go-cmp v0.6.0 // indirect
|
github.com/google/go-cmp v0.6.0 // indirect
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
|
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
|
||||||
|
github.com/spf13/pflag v1.0.6 // indirect
|
||||||
github.com/zclconf/go-cty v1.16.3 // indirect
|
github.com/zclconf/go-cty v1.16.3 // indirect
|
||||||
golang.org/x/mod v0.17.0 // indirect
|
golang.org/x/mod v0.17.0 // indirect
|
||||||
golang.org/x/sync v0.14.0 // indirect
|
golang.org/x/sync v0.14.0 // indirect
|
||||||
|
59
main.go
59
main.go
@@ -6,7 +6,6 @@ import (
|
|||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
"crypto/x509/pkix"
|
"crypto/x509/pkix"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
@@ -14,6 +13,7 @@ import (
|
|||||||
|
|
||||||
gohcl "github.com/hashicorp/hcl/v2/gohcl"
|
gohcl "github.com/hashicorp/hcl/v2/gohcl"
|
||||||
hclparse "github.com/hashicorp/hcl/v2/hclparse"
|
hclparse "github.com/hashicorp/hcl/v2/hclparse"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CAConfig represents a CA block in HCL
|
// CAConfig represents a CA block in HCL
|
||||||
@@ -131,13 +131,38 @@ func (c *CAConfig) Validate() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
initCA := flag.Bool("initca", false, "Generate a new CA certificate and key")
|
var configPath string
|
||||||
configPath := flag.String("config", "ca_config.hcl", "Path to CA configuration file")
|
var overwrite bool
|
||||||
overwrite := flag.Bool("overwrite", false, "Allow overwriting existing files")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if *initCA {
|
var rootCmd = &cobra.Command{
|
||||||
config, err := LoadCAConfig(*configPath)
|
Use: "lab-ca",
|
||||||
|
Short: "Certificate Authority Utility",
|
||||||
|
Long: "lab-ca - Certificate Authority Utility",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
printMainHelp()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var initcaCmd = &cobra.Command{
|
||||||
|
Use: "initca",
|
||||||
|
Short: "Generate a new CA certificate and key",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
handleInitCA(configPath, overwrite)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
initcaCmd.Flags().StringVar(&configPath, "config", "ca_config.hcl", "Path to CA configuration file")
|
||||||
|
initcaCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Allow overwriting existing files")
|
||||||
|
|
||||||
|
rootCmd.AddCommand(initcaCmd)
|
||||||
|
|
||||||
|
if err := rootCmd.Execute(); err != nil {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func handleInitCA(configPath string, overwrite bool) {
|
||||||
|
config, err := LoadCAConfig(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error loading config:", err)
|
fmt.Println("Error loading config:", err)
|
||||||
return
|
return
|
||||||
@@ -147,17 +172,25 @@ func main() {
|
|||||||
fmt.Println("Error generating CA:", err)
|
fmt.Println("Error generating CA:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := SavePEM("ca_cert.pem", certPEM, false, *overwrite); err != nil {
|
if err := SavePEM("ca_cert.pem", certPEM, false, overwrite); err != nil {
|
||||||
fmt.Println("Error saving CA certificate:", err)
|
fmt.Println("Error saving CA certificate:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := SavePEM("ca_key.pem", keyPEM, true, *overwrite); err != nil {
|
if err := SavePEM("ca_key.pem", keyPEM, true, overwrite); err != nil {
|
||||||
fmt.Println("Error saving CA key:", err)
|
fmt.Println("Error saving CA key:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fmt.Println("CA certificate and key generated.")
|
fmt.Println("CA certificate and key generated.")
|
||||||
return
|
}
|
||||||
}
|
|
||||||
|
func printMainHelp() {
|
||||||
fmt.Println("No action specified. Use -initca to generate a CA.")
|
fmt.Println("lab-ca - Certificate Authority Utility")
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println("Usage:")
|
||||||
|
fmt.Println(" lab-ca <command> [options]")
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println("Available commands:")
|
||||||
|
fmt.Println(" initca Generate a new CA certificate and key")
|
||||||
|
fmt.Println()
|
||||||
|
fmt.Println("Use 'lab-ca <command> --help' for more information about a command.")
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user