From f685a9baa219d3b82d136d3751ee2d002a111dbf Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 9 Jun 2020 04:30:34 +0000 Subject: [PATCH] few things - code cleanup - remove buckets from invocation (preserve on disk) - add maintainer to doc schema --- main.go | 80 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/main.go b/main.go index d2d9b43..adc6bd5 100644 --- a/main.go +++ b/main.go @@ -20,16 +20,6 @@ var rootCmd = &cobra.Command{ Short: "Run commands unique to tilde.town", } -var contribCmd = &cobra.Command{ - Use: "contrib", - Short: "community-maintained town commands", -} - -var adminCmd = &cobra.Command{ - Use: "admin", - Short: "commands used for administering the town", -} - func isAdmin() (bool, error) { u, err := user.Current() if err != nil { @@ -53,34 +43,13 @@ func isAdmin() (bool, error) { } return false, nil - } -func init() { - rootCmd.AddCommand(contribCmd) - - parseCommands(rootCmd, "core") - parseCommands(contribCmd, "contrib") - - admin, err := isAdmin() - if err != nil { - panic(fmt.Sprintf("failed to check admin status: %s", err)) - } - - if admin { - rootCmd.AddCommand(adminCmd) - parseCommands(adminCmd, "admin") - } - - // I feel like the example/documentation yaml can be frontmatter for non-binary files. to start - // i'll just do the accompanying yaml file. -} - -func parseCommands(targetCmd *cobra.Command, path string) { +func parseCommands(targetCmd *cobra.Command, path string) error { binPath := filepath.Join(binroot, path) files, err := ioutil.ReadDir(binPath) if err != nil { - panic(fmt.Sprintf("failed to list directory %s: %s", binPath, err)) + return fmt.Errorf("failed to list directory %s: %s", binPath, err) } for _, file := range files { @@ -88,12 +57,15 @@ func parseCommands(targetCmd *cobra.Command, path string) { parseCommand(targetCmd, filepath.Join(binPath, file.Name())) } } + + return nil } type commandDoc struct { - ShortDesc string `yaml:"shortDesc"` - LongDesc string `yaml:"longDesc"` - Examples string + ShortDesc string `yaml:"shortDesc"` + LongDesc string `yaml:"longDesc"` + Examples string + Maintainer string } func parseCommand(targetCmd *cobra.Command, yamlPath string) { @@ -149,6 +121,40 @@ func execWrapper(executablePath string) func(*cobra.Command, []string) error { } } -func main() { +func cli() int { + err := parseCommands(rootCmd, "core") + if err != nil { + fmt.Fprintf(os.Stderr, "failed to parse core commands: %s", err) + return 1 + } + + 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 { + err = parseCommands(rootCmd, "admin") + if err != nil { + fmt.Fprintf(os.Stderr, "failed to parse admin commands: %s", err) + return 1 + } + } + + // I feel like the example/documentation yaml can be frontmatter for non-binary files. to start + // i'll just do the accompanying yaml file. rootCmd.Execute() + + return 0 +} + +func main() { + os.Exit(cli()) }