package main import ( "fmt" create "github.com/drone/go-license/cmd/license-create" keygen "github.com/drone/go-license/cmd/license-keygen" renew "github.com/drone/go-license/cmd/license-renew" server "github.com/drone/go-license/cmd/license-server" verify "github.com/drone/go-license/cmd/license-verify" "github.com/spf13/cobra" "os" "time" ) var ( licenseKeyGenArgs keygen.Args licenseCreateArgs create.Args licenseRenewArgs renew.Args licenseVerifyArgs verify.Args ) var rootCmd = &cobra.Command{ Use: "go-license", Short: "go license tool", Long: `This tool is used to generate ed25519 signed license`, } var keygenCmd = &cobra.Command{ Use: "keygen --out=id_ed25519", Short: "Output license public and private keys", Args: cobra.MaximumNArgs(1), DisableFlagsInUseLine: true, Run: func(cmd *cobra.Command, args []string) { keygen.LicenseKeyGen(licenseKeyGenArgs) }, } var createCmd = &cobra.Command{ Use: "create --iss=company_name --cus=user_name --sub=user_id --typ=internal --exp=12h", Short: "Create a certificate signed with ed25519", Long: `Create a certificate signed with ed25519`, Run: func(cmd *cobra.Command, args []string) { fmt.Printf("args:%+v\n", licenseCreateArgs) create.LicenseCreate(licenseCreateArgs) }, } var renewCmd = &cobra.Command{ Use: "renew --pub=public_key --file=file.txt --addr=http://localhost:9000", Short: "Renew a license", Long: `Renew a license`, //Args: cobra.ExactArgs(0), Run: func(cmd *cobra.Command, args []string) { fmt.Printf("args:%+v\n", licenseRenewArgs) renew.LicenseRenew(licenseRenewArgs) }, } var serverCmd = &cobra.Command{ Use: "server --addr=:9000 --renewal=30d --signer=id_ed25519", Short: "Start the server", Run: func(cmd *cobra.Command, args []string) { fmt.Printf("The service listens on:%s signer:%s renewal:%s\n", server.Payload.Addr, server.Payload.Signer, server.Payload.Renewal) server.LicenseServer() }, } var verifyCmd = &cobra.Command{ Use: "verify --pub=id_ed25519.pub --file=license.txt", Short: "Validate license and return the plaintext license content", Run: func(cmd *cobra.Command, args []string) { fmt.Printf("args:%+v\n", licenseVerifyArgs) verify.LicenseVerify(licenseVerifyArgs) }, } var healthCmd = &cobra.Command{ Use: "health --pub=id_ed25519.pub --file=license.txt", Short: "Validate license and return the plaintext license content", Run: func(cmd *cobra.Command, args []string) { fmt.Printf("args:%+v\n", licenseVerifyArgs) verify.LicenseHealth(licenseVerifyArgs) }, } /* privateKey, _ := fs.ReadFile(Files, "id_ed25519") publicKey, _ := fs.ReadFile(Files, "id_ed25519.pub") fmt.Println(string(privateKey)) fmt.Println(string(publicKey)) */ func init() { keygenCmd.Flags().StringVarP(&licenseKeyGenArgs.Out, "out", "", "id_ed25519", "license file name") rootCmd.AddCommand(keygenCmd) createCmd.Flags().StringVarP(&licenseCreateArgs.In, "in", "", "empty", "Input data") // 兼容内容:去除默认值,在函数中判断,id_ed25519 createCmd.Flags().StringVarP(&licenseCreateArgs.Out, "out", "", "license.txt", "license file") createCmd.Flags().StringVarP(&licenseCreateArgs.Iss, "iss", "", "", "Issuer") createCmd.Flags().StringVarP(&licenseCreateArgs.Cus, "cus", "", "", "Customer") createCmd.Flags().StringVarP(&licenseCreateArgs.Sub, "sub", "", "", "Subject") createCmd.Flags().StringVarP(&licenseCreateArgs.Typ, "typ", "", "", "Type") createCmd.Flags().StringVarP(&licenseCreateArgs.Dat, "dat", "", "", "JSON data") createCmd.Flags().IntVarP(&licenseCreateArgs.Lim, "lim", "", 0, "Limit") createCmd.Flags().DurationVarP(&licenseCreateArgs.Exp, "exp", "", 0, "Expiration time") rootCmd.AddCommand(createCmd) renewCmd.Flags().StringVarP(&licenseRenewArgs.Pub, "pub", "", "empty", "Public key") // id_ed25519.pub renewCmd.Flags().StringVarP(&licenseRenewArgs.File, "file", "", "license.txt", "File name") renewCmd.Flags().StringVarP(&licenseRenewArgs.Addr, "addr", "", "http://localhost:9000", "Ip Address") rootCmd.AddCommand(renewCmd) serverCmd.Flags().StringVarP(&server.Payload.Addr, "addr", "", ":9000", "Address to bind to") serverCmd.Flags().DurationVarP(&server.Payload.Renewal, "renewal", "", time.Hour*24*30, "Certificate renewal interval in seconds") serverCmd.Flags().StringVarP(&server.Payload.Signer, "signer", "", "empty", "Certificate signer") // id_ed25519 rootCmd.AddCommand(serverCmd) verifyCmd.Flags().StringVarP(&licenseVerifyArgs.Pub, "pub", "", "empty", "Public key file path") //id_ed25519.pub verifyCmd.Flags().StringVarP(&licenseVerifyArgs.File, "file", "", "license.txt", "File to verify") rootCmd.AddCommand(verifyCmd) healthCmd.Flags().StringVarP(&licenseVerifyArgs.Pub, "pub", "", "empty", "Public key file path") //id_ed25519.pub healthCmd.Flags().StringVarP(&licenseVerifyArgs.File, "file", "", "license.txt", "File to verify") rootCmd.AddCommand(healthCmd) /*privateKey, _ := fs.ReadFile(license_keyinit.Files, "id_ed25519") publicKey, _ := fs.ReadFile(license_keyinit.Files, "id_ed25519.pub") fmt.Println("privateKey", string(privateKey)) fmt.Println("publicKey", string(publicKey))*/ } func main() { if err := rootCmd.Execute(); err != nil { fmt.Println(err) os.Exit(1) } }