From 27373034b1332e185451dd02916fd474199bef22 Mon Sep 17 00:00:00 2001 From: Takuya Ueda Date: Fri, 10 Jul 2020 21:28:11 +0900 Subject: [PATCH] Fix nested directory --- _template/{@@.Pkg@@ => }/@@.Pkg@@.go | 0 _template/{@@.Pkg@@ => }/@@.Pkg@@_test.go | 0 _template/{@@.Pkg@@ => }/cmd/@@.Pkg@@/main.go | 0 _template/{@@.Pkg@@ => }/plugin/main.go | 0 _template/{@@.Pkg@@ => }/testdata/src/a/a.go | 0 main.go | 19 +++++++++++-------- template.go | 2 +- tools/txtar/main.go | 2 +- 8 files changed, 13 insertions(+), 10 deletions(-) rename _template/{@@.Pkg@@ => }/@@.Pkg@@.go (100%) rename _template/{@@.Pkg@@ => }/@@.Pkg@@_test.go (100%) rename _template/{@@.Pkg@@ => }/cmd/@@.Pkg@@/main.go (100%) rename _template/{@@.Pkg@@ => }/plugin/main.go (100%) rename _template/{@@.Pkg@@ => }/testdata/src/a/a.go (100%) diff --git a/_template/@@.Pkg@@/@@.Pkg@@.go b/_template/@@.Pkg@@.go similarity index 100% rename from _template/@@.Pkg@@/@@.Pkg@@.go rename to _template/@@.Pkg@@.go diff --git a/_template/@@.Pkg@@/@@.Pkg@@_test.go b/_template/@@.Pkg@@_test.go similarity index 100% rename from _template/@@.Pkg@@/@@.Pkg@@_test.go rename to _template/@@.Pkg@@_test.go diff --git a/_template/@@.Pkg@@/cmd/@@.Pkg@@/main.go b/_template/cmd/@@.Pkg@@/main.go similarity index 100% rename from _template/@@.Pkg@@/cmd/@@.Pkg@@/main.go rename to _template/cmd/@@.Pkg@@/main.go diff --git a/_template/@@.Pkg@@/plugin/main.go b/_template/plugin/main.go similarity index 100% rename from _template/@@.Pkg@@/plugin/main.go rename to _template/plugin/main.go diff --git a/_template/@@.Pkg@@/testdata/src/a/a.go b/_template/testdata/src/a/a.go similarity index 100% rename from _template/@@.Pkg@@/testdata/src/a/a.go rename to _template/testdata/src/a/a.go diff --git a/main.go b/main.go index 388f475..930ab0a 100644 --- a/main.go +++ b/main.go @@ -44,11 +44,12 @@ func main() { type Skeleton struct { ExeName string Args []string + Dir string + ImportPath string OverWrite bool Cmd bool Checker string Plugin bool - ImportPath string Mode Mode } @@ -78,11 +79,13 @@ func (s *Skeleton) Run() error { if len(s.Args) < 1 { if s.ImportPath != "" { + s.Dir = s.ImportPath td.Pkg = path.Base(s.ImportPath) } else { return errors.New("package must be specified") } } else { + s.Dir = s.Args[0] td.Pkg = path.Base(s.Args[0]) } @@ -91,19 +94,19 @@ func (s *Skeleton) Run() error { return err } - td.ImportPath = s.importPath(cwd, td.Pkg) + td.ImportPath = s.importPath(cwd) if td.ImportPath == "" { const format = "%s must be executed in GOPATH or -path option must be specified" return fmt.Errorf(format, s.ExeName) } - exist, err := isExist(td.Pkg) + exist, err := isExist(s.Dir) if err != nil { return err } if exist && !s.OverWrite { - if exit := s.selectMode(td.Pkg); exit { + if exit := s.selectMode(s.Dir); exit { return nil } } @@ -115,7 +118,7 @@ func (s *Skeleton) Run() error { return nil } -func (s *Skeleton) importPath(cwd string, pkg string) string { +func (s *Skeleton) importPath(cwd string) string { if s.ImportPath != "" { return s.ImportPath @@ -132,7 +135,7 @@ func (s *Skeleton) importPath(cwd string, pkg string) string { if err != nil { return "" } - return path.Join(filepath.ToSlash(rel), pkg) + return path.Join(filepath.ToSlash(rel), filepath.ToSlash(s.Dir)) } } @@ -165,7 +168,7 @@ func (s *Skeleton) selectMode(dir string) bool { func (s *Skeleton) createAll(td *TemplateData) error { if s.Mode == ModeRemoveAndCreateNew { - if err := os.RemoveAll(td.Pkg); err != nil { + if err := os.RemoveAll(s.Dir); err != nil { return err } } @@ -190,7 +193,7 @@ func (s *Skeleton) createFile(f txtar.File) (rerr error) { return nil } - path := filepath.FromSlash(f.Name) + path := filepath.Join(s.Dir, filepath.FromSlash(f.Name)) exist, err := isExist(path) if err != nil { diff --git a/template.go b/template.go index ea1b302..45f091b 100644 --- a/template.go +++ b/template.go @@ -4,4 +4,4 @@ package main import "text/template" -var tmpl = template.Must(template.New("template").Delims(`@@`, `@@`).Parse("-- @@.Pkg@@/@@.Pkg@@.go --\npackage @@.Pkg@@\n\nimport (\n\t\"go/ast\"\n\n\t\"golang.org/x/tools/go/analysis\"\n\t\"golang.org/x/tools/go/analysis/passes/inspect\"\n\t\"golang.org/x/tools/go/ast/inspector\"\n)\n\nconst doc = \"@@.Pkg@@ is ...\"\n\n// Analyzer is ...\nvar Analyzer = &analysis.Analyzer{\n\tName: \"@@.Pkg@@\",\n\tDoc: doc,\n\tRun: run,\n\tRequires: []*analysis.Analyzer{\n\t\tinspect.Analyzer,\n\t},\n}\n\nfunc run(pass *analysis.Pass) (interface{}, error) {\n\tinspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)\n\n\tnodeFilter := []ast.Node{\n\t\t(*ast.Ident)(nil),\n\t}\n\n\tinspect.Preorder(nodeFilter, func(n ast.Node) {\n\t\tswitch n := n.(type) {\n\t\tcase *ast.Ident:\n\t\t\tif n.Name == \"gopher\" {\n\t\t\t\tpass.Reportf(n.Pos(), \"identifier is gopher\")\n\t\t\t}\n\t\t}\n\t})\n\n\treturn nil, nil\n}\n\n-- @@.Pkg@@/@@.Pkg@@_test.go --\npackage @@.Pkg@@_test\n\nimport (\n\t\"testing\"\n\n\t\"@@.ImportPath@@\"\n\t\"golang.org/x/tools/go/analysis/analysistest\"\n)\n\n// TestAnalyzer is a test for Analyzer.\nfunc TestAnalyzer(t *testing.T) {\n\ttestdata := analysistest.TestData()\n\tanalysistest.Run(t, testdata, @@.Pkg@@.Analyzer, \"a\")\n}\n\n-- @@.Pkg@@/cmd/@@.Pkg@@/main.go --\n@@ if .Cmd -@@\npackage main\n\nimport (\n\t\"@@.ImportPath@@\"\n\t\"golang.org/x/tools/go/analysis/@@.Checker@@checker\"\n)\n\nfunc main() { @@.Checker@@checker.Main(@@.Pkg@@.Analyzer) }\n@@end@@\n-- @@.Pkg@@/plugin/main.go --\n@@ if .Plugin -@@\n// This file can build as a plugin for golangci-lint by below command.\n// go build -buildmode=plugin -o path_to_plugin_dir @@.ImportPath@@/plugin/@@.Pkg@@\n// See: https://golangci-lint.run/contributing/new-linters/#how-to-add-a-private-linter-to-golangci-lint\n\npackage main\n\nimport (\n\t\"strings\"\n\n\t\"@@.ImportPath@@\"\n\t\"golang.org/x/tools/go/analysis\"\n)\n\n// flags for Analyzer.Flag.\n// If you would like to specify flags for your plugin, you can put them via 'ldflags' as below.\n// $ go build -buildmode=plugin -ldflags \"-X 'main.flags=-opt val'\" @@.ImportPath@@/plugin/@@.Pkg@@\nvar flags string\n\n// AnalyzerPlugin provides analyzers as a plugin.\n// It follows golangci-lint style plugin.\nvar AnalyzerPlugin analyzerPlugin\n\ntype analyzerPlugin struct{}\n\nfunc (analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {\n\tif flags != \"\" {\n\t\tflagset := @@.Pkg@@.Analyzer.Flags\n\t\tif err := flagset.Parse(strings.Split(flags, \" \")); err != nil {\n\t\t\tpanic(\"cannot parse flags of @@.Pkg@@: \" + err.Error())\n\t\t}\n\t}\n\treturn []*analysis.Analyzer{\n\t\t@@.Pkg@@.Analyzer,\n\t}\n}\n@@end@@\n-- @@.Pkg@@/testdata/src/a/a.go --\npackage a\n\nfunc f() {\n\t// The pattern can be written in regular expression.\n\tvar gopher int // want \"pattern\"\n\tprint(gopher) // want \"identifier is gopher\"\n}\n\n")) +var tmpl = template.Must(template.New("template").Delims(`@@`, `@@`).Parse("-- @@.Pkg@@.go --\npackage @@.Pkg@@\n\nimport (\n\t\"go/ast\"\n\n\t\"golang.org/x/tools/go/analysis\"\n\t\"golang.org/x/tools/go/analysis/passes/inspect\"\n\t\"golang.org/x/tools/go/ast/inspector\"\n)\n\nconst doc = \"@@.Pkg@@ is ...\"\n\n// Analyzer is ...\nvar Analyzer = &analysis.Analyzer{\n\tName: \"@@.Pkg@@\",\n\tDoc: doc,\n\tRun: run,\n\tRequires: []*analysis.Analyzer{\n\t\tinspect.Analyzer,\n\t},\n}\n\nfunc run(pass *analysis.Pass) (interface{}, error) {\n\tinspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)\n\n\tnodeFilter := []ast.Node{\n\t\t(*ast.Ident)(nil),\n\t}\n\n\tinspect.Preorder(nodeFilter, func(n ast.Node) {\n\t\tswitch n := n.(type) {\n\t\tcase *ast.Ident:\n\t\t\tif n.Name == \"gopher\" {\n\t\t\t\tpass.Reportf(n.Pos(), \"identifier is gopher\")\n\t\t\t}\n\t\t}\n\t})\n\n\treturn nil, nil\n}\n\n-- @@.Pkg@@_test.go --\npackage @@.Pkg@@_test\n\nimport (\n\t\"testing\"\n\n\t\"@@.ImportPath@@\"\n\t\"golang.org/x/tools/go/analysis/analysistest\"\n)\n\n// TestAnalyzer is a test for Analyzer.\nfunc TestAnalyzer(t *testing.T) {\n\ttestdata := analysistest.TestData()\n\tanalysistest.Run(t, testdata, @@.Pkg@@.Analyzer, \"a\")\n}\n\n-- cmd/@@.Pkg@@/main.go --\n@@ if .Cmd -@@\npackage main\n\nimport (\n\t\"@@.ImportPath@@\"\n\t\"golang.org/x/tools/go/analysis/@@.Checker@@checker\"\n)\n\nfunc main() { @@.Checker@@checker.Main(@@.Pkg@@.Analyzer) }\n@@end@@\n-- plugin/main.go --\n@@ if .Plugin -@@\n// This file can build as a plugin for golangci-lint by below command.\n// go build -buildmode=plugin -o path_to_plugin_dir @@.ImportPath@@/plugin/@@.Pkg@@\n// See: https://golangci-lint.run/contributing/new-linters/#how-to-add-a-private-linter-to-golangci-lint\n\npackage main\n\nimport (\n\t\"strings\"\n\n\t\"@@.ImportPath@@\"\n\t\"golang.org/x/tools/go/analysis\"\n)\n\n// flags for Analyzer.Flag.\n// If you would like to specify flags for your plugin, you can put them via 'ldflags' as below.\n// $ go build -buildmode=plugin -ldflags \"-X 'main.flags=-opt val'\" @@.ImportPath@@/plugin/@@.Pkg@@\nvar flags string\n\n// AnalyzerPlugin provides analyzers as a plugin.\n// It follows golangci-lint style plugin.\nvar AnalyzerPlugin analyzerPlugin\n\ntype analyzerPlugin struct{}\n\nfunc (analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {\n\tif flags != \"\" {\n\t\tflagset := @@.Pkg@@.Analyzer.Flags\n\t\tif err := flagset.Parse(strings.Split(flags, \" \")); err != nil {\n\t\t\tpanic(\"cannot parse flags of @@.Pkg@@: \" + err.Error())\n\t\t}\n\t}\n\treturn []*analysis.Analyzer{\n\t\t@@.Pkg@@.Analyzer,\n\t}\n}\n@@end@@\n-- testdata/src/a/a.go --\npackage a\n\nfunc f() {\n\t// The pattern can be written in regular expression.\n\tvar gopher int // want \"pattern\"\n\tprint(gopher) // want \"identifier is gopher\"\n}\n\n")) diff --git a/tools/txtar/main.go b/tools/txtar/main.go index 41cfd6c..cc2fd01 100644 --- a/tools/txtar/main.go +++ b/tools/txtar/main.go @@ -57,7 +57,7 @@ func main() { p := filepath.ToSlash(path) ar.Files = append(ar.Files, txtar.File{ - Name: strings.TrimLeft(p, *flagStripPrefix), + Name: strings.TrimPrefix(p, *flagStripPrefix), Data: data, })