🚨 go fmt; go mod tidy

This commit is contained in:
Brian Wiborg 2024-05-13 10:45:38 +02:00
parent fb2a29be51
commit b0657a3fb2
No known key found for this signature in database
GPG key ID: BE53FA9286B719D6
20 changed files with 715 additions and 722 deletions

View file

@ -5,12 +5,12 @@ import (
)
var App = cli.App{
Name: "govote",
Usage: "🌈 Referendums and concensus.",
Commands: []*cli.Command{
newCmd,
showCmd,
voteCmd,
serveCmd,
},
Name: "govote",
Usage: "🌈 Referendums and concensus.",
Commands: []*cli.Command{
newCmd,
showCmd,
voteCmd,
serveCmd,
},
}

View file

@ -16,93 +16,93 @@ import (
)
var newCmd = &cli.Command{
Name: "new",
Usage: " Create a voting",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "deadline",
Usage: "Duration for which this voting is open",
Aliases: []string{"D"},
Value: "1m",
},
&cli.StringFlag{
Name: "quorum",
Usage: "Minimum required number of participants",
Aliases: []string{"Q"},
Value: "SIMPLE",
},
&cli.StringFlag{
Name: "threshold",
Usage: "Minimum number of positive votes",
Aliases: []string{"T"},
Value: "SIMPLE",
},
&cli.StringFlag{
Name: "electors",
Usage: "Comma-separated list of eligible electors or empty if anyone can vote",
Aliases: []string{"E"},
},
&cli.BoolFlag{
Name: "anonymous",
Usage: "Public visibility of votes.",
Aliases: []string{"A"},
Value: false,
},
},
Action: func(ctx *cli.Context) error {
deadline := ctx.String("deadline")
deadlineNum := fmt.Sprintf("%s", deadline[:len(deadline)-1])
deadlineUnit := fmt.Sprintf("%s", deadline[len(deadline)-1:])
Name: "new",
Usage: " Create a voting",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "deadline",
Usage: "Duration for which this voting is open",
Aliases: []string{"D"},
Value: "1m",
},
&cli.StringFlag{
Name: "quorum",
Usage: "Minimum required number of participants",
Aliases: []string{"Q"},
Value: "SIMPLE",
},
&cli.StringFlag{
Name: "threshold",
Usage: "Minimum number of positive votes",
Aliases: []string{"T"},
Value: "SIMPLE",
},
&cli.StringFlag{
Name: "electors",
Usage: "Comma-separated list of eligible electors or empty if anyone can vote",
Aliases: []string{"E"},
},
&cli.BoolFlag{
Name: "anonymous",
Usage: "Public visibility of votes.",
Aliases: []string{"A"},
Value: false,
},
},
Action: func(ctx *cli.Context) error {
deadline := ctx.String("deadline")
deadlineNum := fmt.Sprintf("%s", deadline[:len(deadline)-1])
deadlineUnit := fmt.Sprintf("%s", deadline[len(deadline)-1:])
deadlineInt, err := strconv.Atoi(deadlineNum)
if err != nil {
return err
}
deadlineInt, err := strconv.Atoi(deadlineNum)
if err != nil {
return err
}
if !strings.Contains("mhd", deadlineUnit) {
return fmt.Errorf("invalid deadline unit '%s'. use one of: [ m | d | h ]", deadlineUnit)
}
if !strings.Contains("mhd", deadlineUnit) {
return fmt.Errorf("invalid deadline unit '%s'. use one of: [ m | d | h ]", deadlineUnit)
}
var d time.Time
switch deadlineUnit {
case "m", "":
d = time.Now().UTC().Add(time.Duration(deadlineInt)*time.Minute).Round(time.Second)
case "h":
d = time.Now().UTC().Add(time.Duration(deadlineInt)*time.Hour).Round(time.Second)
case "d":
d = time.Now().UTC().Add(time.Duration(deadlineInt)*time.Hour*24).Round(time.Second)
default:
panic("this code should never be reached")
}
var d time.Time
switch deadlineUnit {
case "m", "":
d = time.Now().UTC().Add(time.Duration(deadlineInt) * time.Minute).Round(time.Second)
case "h":
d = time.Now().UTC().Add(time.Duration(deadlineInt) * time.Hour).Round(time.Second)
case "d":
d = time.Now().UTC().Add(time.Duration(deadlineInt) * time.Hour * 24).Round(time.Second)
default:
panic("this code should never be reached")
}
var (
q quorum.Quorum
t threshold.Threshold
)
if q, err = quorum.FromString(ctx.String("quorum")); err != nil {
return err
}
if t, err = threshold.FromString(ctx.String("threshold")); err != nil {
return err
}
e := strings.Split(ctx.String("electors"), " ")
a := ctx.Bool("anonymous")
var (
q quorum.Quorum
t threshold.Threshold
)
if q, err = quorum.FromString(ctx.String("quorum")); err != nil {
return err
}
if t, err = threshold.FromString(ctx.String("threshold")); err != nil {
return err
}
e := strings.Split(ctx.String("electors"), " ")
a := ctx.Bool("anonymous")
var r = ""
if ctx.Args().Len() == 0 {
fmt.Print("Give your referendum a concise name or subject: ")
inputReader := bufio.NewReader(os.Stdin)
r, _ = inputReader.ReadString('\n')
r = r[:len(r)-1]
} else {
r = strings.Join(ctx.Args().Slice(), " ")
}
var r = ""
if ctx.Args().Len() == 0 {
fmt.Print("Give your referendum a concise name or subject: ")
inputReader := bufio.NewReader(os.Stdin)
r, _ = inputReader.ReadString('\n')
r = r[:len(r)-1]
} else {
r = strings.Join(ctx.Args().Slice(), " ")
}
id := utils.GenerateRandomString(11)
if err := store.NewVoting(string(id), r, d, q, t, e, a); err != nil {
return err
}
fmt.Println(string(id))
return nil
},
id := utils.GenerateRandomString(11)
if err := store.NewVoting(string(id), r, d, q, t, e, a); err != nil {
return err
}
fmt.Println(string(id))
return nil
},
}

