From 2fa004a2a018f9e6768d83d1f7467419f433fe8e Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Thu, 14 Oct 2021 12:25:55 -0700 Subject: [PATCH] cmd/cloner: emit go:generate pragmas (#3082) Emit a go:generate pragma with the full set of flags passed to cloner. This allows the user to simply run "go generate" at the location of the generate file to reproduce the file. Signed-off-by: Joe Tsai --- cmd/cloner/cloner.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/cmd/cloner/cloner.go b/cmd/cloner/cloner.go index f24632eee..91ed22582 100644 --- a/cmd/cloner/cloner.go +++ b/cmd/cloner/cloner.go @@ -95,7 +95,20 @@ func main() { } contents := new(bytes.Buffer) - fmt.Fprintf(contents, header, *flagTypes, pkg.Name) + var flagArgs []string + if *flagTypes != "" { + flagArgs = append(flagArgs, "-type="+*flagTypes) + } + if *flagOutput != "" { + flagArgs = append(flagArgs, "-output="+*flagOutput) + } + if *flagBuildTags != "" { + flagArgs = append(flagArgs, "-tags="+*flagBuildTags) + } + if *flagCloneFunc { + flagArgs = append(flagArgs, "-clonefunc") + } + fmt.Fprintf(contents, header, strings.Join(flagArgs, " "), pkg.Name) fmt.Fprintf(contents, "import (\n") for s := range imports { fmt.Fprintf(contents, "\t%q\n", s) @@ -117,8 +130,8 @@ const header = `// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserve // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// Code generated by the following command; DO NOT EDIT. -// tailscale.com/cmd/cloner -type %s +// Code generated by tailscale.com/cmd/cloner; DO NOT EDIT. +//go:generate go run tailscale.com/cmd/cloner %s package %s