Compare commits

..

2 Commits

Author SHA1 Message Date
vilmibm 122af44a9e upgrade to go 1.18 2022-07-31 13:43:40 +00:00
vilmibm a2e5c81752 WIP working on contrib 2022-07-31 13:43:29 +00:00
2 changed files with 116 additions and 5 deletions

View File

@ -1,10 +1,121 @@
package main package main
import "fmt" import (
"errors"
"fmt"
"github.com/spf13/cobra"
"os"
)
const dbpath = "/town/var/commands.db" const (
submitOp = "submit"
delOp = "del"
updateOp = "update"
dbpath = "/town/var/commands.db"
)
type ContribOptions struct {
CmdName string
Operation string // submit, delete, update
Admin bool
ExecPath string
}
func runContrib(opts *ContribOptions) error {
var action func(*ContribOptions) error
switch opts.Operation {
case submitOp:
action = submit
case delOp:
action = delete
case updateOp:
action = update
}
return action(opts)
}
func submit(opts *ContribOptions) error {
// TODO run survey
// TODO create pending row
// TODO email me
// TODO print confirmation
return nil
}
func update(opts *ContribOptions) error {
return nil
}
func delete(opts *ContribOptions) error {
return nil
}
func rootCmd() *cobra.Command {
var updateName string
var delName string
var admin bool
rc := &cobra.Command{
Use: "contrib [path to executable]",
Short: "Submit new commands to the town launcher",
RunE: func(cmd *cobra.Command, args []string) error {
// submit - requires path arg
// update - requres path arg
// delete - no arg needed
if updateName != "" && delName != "" {
return errors.New("-u and -d are mutually exclusive")
}
var cmdName string
operation := "submit"
if updateName != "" {
operation = "update"
cmdName = updateName
} else if delName != "" {
operation = "delete"
cmdName = delName
}
if (operation == "update" || operation == "submit") && len(args) == 0 {
return errors.New("path to executable required when submitting or updating")
}
if operation == "delete" && len(args) > 0 {
return fmt.Errorf("no arguments expected when deleting; got %d", len(args))
}
if len(args) > 1 {
return fmt.Errorf("at most one argument is accepted; got %d", len(args))
}
var execPath string
if len(args) == 1 {
execPath = args[0]
}
opts := &ContribOptions{
Operation: operation,
Admin: admin,
ExecPath: execPath,
CmdName: cmdName,
}
return runContrib(opts)
},
// TODO longer example
}
rc.Flags().StringVarP(&updateName, "update", "u", "", "Name of command to update")
rc.Flags().StringVarP(&delName, "delete", "d", "", "Name of command to delete")
rc.Flags().BoolVarP(&admin, "admin", "a", false, "admin level operations")
return rc
}
func main() { func main() {
// TODO if err := rootCmd().Execute(); err != nil {
fmt.Println("TODO") fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
} }

2
go.mod
View File

@ -1,6 +1,6 @@
module git.tilde.town/tildetown/town module git.tilde.town/tildetown/town
go 1.14 go 1.18
require ( require (
github.com/AlecAivazis/survey/v2 v2.3.5 github.com/AlecAivazis/survey/v2 v2.3.5