Working on export tool
This commit is contained in:
parent
ba90f2f10e
commit
f2da93215a
11 changed files with 176 additions and 33 deletions
63
tools/testtool/commands/export.go
Normal file
63
tools/testtool/commands/export.go
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
var exportCmd = &cobra.Command{
|
||||||
|
Use: "export",
|
||||||
|
Short: "export source code to public repo",
|
||||||
|
Run: exportCode,
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
flagPush bool
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(exportCmd)
|
||||||
|
|
||||||
|
exportCmd.Flags().BoolVar(&flagPush, "push", false, "push to public repo")
|
||||||
|
}
|
||||||
|
|
||||||
|
func git(args ...string) {
|
||||||
|
log.Println("git", strings.Join(args, " "))
|
||||||
|
|
||||||
|
cmd := exec.Command("git", args...)
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func exportCode(cmd *cobra.Command, args []string) {
|
||||||
|
git("checkout", "-b", "temp")
|
||||||
|
git("reset", "public")
|
||||||
|
|
||||||
|
privateFiles := listPrivateFiles(".")
|
||||||
|
for _, f := range privateFiles {
|
||||||
|
log.Printf("rm %s", f)
|
||||||
|
if err := os.Remove(f); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
git("checkout", "public")
|
||||||
|
git("branch", "-D", "temp")
|
||||||
|
|
||||||
|
git("add", "-A")
|
||||||
|
git("commit", "-m", "export public files", "--allow-empty")
|
||||||
|
|
||||||
|
if flagPush {
|
||||||
|
git("push", "public", "public:master")
|
||||||
|
}
|
||||||
|
|
||||||
|
git("checkout", "master")
|
||||||
|
}
|
55
tools/testtool/commands/list.go
Normal file
55
tools/testtool/commands/list.go
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// List all _test.go files in given directory including the ones with "private" build tag.
|
||||||
|
//
|
||||||
|
// Returns absolute paths.
|
||||||
|
func listTestFiles(rootPackage string) []string {
|
||||||
|
files := getPackageFiles(rootPackage, []string{"-tags", "private"})
|
||||||
|
var tests []string
|
||||||
|
for f := range files {
|
||||||
|
if strings.HasSuffix(f, "_test.go") {
|
||||||
|
tests = append(tests, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(tests)
|
||||||
|
return tests
|
||||||
|
}
|
||||||
|
|
||||||
|
// List all .go source files in given directory protected by "!change" build tag.
|
||||||
|
//
|
||||||
|
// Returns absolute paths.
|
||||||
|
func listProtectedFiles(rootPackage string) []string {
|
||||||
|
allFiles := getPackageFiles(rootPackage, nil)
|
||||||
|
allFilesWithoutProtected := getPackageFiles(rootPackage, []string{"-tags", "change"})
|
||||||
|
|
||||||
|
var protectedFiles []string
|
||||||
|
for f := range allFiles {
|
||||||
|
if _, ok := allFilesWithoutProtected[f]; !ok {
|
||||||
|
protectedFiles = append(protectedFiles, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(protectedFiles)
|
||||||
|
return protectedFiles
|
||||||
|
}
|
||||||
|
|
||||||
|
func listPrivateFiles(rootPackage string) []string {
|
||||||
|
allFiles := getPackageFiles(rootPackage, []string{})
|
||||||
|
allWithPrivate := getPackageFiles(rootPackage, []string{"-tags", "private,solution"})
|
||||||
|
|
||||||
|
var files []string
|
||||||
|
for f := range allWithPrivate {
|
||||||
|
if _, isPublic := allFiles[f]; !isPublic {
|
||||||
|
files = append(files, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Strings(files)
|
||||||
|
return files
|
||||||
|
}
|
35
tools/testtool/commands/list_test.go
Normal file
35
tools/testtool/commands/list_test.go
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func absPaths(files []string ) []string {
|
||||||
|
var abs []string
|
||||||
|
for _, f := range files {
|
||||||
|
absPath, _ := filepath.Abs("../testdata/list/" + f)
|
||||||
|
abs = append(abs, absPath)
|
||||||
|
}
|
||||||
|
return abs
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestListTestFiles(t *testing.T) {
|
||||||
|
require.Equal(t,
|
||||||
|
absPaths([]string{"sum/private_test.go", "sum/public_test.go",}),
|
||||||
|
listTestFiles("../testdata/list"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestProtectedFiles(t *testing.T) {
|
||||||
|
require.Equal(t,
|
||||||
|
absPaths([]string{"sum/dontchange.go"}),
|
||||||
|
listProtectedFiles("../testdata/list"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrivateFiles(t *testing.T) {
|
||||||
|
require.Equal(t,
|
||||||
|
absPaths([]string{"sum/solution.go.go", "sum/private_test.go"}),
|
||||||
|
listProtectedFiles("../testdata/list"))
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
@ -14,11 +15,16 @@ var rootCmd = &cobra.Command{
|
||||||
TraverseChildren: true,
|
TraverseChildren: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.SetPrefix("testtool: ")
|
||||||
|
log.SetFlags(0)
|
||||||
|
}
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||||
func Execute() {
|
func Execute() {
|
||||||
if err := rootCmd.Execute(); err != nil {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
fmt.Println(err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"golang.org/x/tools/go/packages"
|
"golang.org/x/tools/go/packages"
|
||||||
|
@ -146,37 +145,6 @@ func copyContents(src, dst string) {
|
||||||
copyDir(src+"/", dst)
|
copyDir(src+"/", dst)
|
||||||
}
|
}
|
||||||
|
|
||||||
// List all _test.go files in given directory including the ones with "private" build tag.
|
|
||||||
//
|
|
||||||
// Returns absolute paths.
|
|
||||||
func listTestFiles(problemDir string) []string {
|
|
||||||
files := getPackageFiles(problemDir, []string{"-tags", "private"})
|
|
||||||
var tests []string
|
|
||||||
for f := range files {
|
|
||||||
if strings.HasSuffix(f, "_test.go") {
|
|
||||||
tests = append(tests, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tests
|
|
||||||
}
|
|
||||||
|
|
||||||
// List all .go source files in given directory protected by "!change" build tag.
|
|
||||||
//
|
|
||||||
// Returns absolute paths.
|
|
||||||
func listProtectedFiles(problemDir string) []string {
|
|
||||||
allFiles := getPackageFiles(problemDir, nil)
|
|
||||||
allFilesWithoutProtected := getPackageFiles(problemDir, []string{"-tags", "change"})
|
|
||||||
|
|
||||||
var protectedFiles []string
|
|
||||||
for f := range allFiles {
|
|
||||||
if _, ok := allFilesWithoutProtected[f]; !ok {
|
|
||||||
protectedFiles = append(protectedFiles, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return protectedFiles
|
|
||||||
}
|
|
||||||
|
|
||||||
// Copy files preserving directory structure relative to baseDir.
|
// Copy files preserving directory structure relative to baseDir.
|
||||||
//
|
//
|
||||||
// Existing files get replaced.
|
// Existing files get replaced.
|
||||||
|
|
3
tools/testtool/testdata/list/go.mod
vendored
Normal file
3
tools/testtool/testdata/list/go.mod
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
module gitlab.com/slon/shad-go
|
||||||
|
|
||||||
|
go 1.13
|
3
tools/testtool/testdata/list/sum/dontchange.go
vendored
Normal file
3
tools/testtool/testdata/list/sum/dontchange.go
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
// +build !change
|
||||||
|
|
||||||
|
package sum
|
3
tools/testtool/testdata/list/sum/private_test.go
vendored
Normal file
3
tools/testtool/testdata/list/sum/private_test.go
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
// +build private
|
||||||
|
|
||||||
|
package sum
|
3
tools/testtool/testdata/list/sum/public.go
vendored
Normal file
3
tools/testtool/testdata/list/sum/public.go
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
// +build !solution
|
||||||
|
|
||||||
|
package sum
|
1
tools/testtool/testdata/list/sum/public_test.go
vendored
Normal file
1
tools/testtool/testdata/list/sum/public_test.go
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
package sum
|
3
tools/testtool/testdata/list/sum/solution.go
vendored
Normal file
3
tools/testtool/testdata/list/sum/solution.go
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
// +build solution
|
||||||
|
|
||||||
|
package sum
|
Loading…
Reference in a new issue