From 68e009ad06cbbe3f5fffd37bf556fea7b9f7faea Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 20 Apr 2023 17:42:07 +0800 Subject: [PATCH] [add] pack command --- cmd/pack.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 1 + 2 files changed, 81 insertions(+) create mode 100644 cmd/pack.go diff --git a/cmd/pack.go b/cmd/pack.go new file mode 100644 index 00000000..d690743a --- /dev/null +++ b/cmd/pack.go @@ -0,0 +1,80 @@ +package cmd + +import ( + "bufio" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/fatih/color" + "github.com/spf13/cobra" + "github.com/yaoapp/gou/application/yaz" + "github.com/yaoapp/yao/config" +) + +var packOutput = "" +var packCmd = &cobra.Command{ + Use: "pack", + Short: L("Package the application"), + Long: L("Package the application into a single file"), + Run: func(cmd *cobra.Command, args []string) { + + cfg := config.Conf + output, err := filepath.Abs(filepath.Join(cfg.Root, "dist")) + if err != nil { + color.Red(err.Error()) + } + + if packOutput != "" { + output, err = filepath.Abs(packOutput) + if err != nil { + color.Red(err.Error()) + } + } + + stat, err := os.Stat(output) + if err != nil && os.IsNotExist(err) { + color.Green("Creating directory %s", output) + err = os.MkdirAll(output, 0755) + if err != nil { + color.Red(err.Error()) + os.Exit(1) + } + } else if err != nil { + color.Red(err.Error()) + os.Exit(1) + + } else if !stat.IsDir() { + color.Red("Output directory %s is not a directory.\n", output) + os.Exit(1) + } + + outputFile := filepath.Join(output, "app.yaz") + _, err = os.Stat(outputFile) + if !os.IsNotExist(err) { + color.Yellow("%s already exists", outputFile) + fmt.Printf("%s", color.RedString("Do you want to overwrite it? (y/n): ")) + scanner := bufio.NewScanner(os.Stdin) + if scanner.Scan() { + if strings.ToLower(scanner.Text()) != "y" { + os.Exit(0) + return + } + } + } + + os.Remove(outputFile) + err = yaz.CompressTo(cfg.Root, outputFile) + if err != nil { + color.Red(err.Error()) + os.Exit(1) + } + + color.Green("Packaged to %s", outputFile) + }, +} + +func init() { + packCmd.PersistentFlags().StringVarP(&packOutput, "output", "o", "", L("Output Directory")) +} diff --git a/cmd/root.go b/cmd/root.go index 18864252..5cfa7e3b 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -122,6 +122,7 @@ func init() { restoreCmd, // socketCmd, // websocketCmd, + packCmd, studioCmd, upgradeCmd, )