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

74
main.go
View File

@ -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
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())
}