You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.4 KiB

2 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "path"
  6. _ "embed"
  7. "github.com/spf13/cobra"
  8. )
  9. //go:embed templates/template.go
  10. var template []byte
  11. func main() {
  12. root := &cobra.Command{};
  13. servecmd := &cobra.Command{
  14. Use: "serve",
  15. Short: "server to listen for competitive companion requests",
  16. Run: func(cmd *cobra.Command, args []string) {
  17. port, _ := cmd.Flags().GetInt("port");
  18. fmt.Println("Listning on port", port, "...")
  19. server(port);
  20. },
  21. }
  22. servecmd.Flags().IntP("port", "P", 10046, "port to listen to")
  23. initcmd := &cobra.Command {
  24. Use: "init",
  25. Short: "initialize directories",
  26. Args: cobra.MinimumNArgs(1),
  27. Run: func(cmd *cobra.Command, args []string) {
  28. for _,folder := range args {
  29. err := os.MkdirAll(folder, 0755); if err != nil {
  30. fmt.Println(err);
  31. continue
  32. }
  33. mainfile := path.Join(folder, "main.go");
  34. _, err = os.Stat(mainfile); if !os.IsNotExist(err) {
  35. fmt.Println(mainfile, "already exists.")
  36. continue
  37. }
  38. os.WriteFile(path.Join(folder, "main.go"), template, 0644);
  39. }
  40. },
  41. }
  42. testcmd := &cobra.Command{
  43. Use: "test",
  44. Short: "test code",
  45. Run: func(cmd *cobra.Command, args []string) {
  46. dir, _ := os.Getwd();
  47. tests, _ := cmd.Flags().GetStringArray("tests");
  48. runTests(dir, tests)
  49. },
  50. }
  51. testcmd.Flags().StringArrayP("tests", "T", []string{}, "Tests to execute (default is everything)")
  52. root.AddCommand(servecmd, initcmd, testcmd);
  53. root.Execute();
  54. }