few things

- code cleanup
- remove buckets from invocation (preserve on disk)
- add maintainer to doc schema
master
vilmibm 2020-06-09 04:30:34 +00:00
parent 24aaa81aea
commit f685a9baa2
1 changed files with 43 additions and 37 deletions

76
main.go
View File

@ -20,16 +20,6 @@ var rootCmd = &cobra.Command{
Short: "Run commands unique to tilde.town", 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) { func isAdmin() (bool, error) {
u, err := user.Current() u, err := user.Current()
if err != nil { if err != nil {
@ -53,34 +43,13 @@ func isAdmin() (bool, error) {
} }
return false, nil return false, nil
} }
func init() { func parseCommands(targetCmd *cobra.Command, path string) error {
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) {
binPath := filepath.Join(binroot, path) binPath := filepath.Join(binroot, path)
files, err := ioutil.ReadDir(binPath) files, err := ioutil.ReadDir(binPath)
if err != nil { 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 { for _, file := range files {
@ -88,12 +57,15 @@ func parseCommands(targetCmd *cobra.Command, path string) {
parseCommand(targetCmd, filepath.Join(binPath, file.Name())) parseCommand(targetCmd, filepath.Join(binPath, file.Name()))
} }
} }
return nil
} }
type commandDoc struct { type commandDoc struct {
ShortDesc string `yaml:"shortDesc"` ShortDesc string `yaml:"shortDesc"`
LongDesc string `yaml:"longDesc"` LongDesc string `yaml:"longDesc"`
Examples string Examples string
Maintainer string
} }
func parseCommand(targetCmd *cobra.Command, yamlPath 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 {
rootCmd.Execute() 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())
} }