main.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // mkpkg builds the Tailscale rpm and deb packages.
  4. package main
  5. import (
  6. "flag"
  7. "fmt"
  8. "log"
  9. "os"
  10. "strings"
  11. "github.com/goreleaser/nfpm/v2"
  12. _ "github.com/goreleaser/nfpm/v2/deb"
  13. "github.com/goreleaser/nfpm/v2/files"
  14. _ "github.com/goreleaser/nfpm/v2/rpm"
  15. )
  16. // parseFiles parses a comma-separated list of colon-separated pairs
  17. // into files.Contents format.
  18. func parseFiles(s string, typ string) (files.Contents, error) {
  19. if len(s) == 0 {
  20. return nil, nil
  21. }
  22. var contents files.Contents
  23. for _, f := range strings.Split(s, ",") {
  24. fs := strings.Split(f, ":")
  25. if len(fs) != 2 {
  26. return nil, fmt.Errorf("unparseable file field %q", f)
  27. }
  28. contents = append(contents, &files.Content{Type: files.TypeFile, Source: fs[0], Destination: fs[1]})
  29. }
  30. return contents, nil
  31. }
  32. func parseEmptyDirs(s string) files.Contents {
  33. // strings.Split("", ",") would return []string{""}, which is not suitable:
  34. // this would create an empty dir record with path "", breaking the package
  35. if s == "" {
  36. return nil
  37. }
  38. var contents files.Contents
  39. for _, d := range strings.Split(s, ",") {
  40. contents = append(contents, &files.Content{Type: files.TypeDir, Destination: d})
  41. }
  42. return contents
  43. }
  44. func main() {
  45. out := flag.String("out", "", "output file to write")
  46. name := flag.String("name", "tailscale", "package name")
  47. description := flag.String("description", "The easiest, most secure, cross platform way to use WireGuard + oauth2 + 2FA/SSO", "package description")
  48. goarch := flag.String("arch", "amd64", "GOARCH this package is for")
  49. pkgType := flag.String("type", "deb", "type of package to build (deb or rpm)")
  50. regularFiles := flag.String("files", "", "comma-separated list of files in src:dst form")
  51. configFiles := flag.String("configs", "", "like --files, but for files marked as user-editable config files")
  52. emptyDirs := flag.String("emptydirs", "", "comma-separated list of empty directories")
  53. version := flag.String("version", "0.0.0", "version of the package")
  54. postinst := flag.String("postinst", "", "debian postinst script path")
  55. prerm := flag.String("prerm", "", "debian prerm script path")
  56. postrm := flag.String("postrm", "", "debian postrm script path")
  57. replaces := flag.String("replaces", "", "package which this package replaces, if any")
  58. depends := flag.String("depends", "", "comma-separated list of packages this package depends on")
  59. recommends := flag.String("recommends", "", "comma-separated list of packages this package recommends")
  60. flag.Parse()
  61. filesList, err := parseFiles(*regularFiles, files.TypeFile)
  62. if err != nil {
  63. log.Fatalf("Parsing --files: %v", err)
  64. }
  65. configsList, err := parseFiles(*configFiles, files.TypeConfig)
  66. if err != nil {
  67. log.Fatalf("Parsing --configs: %v", err)
  68. }
  69. emptyDirList := parseEmptyDirs(*emptyDirs)
  70. contents := append(filesList, append(configsList, emptyDirList...)...)
  71. contents, err = files.PrepareForPackager(contents, 0, *pkgType, false)
  72. if err != nil {
  73. log.Fatalf("Building package contents: %v", err)
  74. }
  75. info := nfpm.WithDefaults(&nfpm.Info{
  76. Name: *name,
  77. Arch: *goarch,
  78. Platform: "linux",
  79. Version: *version,
  80. Maintainer: "Tailscale Inc <[email protected]>",
  81. Description: *description,
  82. Homepage: "https://www.tailscale.com",
  83. License: "MIT",
  84. Overridables: nfpm.Overridables{
  85. Contents: contents,
  86. Scripts: nfpm.Scripts{
  87. PostInstall: *postinst,
  88. PreRemove: *prerm,
  89. PostRemove: *postrm,
  90. },
  91. },
  92. })
  93. if len(*depends) != 0 {
  94. info.Overridables.Depends = strings.Split(*depends, ",")
  95. }
  96. if len(*recommends) != 0 {
  97. info.Overridables.Recommends = strings.Split(*recommends, ",")
  98. }
  99. if *replaces != "" {
  100. info.Overridables.Replaces = []string{*replaces}
  101. info.Overridables.Conflicts = []string{*replaces}
  102. }
  103. switch *pkgType {
  104. case "deb":
  105. info.Section = "net"
  106. info.Priority = "extra"
  107. case "rpm":
  108. info.Overridables.RPM.Group = "Network"
  109. }
  110. pkg, err := nfpm.Get(*pkgType)
  111. if err != nil {
  112. log.Fatalf("Getting packager for %q: %v", *pkgType, err)
  113. }
  114. f, err := os.Create(*out)
  115. if err != nil {
  116. log.Fatalf("Creating output file %q: %v", *out, err)
  117. }
  118. defer f.Close()
  119. if err := pkg.Package(info, f); err != nil {
  120. log.Fatalf("Creating package %q: %v", *out, err)
  121. }
  122. }