From 4dbbd0aa4af8b715180a8293b3255b5bb8f7fb5e Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Tue, 13 Jul 2021 16:34:21 -0700 Subject: [PATCH] cmd/addlicense: add command to add licenseheaders to generated code And use it to make our stringer invocations match the existing code. Signed-off-by: Josh Bleecher Snyder --- cmd/addlicense/main.go | 77 +++++++++++++++++++++++++++++++++ derp/derp_server.go | 2 +- wgengine/magicsock/magicsock.go | 2 +- 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 cmd/addlicense/main.go diff --git a/cmd/addlicense/main.go b/cmd/addlicense/main.go new file mode 100644 index 000000000..0ca9dc5de --- /dev/null +++ b/cmd/addlicense/main.go @@ -0,0 +1,77 @@ +// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Program addlicense adds a license header to a file. +// It is intended for use with 'go generate', +// so it has a slightly weird usage. +package main + +import ( + "flag" + "fmt" + "os" + "os/exec" +) + +var ( + year = flag.Int("year", 0, "copyright year") + file = flag.String("file", "", "file to modify") +) + +func usage() { + fmt.Fprintf(os.Stderr, ` +usage: addlicense -year YEAR -file FILE +`[1:]) + + flag.PrintDefaults() + fmt.Fprintf(os.Stderr, ` +addlicense adds a Tailscale license to the beginning of file, +using year as the copyright year. + +It is intended for use with 'go generate', so it also runs a subcommand, +which presumably creates the file. + +Sample usage: + +addlicense -year 2021 -file pull_strings.go stringer -type=pull +`[1:]) + os.Exit(2) +} + +func main() { + flag.Usage = usage + flag.Parse() + if len(flag.Args()) == 0 { + flag.Usage() + } + cmd := exec.Command(flag.Arg(0), flag.Args()[1:]...) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + err := cmd.Run() + check(err) + b, err := os.ReadFile(*file) + check(err) + f, err := os.OpenFile(*file, os.O_TRUNC|os.O_WRONLY, 0644) + check(err) + _, err = fmt.Fprintf(f, license, *year) + check(err) + _, err = f.Write(b) + check(err) + err = f.Close() + check(err) +} + +func check(err error) { + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +var license = ` +// Copyright (c) %d Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +`[1:] diff --git a/derp/derp_server.go b/derp/derp_server.go index 32b09fb7b..70957a138 100644 --- a/derp/derp_server.go +++ b/derp/derp_server.go @@ -698,7 +698,7 @@ func (c *sclient) handleFrameSendPacket(ft frameType, fl uint32) error { // dropReason is why we dropped a DERP frame. type dropReason int -//go:generate stringer -type=dropReason -trimprefix=dropReason +//go:generate go run tailscale.com/cmd/addlicense -year 2021 -file dropreason_string.go stringer -type=dropReason -trimprefix=dropReason const ( dropReasonUnknownDest dropReason = iota // unknown destination pubkey diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index a3b89759d..220ffebf7 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -3489,7 +3489,7 @@ func (de *discoEndpoint) sendDiscoPing(ep netaddr.IPPort, txid stun.TxID, logLev // discoPingPurpose is the reason why a discovery ping message was sent. type discoPingPurpose int -//go:generate stringer -type=discoPingPurpose -trimprefix=ping +//go:generate go run tailscale.com/cmd/addlicense -year 2020 -file discopingpurpose_string.go stringer -type=discoPingPurpose -trimprefix=ping const ( // pingDiscovery means that purpose of a ping was to see if a // path was valid.