Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4.2.2
with:
fetch-depth: 2
- uses: actions/setup-go@v3
- uses: actions/setup-go@v5.4.0
with:
go-version: '1.21'
go-version: '1.25'
- name: Run coverage
run: go test -coverpkg=./... ./... -race -coverprofile=coverage.out -covermode=atomic
run: go env -w GOTOOLCHAIN=go1.25.0+auto && go test -coverpkg=./... ./... -race -coverprofile=coverage.out -covermode=atomic
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v5
with:
verbose: true
verbose: true
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
74 changes: 0 additions & 74 deletions .github/workflows/codeql.yml

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4.2.2

- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5.4.0
with:
go-version: '1.21'
go-version: '1.25'

- name: Build
run: go build -v ./...
Expand Down
30 changes: 12 additions & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ var cfgFile string
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "ballot",
Short: "A brief description of your application",
Long: `A longer description that spans multiple lines and likely contains
examples and usage of using your application. For example:

Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
// Uncomment the following line if your bare application
// has an action associated with it:
// Run: func(cmd *cobra.Command, args []string) { },
Short: "Consul-based leader election with tagging support",
Long: `Ballot is a distributed leader election tool built on Consul.

It uses Consul's session and KV store APIs to perform leader election
among service instances. When a service becomes leader, it is tagged
with a configurable primary tag and optional hooks can be executed.

Features:
- Consul session-based leader election
- Automatic primary tag management
- Configurable promotion/demotion hooks
- Health check aware leadership`,
}

// Execute adds all child commands to the root command and sets flags appropriately.
Expand All @@ -52,15 +54,7 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.

rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.ballot.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

// initConfig reads in config file and ENV variables if set.
Expand Down
39 changes: 28 additions & 11 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package cmd
import (
"context"
"os"
"os/signal"
"sync"
"syscall"

"github.com/ncode/ballot/internal/ballot"
log "github.com/sirupsen/logrus"
Expand All @@ -30,25 +33,39 @@ var runCmd = &cobra.Command{
Use: "run",
Short: "Run the ballot and starts all the defined elections",
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
for _, name := range viper.GetStringSlice("election.enabled") {
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer cancel()

var wg sync.WaitGroup
enabledServices := viper.GetStringSlice("election.enabled")

for _, name := range enabledServices {
b, err := ballot.New(ctx, name)
if err != nil {
log.WithFields(log.Fields{
"caller": "run",
"step": "New",
"caller": "run",
"step": "New",
"service": name,
}).Error(err)
os.Exit(1)
}

err = b.Run()
if err != nil {
log.WithFields(log.Fields{
"caller": "run",
"step": "runCmd",
}).Error(err)
}
wg.Add(1)
go func(b *ballot.Ballot, name string) {
defer wg.Done()
err := b.Run()
if err != nil {
log.WithFields(log.Fields{
"caller": "run",
"step": "runCmd",
"service": name,
}).Error(err)
}
}(b, name)
}

wg.Wait()
log.Info("All elections stopped, shutting down")
},
}

Expand Down
50 changes: 22 additions & 28 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,55 +1,49 @@
module github.com/ncode/ballot

go 1.21
toolchain go1.23.7
go 1.25.5

require (
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/hashicorp/consul/api v1.27.0
github.com/hashicorp/consul/api v1.33.0
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
github.com/spf13/cobra v1.10.2
github.com/spf13/viper v1.21.0
)

require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/hashicorp/go-metrics v0.5.4 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/stretchr/objx v0.5.0 // indirect
golang.org/x/net v0.36.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/exp v0.0.0-20251219203646-944ab1f22d93 // indirect
)

require (
github.com/armon/go-metrics v0.4.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/fatih/color v1.18.0 // indirect
github.com/fsnotify/fsnotify v1.9.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-hclog v1.6.2 // indirect
github.com/hashicorp/go-hclog v1.6.3 // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/hashicorp/serf v0.10.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.4
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/sagikazarmark/locafero v0.12.0 // indirect
github.com/spf13/afero v1.15.0 // indirect
github.com/spf13/cast v1.10.0 // indirect
github.com/spf13/pflag v1.0.10 // indirect
github.com/stretchr/testify v1.11.1
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.30.0 // indirect
golang.org/x/text v0.22.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
golang.org/x/sys v0.39.0 // indirect
golang.org/x/text v0.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading