[add] pack & start command support license

This commit is contained in:
Max 2023-04-23 10:57:28 +08:00
parent 95ff65d625
commit 082bcf57ff
7 changed files with 61 additions and 36 deletions

View file

@ -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)
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"))
}

View file

@ -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"))
}
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)
}
}

View file

@ -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
}

View file

@ -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)
}

32
pack/pack.go Normal file
View file

@ -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)
}

View file

@ -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
}

View file

@ -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)
}