You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
7.6 KiB

  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. // +build bindata
  5. package cmd
  6. import (
  7. "errors"
  8. "fmt"
  9. "os"
  10. "path/filepath"
  11. "sort"
  12. "strings"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/options"
  15. "code.gitea.io/gitea/modules/public"
  16. "code.gitea.io/gitea/modules/setting"
  17. "code.gitea.io/gitea/modules/templates"
  18. "github.com/gobwas/glob"
  19. "github.com/urfave/cli"
  20. )
  21. // Cmdembedded represents the available extract sub-command.
  22. var (
  23. Cmdembedded = cli.Command{
  24. Name: "embedded",
  25. Usage: "Extract embedded resources",
  26. Description: "A command for extracting embedded resources, like templates and images",
  27. Subcommands: []cli.Command{
  28. subcmdList,
  29. subcmdView,
  30. subcmdExtract,
  31. },
  32. }
  33. subcmdList = cli.Command{
  34. Name: "list",
  35. Usage: "List files matching the given pattern",
  36. Action: runList,
  37. Flags: []cli.Flag{
  38. cli.BoolFlag{
  39. Name: "include-vendored,vendor",
  40. Usage: "Include files under public/vendor as well",
  41. },
  42. },
  43. }
  44. subcmdView = cli.Command{
  45. Name: "view",
  46. Usage: "View a file matching the given pattern",
  47. Action: runView,
  48. Flags: []cli.Flag{
  49. cli.BoolFlag{
  50. Name: "include-vendored,vendor",
  51. Usage: "Include files under public/vendor as well",
  52. },
  53. },
  54. }
  55. subcmdExtract = cli.Command{
  56. Name: "extract",
  57. Usage: "Extract resources",
  58. Action: runExtract,
  59. Flags: []cli.Flag{
  60. cli.BoolFlag{
  61. Name: "include-vendored,vendor",
  62. Usage: "Include files under public/vendor as well",
  63. },
  64. cli.BoolFlag{
  65. Name: "overwrite",
  66. Usage: "Overwrite files if they already exist",
  67. },
  68. cli.BoolFlag{
  69. Name: "rename",
  70. Usage: "Rename files as {name}.bak if they already exist (overwrites previous .bak)",
  71. },
  72. cli.BoolFlag{
  73. Name: "custom",
  74. Usage: "Extract to the 'custom' directory as per app.ini",
  75. },
  76. cli.StringFlag{
  77. Name: "destination,dest-dir",
  78. Usage: "Extract to the specified directory",
  79. },
  80. },
  81. }
  82. sections map[string]*section
  83. assets []asset
  84. )
  85. type section struct {
  86. Path string
  87. Names func() []string
  88. IsDir func(string) (bool, error)
  89. Asset func(string) ([]byte, error)
  90. }
  91. type asset struct {
  92. Section *section
  93. Name string
  94. Path string
  95. }
  96. func initEmbeddedExtractor(c *cli.Context) error {
  97. // Silence the console logger
  98. log.DelNamedLogger("console")
  99. log.DelNamedLogger(log.DEFAULT)
  100. // Read configuration file
  101. setting.NewContext()
  102. pats, err := getPatterns(c.Args())
  103. if err != nil {
  104. return err
  105. }
  106. sections := make(map[string]*section, 3)
  107. sections["public"] = &section{Path: "public", Names: public.AssetNames, IsDir: public.AssetIsDir, Asset: public.Asset}
  108. sections["options"] = &section{Path: "options", Names: options.AssetNames, IsDir: options.AssetIsDir, Asset: options.Asset}
  109. sections["templates"] = &section{Path: "templates", Names: templates.AssetNames, IsDir: templates.AssetIsDir, Asset: templates.Asset}
  110. for _, sec := range sections {
  111. assets = append(assets, buildAssetList(sec, pats, c)...)
  112. }
  113. // Sort assets
  114. sort.SliceStable(assets, func(i, j int) bool {
  115. return assets[i].Path < assets[j].Path
  116. })
  117. return nil
  118. }
  119. func runList(c *cli.Context) error {
  120. if err := runListDo(c); err != nil {
  121. fmt.Fprintf(os.Stderr, "%v\n", err)
  122. return err
  123. }
  124. return nil
  125. }
  126. func runView(c *cli.Context) error {
  127. if err := runViewDo(c); err != nil {
  128. fmt.Fprintf(os.Stderr, "%v\n", err)
  129. return err
  130. }
  131. return nil
  132. }
  133. func runExtract(c *cli.Context) error {
  134. if err := runExtractDo(c); err != nil {
  135. fmt.Fprintf(os.Stderr, "%v\n", err)
  136. return err
  137. }
  138. return nil
  139. }
  140. func runListDo(c *cli.Context) error {
  141. if err := initEmbeddedExtractor(c); err != nil {
  142. return err
  143. }
  144. for _, a := range assets {
  145. fmt.Println(a.Path)
  146. }
  147. return nil
  148. }
  149. func runViewDo(c *cli.Context) error {
  150. if err := initEmbeddedExtractor(c); err != nil {
  151. return err
  152. }
  153. if len(assets) == 0 {
  154. return fmt.Errorf("No files matched the given pattern")
  155. } else if len(assets) > 1 {
  156. return fmt.Errorf("Too many files matched the given pattern; try to be more specific")
  157. }
  158. data, err := assets[0].Section.Asset(assets[0].Name)
  159. if err != nil {
  160. return fmt.Errorf("%s: %v", assets[0].Path, err)
  161. }
  162. if _, err = os.Stdout.Write(data); err != nil {
  163. return fmt.Errorf("%s: %v", assets[0].Path, err)
  164. }
  165. return nil
  166. }
  167. func runExtractDo(c *cli.Context) error {
  168. if err := initEmbeddedExtractor(c); err != nil {
  169. return err
  170. }
  171. if len(c.Args()) == 0 {
  172. return fmt.Errorf("A list of pattern of files to extract is mandatory (e.g. '**' for all)")
  173. }
  174. destdir := "."
  175. if c.IsSet("destination") {
  176. destdir = c.String("destination")
  177. } else if c.Bool("custom") {
  178. destdir = setting.CustomPath
  179. fmt.Println("Using app.ini at", setting.CustomConf)
  180. }
  181. fi, err := os.Stat(destdir)
  182. if errors.Is(err, os.ErrNotExist) {
  183. // In case Windows users attempt to provide a forward-slash path
  184. wdestdir := filepath.FromSlash(destdir)
  185. if wfi, werr := os.Stat(wdestdir); werr == nil {
  186. destdir = wdestdir
  187. fi = wfi
  188. err = nil
  189. }
  190. }
  191. if err != nil {
  192. return fmt.Errorf("%s: %s", destdir, err)
  193. } else if !fi.IsDir() {
  194. return fmt.Errorf("%s is not a directory.", destdir)
  195. }
  196. fmt.Printf("Extracting to %s:\n", destdir)
  197. overwrite := c.Bool("overwrite")
  198. rename := c.Bool("rename")
  199. for _, a := range assets {
  200. if err := extractAsset(destdir, a, overwrite, rename); err != nil {
  201. // Non-fatal error
  202. fmt.Fprintf(os.Stderr, "%s: %v", a.Path, err)
  203. }
  204. }
  205. return nil
  206. }
  207. func extractAsset(d string, a asset, overwrite, rename bool) error {
  208. dest := filepath.Join(d, filepath.FromSlash(a.Path))
  209. dir := filepath.Dir(dest)
  210. data, err := a.Section.Asset(a.Name)
  211. if err != nil {
  212. return fmt.Errorf("%s: %v", a.Path, err)
  213. }
  214. if err := os.MkdirAll(dir, os.ModePerm); err != nil {
  215. return fmt.Errorf("%s: %v", dir, err)
  216. }
  217. perms := os.ModePerm & 0666
  218. fi, err := os.Lstat(dest)
  219. if err != nil {
  220. if !errors.Is(err, os.ErrNotExist) {
  221. return fmt.Errorf("%s: %v", dest, err)
  222. }
  223. } else if !overwrite && !rename {
  224. fmt.Printf("%s already exists; skipped.\n", dest)
  225. return nil
  226. } else if !fi.Mode().IsRegular() {
  227. return fmt.Errorf("%s already exists, but it's not a regular file", dest)
  228. } else if rename {
  229. if err := os.Rename(dest, dest+".bak"); err != nil {
  230. return fmt.Errorf("Error creating backup for %s: %v", dest, err)
  231. }
  232. // Attempt to respect file permissions mask (even if user:group will be set anew)
  233. perms = fi.Mode()
  234. }
  235. file, err := os.OpenFile(dest, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, perms)
  236. if err != nil {
  237. return fmt.Errorf("%s: %v", dest, err)
  238. }
  239. defer file.Close()
  240. if _, err = file.Write(data); err != nil {
  241. return fmt.Errorf("%s: %v", dest, err)
  242. }
  243. fmt.Println(dest)
  244. return nil
  245. }
  246. func buildAssetList(sec *section, globs []glob.Glob, c *cli.Context) []asset {
  247. var results = make([]asset, 0, 64)
  248. for _, name := range sec.Names() {
  249. if isdir, err := sec.IsDir(name); !isdir && err == nil {
  250. if sec.Path == "public" &&
  251. strings.HasPrefix(name, "vendor/") &&
  252. !c.Bool("include-vendored") {
  253. continue
  254. }
  255. matchName := sec.Path + "/" + name
  256. for _, g := range globs {
  257. if g.Match(matchName) {
  258. results = append(results, asset{Section: sec,
  259. Name: name,
  260. Path: sec.Path + "/" + name})
  261. break
  262. }
  263. }
  264. }
  265. }
  266. return results
  267. }
  268. func getPatterns(args []string) ([]glob.Glob, error) {
  269. if len(args) == 0 {
  270. args = []string{"**"}
  271. }
  272. pat := make([]glob.Glob, len(args))
  273. for i := range args {
  274. if g, err := glob.Compile(args[i], '/'); err != nil {
  275. return nil, fmt.Errorf("'%s': Invalid glob pattern: %v", args[i], err)
  276. } else {
  277. pat[i] = g
  278. }
  279. }
  280. return pat, nil
  281. }