From 082bcf57ff58ade6970f2dd8f84e146cb7e708be Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 23 Apr 2023 10:57:28 +0800 Subject: [PATCH] [add] pack & start command support license --- cmd/pack.go | 13 ++++++++++++- cmd/root.go | 16 +++++++++------- engine/load.go | 5 +++-- engine/load_test.go | 6 +++--- pack/pack.go | 32 ++++++++++++++++++++++++++++++++ share/pack.go | 22 ---------------------- test/utils.go | 3 ++- 7 files changed, 61 insertions(+), 36 deletions(-) create mode 100644 pack/pack.go delete mode 100644 share/pack.go diff --git a/cmd/pack.go b/cmd/pack.go index d690743af1..078c31cbef 100644 --- a/cmd/pack.go +++ b/cmd/pack.go @@ -11,9 +11,12 @@ import ( "github.com/spf13/cobra" "github.com/yaoapp/gou/application/yaz" "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/pack" ) var packOutput = "" +var packLicense = "" + var packCmd = &cobra.Command{ Use: "pack", Short: L("Package the application"), @@ -65,7 +68,14 @@ var packCmd = &cobra.Command{ } os.Remove(outputFile) - err = yaz.CompressTo(cfg.Root, outputFile) + if packLicense != "" { + pack.SetCipher(packLicense) + err = yaz.PackTo(cfg.Root, outputFile, pack.Cipher) + + } else { + err = yaz.CompressTo(cfg.Root, outputFile) + } + if err != nil { color.Red(err.Error()) os.Exit(1) @@ -77,4 +87,5 @@ var packCmd = &cobra.Command{ func init() { packCmd.PersistentFlags().StringVarP(&packOutput, "output", "o", "", L("Output Directory")) + packCmd.PersistentFlags().StringVarP(&packLicense, "license", "l", "", L("Pack with the license")) } diff --git a/cmd/root.go b/cmd/root.go index 8ba4fb93f9..d8f6f02c35 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -9,12 +9,13 @@ import ( "github.com/yaoapp/kun/exception" "github.com/yaoapp/yao/cmd/studio" "github.com/yaoapp/yao/config" + "github.com/yaoapp/yao/pack" "github.com/yaoapp/yao/share" ) var appPath string -var envFile string var yazFile string +var licenseKey string var lang = os.Getenv("YAO_LANG") var langs = map[string]string{ @@ -130,7 +131,7 @@ func init() { // rootCmd.SetHelpCommand(helpCmd) rootCmd.PersistentFlags().StringVarP(&appPath, "app", "a", "", L("Application directory")) rootCmd.PersistentFlags().StringVarP(&yazFile, "file", "f", "", L("Application package file")) - rootCmd.PersistentFlags().StringVarP(&envFile, "env", "e", "", L("Environment file")) + rootCmd.PersistentFlags().StringVarP(&licenseKey, "key", "k", "", L("Application license key")) } // Execute 运行Root @@ -153,11 +154,7 @@ func Boot() { root = r } - if envFile != "" { - config.Conf = config.LoadFrom(envFile) - } else { - config.Conf = config.LoadFrom(filepath.Join(root, ".env")) - } + config.Conf = config.LoadFrom(filepath.Join(root, ".env")) if yazFile != "" { config.Conf.AppSource = yazFile @@ -168,4 +165,9 @@ func Boot() { } else if config.Conf.Mode == "development" { config.Development() } + + // set license + if licenseKey != "" { + pack.SetCipher(licenseKey) + } } diff --git a/engine/load.go b/engine/load.go index 12e66a86be..98aa025e3a 100644 --- a/engine/load.go +++ b/engine/load.go @@ -18,6 +18,7 @@ import ( "github.com/yaoapp/yao/i18n" "github.com/yaoapp/yao/importer" "github.com/yaoapp/yao/model" + "github.com/yaoapp/yao/pack" "github.com/yaoapp/yao/plugin" "github.com/yaoapp/yao/query" "github.com/yaoapp/yao/runtime" @@ -226,14 +227,14 @@ func loadApp(root string) error { var app application.Application if root == "bin:application.yaz" { - app, err = application.OpenFromYaz(root, &share.Pack{}) // Load app from Bin + app, err = application.OpenFromYaz(root, pack.Cipher) // Load app from Bin if err != nil { return err } application.Load(app) } else if strings.HasSuffix(root, ".yaz") { - app, err = application.OpenFromYaz(root, &share.Pack{}) // Load app from .yaz file + app, err = application.OpenFromYaz(root, pack.Cipher) // Load app from .yaz file if err != nil { return err } diff --git a/engine/load_test.go b/engine/load_test.go index d9c71f229a..1675cf2f25 100644 --- a/engine/load_test.go +++ b/engine/load_test.go @@ -8,7 +8,7 @@ import ( "github.com/yaoapp/gou/api" "github.com/yaoapp/gou/application/yaz" "github.com/yaoapp/yao/config" - "github.com/yaoapp/yao/share" + "github.com/yaoapp/yao/pack" ) func TestLoad(t *testing.T) { @@ -33,7 +33,7 @@ func TestLoadYaz(t *testing.T) { defer Unload() // package yaz - file, err := yaz.Pack(config.Conf.Root, &share.Pack{}) + file, err := yaz.Pack(config.Conf.Root,pack.Cipher) if err != nil { t.Fatal(err) } @@ -54,7 +54,7 @@ func TestReoadYaz(t *testing.T) { defer Unload() // package yaz - file, err := yaz.Pack(config.Conf.Root, &share.Pack{}) + file, err := yaz.Pack(config.Conf.Root, pack.Cipher) if err != nil { t.Fatal(err) } diff --git a/pack/pack.go b/pack/pack.go new file mode 100644 index 0000000000..573892a1b6 --- /dev/null +++ b/pack/pack.go @@ -0,0 +1,32 @@ +package pack + +// ******************************************************************************** +// WARNING: DO NOT MODIFY THIS FILE. IT WILL BE REPLACED BY THE APPLICATION CODE. +// ********************************************************************************* + +import ( + "io" + + "github.com/yaoapp/gou/application/yaz/ciphers" +) + +// Pack the yao app package +type Pack struct{ aes ciphers.AES } + +// Cipher the cipher +var Cipher *Pack + +// SetCipher set the cipher +func SetCipher(license string) { + Cipher = &Pack{aes: ciphers.NewAES([]byte(license))} +} + +// Encrypt encrypt +func (pack *Pack) Encrypt(reader io.Reader, writer io.Writer) error { + return pack.aes.Encrypt(reader, writer) +} + +// Decrypt decrypt +func (pack *Pack) Decrypt(reader io.Reader, writer io.Writer) error { + return pack.aes.Decrypt(reader, writer) +} diff --git a/share/pack.go b/share/pack.go deleted file mode 100644 index b1bc5f67ea..0000000000 --- a/share/pack.go +++ /dev/null @@ -1,22 +0,0 @@ -package share - -import "io" - -// ******************************************************************************** -// WARNING: DO NOT MODIFY THIS FILE. IT WILL BE REPLACED BY THE APPLICATION CODE. -// ********************************************************************************* - -// Pack the yao app package -type Pack struct{} - -// Encrypt encrypt -func (pkg *Pack) Encrypt(reader io.Reader, writer io.Writer) error { - _, err := io.Copy(writer, reader) - return err -} - -// Decrypt decrypt -func (pkg *Pack) Decrypt(reader io.Reader, writer io.Writer) error { - _, err := io.Copy(writer, reader) - return err -} diff --git a/test/utils.go b/test/utils.go index 3c3430cfdd..2a8abcfb04 100644 --- a/test/utils.go +++ b/test/utils.go @@ -21,6 +21,7 @@ import ( "github.com/yaoapp/yao/config" "github.com/yaoapp/yao/fs" "github.com/yaoapp/yao/helper" + "github.com/yaoapp/yao/pack" "github.com/yaoapp/yao/runtime" "github.com/yaoapp/yao/share" ) @@ -34,7 +35,7 @@ func Prepare(t *testing.T, cfg config.Config) { var err error if root == "bin:application.pkg" { - app, err = application.OpenFromBin(root, &share.Pack{}) // Load app from Bin + app, err = application.OpenFromBin(root, pack.Cipher) // Load app from Bin if err != nil { t.Fatal(err) }