github.com/jacekolszak/noteo@v0.5.0/cmd/root.go (about)

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/spf13/cobra"
     8  )
     9  
    10  func Root() *cobra.Command {
    11  	root := cobra.Command{
    12  		Use:           "noteo",
    13  		Short:         "Command line note-taking assistant",
    14  		Version:       "0.5.0",
    15  		SilenceErrors: true,
    16  		SilenceUsage:  true,
    17  	}
    18  	root.AddCommand(initialize)
    19  	root.AddCommand(add)
    20  	root.AddCommand(ls())
    21  	root.AddCommand(tag())
    22  	root.AddCommand(mv)
    23  	return &root
    24  }
    25  
    26  func Execute() {
    27  	root := Root()
    28  	if err := root.Execute(); err != nil {
    29  		if e, ok := err.(repositoryError); ok && e.IsNotRepository() {
    30  			_, _ = fmt.Fprintln(os.Stderr, "not a noteo repository (or any of the parent directories)")
    31  			printer := NewPrinter()
    32  			printer.Print("Please run ")
    33  			printer.PrintCommand("noteo init")
    34  			printer.Println()
    35  		} else {
    36  			_, _ = fmt.Fprintln(os.Stderr, err)
    37  			if err := root.UsageFunc()(root); err != nil {
    38  				_, _ = fmt.Fprintln(os.Stderr, err)
    39  			}
    40  		}
    41  		os.Exit(1)
    42  	}
    43  }
    44  
    45  type repositoryError interface {
    46  	IsNotRepository() bool
    47  }