View file

@ -6,16 +6,16 @@ import (
)
var serveCmd = &cli.Command{
Name: "serve",
Usage: "Start the HTTP server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "bind-address",
Usage: "The TCP address:port to bind to",
Value: ":3000",
},
},
Action: func(ctx *cli.Context) error {
return http.Serve(ctx.String("bind-address"))
},
Name: "serve",
Usage: "Start the HTTP server",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "bind-address",
Usage: "The TCP address:port to bind to",
Value: ":3000",
},
},
Action: func(ctx *cli.Context) error {
return http.Serve(ctx.String("bind-address"))
},
}

View file

@ -10,25 +10,25 @@ import (
)
var showCmd = &cli.Command{
Name: "show",
Usage: "📈 Display a voting",
Action: func(ctx *cli.Context) error {
var r string
if ctx.Args().Len() == 0 {
inputReader := bufio.NewReader(os.Stdin)
r, _ = inputReader.ReadString('\n')
r = r[:len(r)-1]
}
Name: "show",
Usage: "📈 Display a voting",
Action: func(ctx *cli.Context) error {
var r string
if ctx.Args().Len() == 0 {
inputReader := bufio.NewReader(os.Stdin)
r, _ = inputReader.ReadString('\n')
r = r[:len(r)-1]
}
id := ctx.Args().Get(0)
if id == "" {
return fmt.Errorf("Please provide an ID!")
}
voting, err := store.GetVoting(id)
if err != nil {
return err
}
fmt.Println(voting)
return nil
},
id := ctx.Args().Get(0)
if id == "" {
return fmt.Errorf("Please provide an ID!")
}
voting, err := store.GetVoting(id)
if err != nil {
return err
}
fmt.Println(voting)
return nil
},
}

View file

@ -8,39 +8,39 @@ import (
)
var voteCmd = &cli.Command{
Name: "vote",
Usage: "📄 Place vote",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "voting-id",
Usage: "Voting ID",
Aliases: []string{"V"},
Required: true,
},
&cli.StringFlag{
Name: "elector",
Usage: "Elector",
Aliases: []string{"E"},
Required: true,
},
&cli.StringFlag{
Name: "choice",
Usage: "Choice",
Aliases: []string{"C"},
Required: true,
},
},
Action: func(ctx *cli.Context) error {
var (
id = uuid.New().String()
votingID = ctx.String("voting-id")
elector = ctx.String("elector")
)
choice, err := vote.ChoiceFromString(ctx.String("choice"))
if err != nil {
return err
}
err = store.PlaceVote(id, votingID, elector, choice)
return err
},
Name: "vote",
Usage: "📄 Place vote",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "voting-id",
Usage: "Voting ID",
Aliases: []string{"V"},
Required: true,
},
&cli.StringFlag{
Name: "elector",
Usage: "Elector",
Aliases: []string{"E"},
Required: true,
},
&cli.StringFlag{
Name: "choice",
Usage: "Choice",
Aliases: []string{"C"},
Required: true,
},
},
Action: func(ctx *cli.Context) error {
var (
id = uuid.New().String()
votingID = ctx.String("voting-id")
elector = ctx.String("elector")
)
choice, err := vote.ChoiceFromString(ctx.String("choice"))
if err != nil {
return err
}
err = store.PlaceVote(id, votingID, elector, choice)
return err
},
}