simplify this and make it parse new format

trunk
vilmibm 2022-10-30 23:09:06 +00:00
parent b1f7e9842d
commit 75d64bf7b6
2 changed files with 16 additions and 64 deletions

View File

@ -159,7 +159,7 @@ func submit(opts *contribOpts) error {
return fmt.Errorf("failed to serialize contrib: %w", err)
}
fname := fmt.Sprintf("%d", rand.Intn(10000))
fname := fmt.Sprintf("%d.yml", time.Now().Unix())
f, err := os.Create(path.Join(contribRequestPath, fname))
if err != nil {
return fmt.Errorf("failed to open contrib file for writing: %w", err)

View File

@ -5,11 +5,9 @@ import (
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"strings"
townuser "git.tilde.town/tildetown/town/user"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
@ -48,19 +46,7 @@ const binroot = "/town/commands"
var rootCmd = &cobra.Command{
Use: "town",
Short: "Run commands unique to tilde.town",
}
var adminCmd = &cobra.Command{
Use: "admin",
Short: "Run administrative commands",
}
func isAdmin() (bool, error) {
u, err := user.Current()
if err != nil {
return false, fmt.Errorf("failed to get information about current user: %w", err)
}
return townuser.IsAdmin(u)
// TODO Long example showing how to contribute a command
}
func parseCommands(targetCmd *cobra.Command, path string) error {
@ -79,61 +65,43 @@ func parseCommands(targetCmd *cobra.Command, path string) error {
return nil
}
type commandDoc struct {
ShortDesc string `yaml:"shortDesc"`
LongDesc string `yaml:"longDesc"`
Examples string
type cmdDoc struct {
CmdName string
ExecPath string
Maintainer string
Category string
ShortDesc string
LongDesc string
}
func parseCommand(targetCmd *cobra.Command, yamlPath string) {
executablePath := strings.TrimSuffix(yamlPath, ".yml")
// TODO handle when files lack executable bit
_, err := os.Stat(executablePath)
if err != nil {
fmt.Fprintf(os.Stderr, "could not find matching executable for %s; skipping...\n", yamlPath)
return
}
yamlBytes, err := ioutil.ReadFile(yamlPath)
if err != nil {
fmt.Fprintf(os.Stderr, "could not read %s; skipping...\n", yamlPath)
return
}
doc := commandDoc{}
var doc cmdDoc
err = yaml.Unmarshal(yamlBytes, &doc)
if err != nil {
fmt.Fprintf(os.Stderr, "could not parse %s; skipping...\n", yamlPath)
return
}
if doc.Maintainer == "" {
fmt.Fprintf(os.Stderr, "%s is missing maintainer field; skipping...\n", yamlPath)
return
}
parsedCmd := &cobra.Command{
Use: filepath.Base(executablePath),
RunE: execWrapper(executablePath),
Use: doc.CmdName,
RunE: execWrapper(doc.ExecPath),
DisableFlagParsing: true,
}
if doc.ShortDesc != "" {
parsedCmd.Short = doc.ShortDesc
}
if doc.LongDesc != "" {
parsedCmd.Long = doc.LongDesc
}
parsedCmd.Long += fmt.Sprintf("\nMaintained by %s; reach out to them via mail or chat with questions", doc.Maintainer)
if doc.Examples != "" {
parsedCmd.Example = doc.Examples
}
targetCmd.AddCommand(parsedCmd)
}
@ -148,30 +116,14 @@ func execWrapper(executablePath string) func(*cobra.Command, []string) error {
}
func cli() int {
err := parseCommands(rootCmd, "core")
files, err := ioutil.ReadDir(binroot)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to parse core commands: %s", err)
return 1
fmt.Fprintf(os.Stderr, "failed to list directory %s: %s", binroot, err)
}
err = parseCommands(rootCmd, "contrib")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to parse contrib commands: %s", err)
return 1
}
admin, err := isAdmin()
if err != nil {
fmt.Fprintf(os.Stderr, "failed to check admin status: %s", err)
return 2
}
if admin {
rootCmd.AddCommand(adminCmd)
err = parseCommands(adminCmd, "admin")
if err != nil {
fmt.Fprintf(os.Stderr, "failed to parse admin commands: %s", err)
return 1
for _, file := range files {
if strings.HasSuffix(file.Name(), "yml") {
parseCommand(rootCmd, filepath.Join(binroot, file.Name()))
}
}