WIP working on contrib

pull/1/head
vilmibm 2022-07-31 13:43:29 +00:00
parent 77049703a4
commit a2e5c81752
1 changed files with 115 additions and 4 deletions

View File

@ -1,10 +1,121 @@
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() {
// TODO
fmt.Println("TODO")
if err := rootCmd().Execute(); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
os.Exit(1)
}
}