Code cleanup for public release

main
Nick Dumas 2 years ago
parent 499a5e6b6d
commit d24ceeac69

@ -5,6 +5,7 @@ package cmd
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -18,7 +19,6 @@ var (
format string format string
) )
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{ var rootCmd = &cobra.Command{
TraverseChildren: true, TraverseChildren: true,
Use: "obp", Use: "obp",
@ -33,8 +33,7 @@ var rootCmd = &cobra.Command{
// Execute adds all child commands to the root command and sets flags appropriately. // Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd. // This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() { func Execute() {
err := rootCmd.Execute() if err := rootCmd.Execute(); err != nil {
if err != nil {
os.Exit(1) os.Exit(1)
} }
} }
@ -42,24 +41,32 @@ func Execute() {
func init() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
// will be global for your application.
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "~/.obp.toml", "config file") rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "~/.obp.toml", "config file")
rootCmd.PersistentFlags().StringVar(&vault, "vault", "", "vault root directory") rootCmd.PersistentFlags().StringVar(&vault, "vault", "", "vault root directory")
rootCmd.MarkPersistentFlagRequired("vault")
err := rootCmd.MarkPersistentFlagRequired("vault")
if err != nil {
log.Panicln("error setting vault flag as required")
}
rootCmd.PersistentFlags().StringVar(&format, "format", "markdown", "output format [markdown, json, csv]") rootCmd.PersistentFlags().StringVar(&format, "format", "markdown", "output format [markdown, json, csv]")
rootCmd.MarkPersistentFlagRequired("format")
viper.BindPFlag("format", rootCmd.PersistentFlags().Lookup("format")) err = rootCmd.MarkPersistentFlagRequired("format")
viper.BindPFlag("vault", rootCmd.PersistentFlags().Lookup("vault")) if err != nil {
log.Panicln("error setting format flag as required")
}
err = viper.BindPFlag("format", rootCmd.PersistentFlags().Lookup("format"))
if err != nil {
log.Panicln("error binding viper to format flag")
}
// Cobra also supports local flags, which will only run err = viper.BindPFlag("vault", rootCmd.PersistentFlags().Lookup("vault"))
// when this action is called directly. if err != nil {
// rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") log.Panicln("error binding viper to vault flag")
}
// rootCmd.SetHelpFunc(gloss.CharmHelp) // rootCmd.SetHelpFunc(gloss.CharmHelp)
// rootCmd.SetUsageFunc(gloss.CharmUsage) // rootCmd.SetUsageFunc(gloss.CharmUsage)

@ -10,6 +10,10 @@ import (
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
// Validate accepts a Markdown file as input via the Reader
// and parses the frontmatter present, if any. It then
// applies the schema fetched from schemaURL against the
// decoded YAML.
func Validate(schemaURL string, r io.Reader) error { func Validate(schemaURL string, r io.Reader) error {
var m interface{} var m interface{}
@ -42,6 +46,10 @@ func recurseDetails(detailed jsonschema.Detailed, acc map[string]jsonschema.Deta
return acc return acc
} }
// PrettyDetails takes error output from jsonschema.Validate
// and pretty-prints it to stdout.
//
// Supported formats are: JSON, Markdown
func PrettyDetails(w io.Writer, format string, details jsonschema.Detailed, filename string) error { func PrettyDetails(w io.Writer, format string, details jsonschema.Detailed, filename string) error {
// acc := make([]jsonschema.Detailed, 0) // acc := make([]jsonschema.Detailed, 0)
acc := make(map[string]jsonschema.Detailed) acc := make(map[string]jsonschema.Detailed)

Loading…
Cancel
Save