main.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "sigs.k8s.io/yaml"
  9. "github.com/mattermost/mattermost-plugin-starter-template/build/sync/plan"
  10. )
  11. func main() {
  12. verbose := flag.Bool("verbose", false, "enable verbose output")
  13. flag.Usage = func() {
  14. fmt.Fprintf(flag.CommandLine.Output(), "Update a pluging directory with /mattermost-plugin-starter-template/.\n")
  15. fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
  16. fmt.Fprintf(flag.CommandLine.Output(), "%s <plan.yml> <plugin_directory>\n", os.Args[0])
  17. flag.PrintDefaults()
  18. }
  19. flag.Parse()
  20. // TODO: implement proper command line parameter parsing.
  21. if len(os.Args) != 3 {
  22. fmt.Fprintf(os.Stderr, "running: \n $ sync [plan.yaml] [plugin path]\n")
  23. os.Exit(1)
  24. }
  25. syncPlan, err := readPlan(os.Args[1])
  26. if err != nil {
  27. fmt.Fprintf(os.Stderr, "coud not load plan: %s\n", err)
  28. os.Exit(1)
  29. }
  30. srcDir, err := os.Getwd()
  31. if err != nil {
  32. fmt.Fprintf(os.Stderr, "failed to get current directory: %s\n", err)
  33. os.Exit(1)
  34. }
  35. trgDir, err := filepath.Abs(os.Args[2])
  36. if err != nil {
  37. fmt.Fprintf(os.Stderr, "could not determine target directory: %s\n", err)
  38. os.Exit(1)
  39. }
  40. srcRepo, err := plan.GetRepoSetup(srcDir)
  41. if err != nil {
  42. fmt.Fprintf(os.Stderr, "%s\n", err)
  43. os.Exit(1)
  44. }
  45. trgRepo, err := plan.GetRepoSetup(trgDir)
  46. if err != nil {
  47. fmt.Fprintf(os.Stderr, "%s\n", err)
  48. os.Exit(1)
  49. }
  50. planSetup := plan.Setup{
  51. Source: srcRepo,
  52. Target: trgRepo,
  53. VerboseLogging: *verbose,
  54. }
  55. err = syncPlan.Execute(planSetup)
  56. if err != nil {
  57. fmt.Fprintf(os.Stderr, "%s\n", err)
  58. os.Exit(1)
  59. }
  60. }
  61. func readPlan(path string) (*plan.Plan, error) {
  62. raw, err := ioutil.ReadFile(path)
  63. if err != nil {
  64. return nil, fmt.Errorf("failed to read plan file %q: %v", path, err)
  65. }
  66. var p plan.Plan
  67. err = yaml.Unmarshal(raw, &p)
  68. if err != nil {
  69. return nil, fmt.Errorf("failed to unmarshal plan yaml: %v", err)
  70. }
  71. return &p, err
  72. }