package main import ( "encoding/json" "fmt" "io/ioutil" "log/syslog" "os" "path" ) // config represents the configuration for the gerrit hook type config struct { GerritUrl string `json:"gerritUrl"` GerritUser string `json:"gerritUser"` GerritPassword string `json:"gerritPassword"` BuildKiteToken string `json:"buildKiteToken"` BuildKiteOrganization string `json:"buildKiteOrganization"` } func loadConfig() (*config, error) { configPath := "/var/run/agenix/gerrit/hooks" configJson, err := ioutil.ReadFile(configPath) if err != nil { return nil, fmt.Errorf("failed to read configuration file %s: %v", configPath, err) } var cfg config err = json.Unmarshal(configJson, &cfg) if err != nil { return nil, fmt.Errorf("failed to unmarshall configuration: %v", err) } return &cfg, nil } func main() { log, err := syslog.New(syslog.LOG_INFO|syslog.LOG_USER, "gerrit-hook") if err != nil { fmt.Fprintf(os.Stderr, "failed to open syslog: %s\n", err) } log.Info(fmt.Sprintf("`gerrit-hook' called with arguments: %v\n", os.Args)) cmd := path.Base(os.Args[0]) cfg, err := loadConfig() if err != nil { os.Exit(1) } if cmd == "patchset-created" { trigger, err := triggerForPatchsetCreated() if err != nil { log.Crit(fmt.Sprintf("failed to create a trigger: %s", err)) os.Exit(1) } gerritHookMain(cfg, log, trigger) } else if cmd == "post-command" { postCommand(cfg) } else { log.Info(fmt.Sprintf("`%s' is not a supported command", cmd)) os.Exit(1) } }