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) 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)) f, err := os.Create(path.Join(contribRequestPath, fname))
if err != nil { if err != nil {
return fmt.Errorf("failed to open contrib file for writing: %w", err) return fmt.Errorf("failed to open contrib file for writing: %w", err)

View File

@ -5,11 +5,9 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"os/exec" "os/exec"
"os/user"
"path/filepath" "path/filepath"
"strings" "strings"
townuser "git.tilde.town/tildetown/town/user"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -48,19 +46,7 @@ const binroot = "/town/commands"
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
Use: "town", Use: "town",
Short: "Run commands unique to tilde.town", Short: "Run commands unique to tilde.town",
} // TODO Long example showing how to contribute a command
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)
} }
func parseCommands(targetCmd *cobra.Command, path string) error { func parseCommands(targetCmd *cobra.Command, path string) error {
@ -79,61 +65,43 @@ func parseCommands(targetCmd *cobra.Command, path string) error {
return nil return nil
} }
type commandDoc struct { type cmdDoc struct {
ShortDesc string `yaml:"shortDesc"` CmdName string
LongDesc string `yaml:"longDesc"` ExecPath string
Examples string
Maintainer string Maintainer string
Category string
ShortDesc string
LongDesc string
} }
func parseCommand(targetCmd *cobra.Command, yamlPath 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) yamlBytes, err := ioutil.ReadFile(yamlPath)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "could not read %s; skipping...\n", yamlPath) fmt.Fprintf(os.Stderr, "could not read %s; skipping...\n", yamlPath)
return return
} }
doc := commandDoc{} var doc cmdDoc
err = yaml.Unmarshal(yamlBytes, &doc) err = yaml.Unmarshal(yamlBytes, &doc)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "could not parse %s; skipping...\n", yamlPath) fmt.Fprintf(os.Stderr, "could not parse %s; skipping...\n", yamlPath)
return return
} }
if doc.Maintainer == "" {
fmt.Fprintf(os.Stderr, "%s is missing maintainer field; skipping...\n", yamlPath)
return
}
parsedCmd := &cobra.Command{ parsedCmd := &cobra.Command{
Use: filepath.Base(executablePath), Use: doc.CmdName,
RunE: execWrapper(executablePath), RunE: execWrapper(doc.ExecPath),
DisableFlagParsing: true, DisableFlagParsing: true,
} }
if doc.ShortDesc != "" { if doc.ShortDesc != "" {
parsedCmd.Short = doc.ShortDesc parsedCmd.Short = doc.ShortDesc
} }
if doc.LongDesc != "" { if doc.LongDesc != "" {
parsedCmd.Long = 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) 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) targetCmd.AddCommand(parsedCmd)
} }
@ -148,30 +116,14 @@ func execWrapper(executablePath string) func(*cobra.Command, []string) error {
} }
func cli() int { func cli() int {
err := parseCommands(rootCmd, "core") files, err := ioutil.ReadDir(binroot)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "failed to parse core commands: %s", err) fmt.Fprintf(os.Stderr, "failed to list directory %s: %s", binroot, err)
return 1
} }
err = parseCommands(rootCmd, "contrib") for _, file := range files {
if err != nil { if strings.HasSuffix(file.Name(), "yml") {
fmt.Fprintf(os.Stderr, "failed to parse contrib commands: %s", err) parseCommand(rootCmd, filepath.Join(binroot, file.Name()))
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
} }
} }