2020-01-31 23:58:10 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2020-02-01 00:59:45 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
2020-01-31 23:58:10 +00:00
|
|
|
"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 (
|
2020-02-01 01:05:29 +00:00
|
|
|
flagPush bool
|
|
|
|
flagMoveToMaster bool
|
2020-01-31 23:58:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(exportCmd)
|
|
|
|
|
|
|
|
exportCmd.Flags().BoolVar(&flagPush, "push", false, "push to public repo")
|
2020-02-01 01:05:29 +00:00
|
|
|
exportCmd.Flags().BoolVar(&flagMoveToMaster, "move-to-master", true, "move to master after completing export")
|
2020-01-31 23:58:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-02-01 00:59:45 +00:00
|
|
|
random := make([]byte, 4)
|
|
|
|
if _, err := io.ReadFull(rand.Reader, random); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-02-01 01:02:12 +00:00
|
|
|
tmpBranch := "temp_" + hex.EncodeToString(random)
|
2020-02-01 00:59:45 +00:00
|
|
|
|
|
|
|
git("checkout", "-b", tmpBranch)
|
2020-01-31 23:58:10 +00:00
|
|
|
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")
|
2020-02-01 00:59:45 +00:00
|
|
|
git("branch", "-D", tmpBranch)
|
2020-01-31 23:58:10 +00:00
|
|
|
|
|
|
|
git("add", "-A")
|
|
|
|
git("commit", "-m", "export public files", "--allow-empty")
|
|
|
|
|
|
|
|
if flagPush {
|
|
|
|
git("push", "public", "public:master")
|
|
|
|
}
|
|
|
|
|
2020-02-01 01:05:29 +00:00
|
|
|
if flagMoveToMaster {
|
|
|
|
git("checkout", "master")
|
|
|
|
}
|
2020-01-31 23:58:10 +00:00
|
|
|
}
|