|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/go-git/go-git/v5" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "go.uber.org/zap" |
| 12 | + "go.uber.org/zap/zapcore" |
| 13 | + |
| 14 | + "github.com/didactiklabs/ginx/internal/utils" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + versionFlag bool |
| 19 | + version string |
| 20 | + logLevelFlag string |
| 21 | + sourceFlag string |
| 22 | + branchFlag string |
| 23 | + pollIntervalFlag int |
| 24 | +) |
| 25 | + |
| 26 | +var RootCmd = &cobra.Command{ |
| 27 | + Use: "ginx [flags] -- <command>", |
| 28 | + Short: "ginx", |
| 29 | + Long: ` |
| 30 | +Ginx is a cli tool that watch a remote repository and run an arbitrary command on changes/updates. |
| 31 | +`, |
| 32 | + PersistentPreRun: func(cmd *cobra.Command, args []string) { |
| 33 | + // Initialize configuration here |
| 34 | + if versionFlag { |
| 35 | + fmt.Printf("%s", version) |
| 36 | + } else { |
| 37 | + initConfig() |
| 38 | + } |
| 39 | + }, |
| 40 | + Run: func(cmd *cobra.Command, args []string) { |
| 41 | + var r *git.Repository |
| 42 | + var err error |
| 43 | + source := sourceFlag |
| 44 | + branch := branchFlag |
| 45 | + interval := time.Duration(pollIntervalFlag) * time.Second |
| 46 | + dir, err := os.MkdirTemp("", "ginx-*") |
| 47 | + if err != nil { |
| 48 | + utils.Logger.Fatal("Failed to create temporary directory.", zap.Error(err)) |
| 49 | + } |
| 50 | + |
| 51 | + if !utils.IsRepoCloned(source) { |
| 52 | + utils.Logger.Info("Cloning repository.", zap.String("url", source), zap.String("branch", branch)) |
| 53 | + r, err = utils.CloneRepo(source, branch, dir) |
| 54 | + if err != nil { |
| 55 | + utils.Logger.Fatal("Failed to clone repository.", zap.Error(err)) |
| 56 | + } |
| 57 | + } else { |
| 58 | + r, err = git.PlainOpen(dir) |
| 59 | + utils.Logger.Info("Repository already exist, open directory repository.", zap.String("directory", dir)) |
| 60 | + if err != nil { |
| 61 | + utils.Logger.Fatal("Failed to open existing directory repository.", zap.Error(err)) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + log.Println("Starting remote repository watcher...") |
| 66 | + for { |
| 67 | + // Get the latest commit hash from the remote repository |
| 68 | + remoteCommit, err := utils.GetLatestRemoteCommit(r, branch) |
| 69 | + utils.Logger.Debug("Fetched remote commit.", zap.String("remoteCommit", remoteCommit)) |
| 70 | + if err != nil { |
| 71 | + utils.Logger.Fatal("error fetching local commit.", zap.Error(err)) |
| 72 | + } |
| 73 | + |
| 74 | + // Get the latest commit hash from the local repository |
| 75 | + localCommit, err := utils.GetLatestLocalCommit(dir) |
| 76 | + utils.Logger.Debug("Fetched local commit.", zap.String("localCommit", localCommit)) |
| 77 | + if err != nil { |
| 78 | + utils.Logger.Fatal("error fetching local commit.", zap.Error(err)) |
| 79 | + } |
| 80 | + |
| 81 | + if remoteCommit != localCommit { |
| 82 | + log.Println("Detected remote changes. Pulling for latest changes...") |
| 83 | + if err := utils.PullRepo(r); err != nil { |
| 84 | + utils.Logger.Info("Failed to pull. Recloning repository.", zap.String("url", source)) |
| 85 | + err := os.RemoveAll(dir) |
| 86 | + if err != nil { |
| 87 | + utils.Logger.Fatal("error removing directory.", zap.Error(err)) |
| 88 | + } |
| 89 | + _, err = utils.CloneRepo(source, branch, dir) |
| 90 | + if err != nil { |
| 91 | + utils.Logger.Fatal("Failed to clone repository.", zap.Error(err)) |
| 92 | + } |
| 93 | + } |
| 94 | + if len(args) > 0 { |
| 95 | + utils.Logger.Info("Running command.", zap.String("command", args[0]), zap.Any("args", args[1:])) |
| 96 | + if err := utils.RunCommand(dir, args[0], args[1:]...); err != nil { |
| 97 | + utils.Logger.Error("Failed to run command.", zap.Error(err)) |
| 98 | + } |
| 99 | + } |
| 100 | + } else { |
| 101 | + utils.Logger.Info("No changes detected in remote repository.", zap.String("url", source)) |
| 102 | + } |
| 103 | + time.Sleep(interval) |
| 104 | + } |
| 105 | + }, |
| 106 | + Args: cobra.ArbitraryArgs, |
| 107 | +} |
| 108 | + |
| 109 | +func initConfig() { |
| 110 | + // Your configuration initialization logic |
| 111 | + logLevel := zapcore.InfoLevel //nolint:all |
| 112 | + switch logLevelFlag { |
| 113 | + case "debug": |
| 114 | + logLevel = zapcore.DebugLevel |
| 115 | + case "error": |
| 116 | + logLevel = zapcore.ErrorLevel |
| 117 | + default: |
| 118 | + logLevel = zapcore.InfoLevel |
| 119 | + } |
| 120 | + utils.InitializeLogger(logLevel) |
| 121 | + utils.Logger.Info("Initialized configuration.") |
| 122 | +} |
| 123 | + |
| 124 | +func Execute() { |
| 125 | + err := RootCmd.Execute() |
| 126 | + if err != nil { |
| 127 | + os.Exit(1) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func init() { |
| 132 | + RootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "display version information") |
| 133 | + RootCmd.PersistentFlags().StringVarP(&logLevelFlag, "log-level", "l", "info", "override log level (debug, info, error)") |
| 134 | + RootCmd.PersistentFlags().StringVarP(&sourceFlag, "source", "s", "", "git repository to watch") |
| 135 | + RootCmd.PersistentFlags().StringVarP(&branchFlag, "branch", "b", "main", "branch to watch") |
| 136 | + RootCmd.PersistentFlags().IntVarP(&pollIntervalFlag, "interval", "n", 30, "interval in seconds to poll the remote repo") |
| 137 | +} |
0 commit comments