cmd_pack.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package cmd
  7. import (
  8. "context"
  9. "strings"
  10. "github.com/gogf/gf/v2/frame/g"
  11. "github.com/gogf/gf/v2/os/gcmd"
  12. "github.com/gogf/gf/v2/os/gfile"
  13. "github.com/gogf/gf/v2/os/gres"
  14. "github.com/gogf/gf/v2/util/gtag"
  15. "github.com/gogf/gf/cmd/gf/v2/internal/utility/allyes"
  16. "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
  17. )
  18. var (
  19. Pack = cPack{}
  20. )
  21. type cPack struct {
  22. g.Meta `name:"pack" usage:"{cPackUsage}" brief:"{cPackBrief}" eg:"{cPackEg}"`
  23. }
  24. const (
  25. cPackUsage = `gf pack SRC DST`
  26. cPackBrief = `packing any file/directory to a resource file, or a go file`
  27. cPackEg = `
  28. gf pack public data.bin
  29. gf pack public,template data.bin
  30. gf pack public,template packed/data.go
  31. gf pack public,template,config packed/data.go
  32. gf pack public,template,config packed/data.go -n=packed -p=/var/www/my-app
  33. gf pack /var/www/public packed/data.go -n=packed
  34. `
  35. cPackSrcBrief = `source path for packing, which can be multiple source paths.`
  36. cPackDstBrief = `
  37. destination file path for packed file. if extension of the filename is ".go" and "-n" option is given,
  38. it enables packing SRC to go file, or else it packs SRC into a binary file.
  39. `
  40. cPackNameBrief = `package name for output go file, it's set as its directory name if no name passed`
  41. cPackPrefixBrief = `prefix for each file packed into the resource file`
  42. cPackKeepPathBrief = `keep the source path from system to resource file, usually for relative path`
  43. )
  44. func init() {
  45. gtag.Sets(g.MapStrStr{
  46. `cPackUsage`: cPackUsage,
  47. `cPackBrief`: cPackBrief,
  48. `cPackEg`: cPackEg,
  49. `cPackSrcBrief`: cPackSrcBrief,
  50. `cPackDstBrief`: cPackDstBrief,
  51. `cPackNameBrief`: cPackNameBrief,
  52. `cPackPrefixBrief`: cPackPrefixBrief,
  53. `cPackKeepPathBrief`: cPackKeepPathBrief,
  54. })
  55. }
  56. type cPackInput struct {
  57. g.Meta `name:"pack"`
  58. Src string `name:"SRC" arg:"true" v:"required" brief:"{cPackSrcBrief}"`
  59. Dst string `name:"DST" arg:"true" v:"required" brief:"{cPackDstBrief}"`
  60. Name string `name:"name" short:"n" brief:"{cPackNameBrief}"`
  61. Prefix string `name:"prefix" short:"p" brief:"{cPackPrefixBrief}"`
  62. KeepPath bool `name:"keepPath" short:"k" brief:"{cPackKeepPathBrief}" orphan:"true"`
  63. }
  64. type cPackOutput struct{}
  65. func (c cPack) Index(ctx context.Context, in cPackInput) (out *cPackOutput, err error) {
  66. if gfile.Exists(in.Dst) && gfile.IsDir(in.Dst) {
  67. mlog.Fatalf("DST path '%s' cannot be a directory", in.Dst)
  68. }
  69. if !gfile.IsEmpty(in.Dst) && !allyes.Check() {
  70. s := gcmd.Scanf("path '%s' is not empty, files might be overwrote, continue? [y/n]: ", in.Dst)
  71. if strings.EqualFold(s, "n") {
  72. return
  73. }
  74. }
  75. if in.Name == "" && gfile.ExtName(in.Dst) == "go" {
  76. in.Name = gfile.Basename(gfile.Dir(in.Dst))
  77. }
  78. var option = gres.Option{
  79. Prefix: in.Prefix,
  80. KeepPath: in.KeepPath,
  81. }
  82. if in.Name != "" {
  83. if err = gres.PackToGoFileWithOption(in.Src, in.Dst, in.Name, option); err != nil {
  84. mlog.Fatalf("pack failed: %v", err)
  85. }
  86. } else {
  87. if err = gres.PackToFileWithOption(in.Src, in.Dst, option); err != nil {
  88. mlog.Fatalf("pack failed: %v", err)
  89. }
  90. }
  91. mlog.Print("done!")
  92. return
  93. }