crtman/cmd/serve.go
2025-10-12 20:01:19 +08:00

89 lines
2.5 KiB
Go

/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"fmt"
"log"
"math/big"
"github.com/jasinco/crtman/internal/cli"
"github.com/jasinco/crtman/internal/crt"
"github.com/jasinco/crtman/internal/store"
"github.com/jasinco/crtman/internal/web"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var init_prog, reinit bool
// serveCmd represents the serve command
var serveCmd = &cobra.Command{
Use: "serve",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("serve called")
viper.BindPFlag("ca.organization", cmd.Flags().Lookup("ca_org"))
viper.BindPFlag("ca.fqdn", cmd.Flags().Lookup("ca_fqdn"))
viper.BindPFlags(cmd.Flags())
// config handle
err := viper.ReadInConfig()
if err != nil {
log.Printf("can't load config %e\n", err)
}
cli.GetListen()
cli.GetPersistent()
log.Printf("Get Persistent Path: %s", cli.PersistentPath)
store.InitStore(cli.PersistentPath)
defer store.CloseStore()
check_init := store.ChekInit()
if init_prog {
if check_init && !reinit {
log.Fatalln("you have initiazlized the program.")
}
cli.GetCA_CFG()
crt.IssueRoot(cli.CA_config)
store.Serial = *big.NewInt(1)
store.InitStoreData()
check_init = true
}
// Check Root CA
if !check_init {
log.Fatalln("Hasn't Initialized, CA_cert not found")
}
cli.ParseFederation()
web.Listen(cli.Host, cli.Port)
},
}
func init() {
rootCmd.AddCommand(serveCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// serveCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
serveCmd.Flags().StringP("store", "s", "", "The place the appstore its persistent stuff")
serveCmd.Flags().BoolVar(&init_prog, "init", false, "Init the settings, create CA, ...etc")
serveCmd.Flags().BoolVar(&reinit, "reinit", false, "It reinit the whole chain(ca, keys)")
serveCmd.Flags().String("ca_org", "", "the CA's organization")
serveCmd.Flags().String("ca_fqdn", "", "the CA's FQDN")
}