Skip to content

Commit

Permalink
[add] pack & start command support license
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Apr 23, 2023
1 parent 95ff65d commit 082bcf5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 36 deletions.
13 changes: 12 additions & 1 deletion cmd/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down Expand Up @@ -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)
Expand All @@ -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"))
}
16 changes: 9 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -168,4 +165,9 @@ func Boot() {
} else if config.Conf.Mode == "development" {
config.Development()
}

// set license
if licenseKey != "" {
pack.SetCipher(licenseKey)
}
}
5 changes: 3 additions & 2 deletions engine/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions engine/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
32 changes: 32 additions & 0 deletions pack/pack.go
Original file line number Diff line number Diff line change
@@ -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)
}
22 changes: 0 additions & 22 deletions share/pack.go

This file was deleted.

3 changes: 2 additions & 1 deletion test/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
Expand Down

0 comments on commit 082bcf5

Please sign in to comment.