cmd

package
v0.0.0-...-18a32cf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2020 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FmtCmd = &cobra.Command{
	Use:   "fmt",
	Short: "format input",
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
		var docs lib.Docs
		for _, fp := range args {
			f, err := os.Open(fp)
			if err != nil {
				fmt.Println(err.Error())
				os.Exit(1)
			}

			found, err := lib.Parse(f)
			if err != nil {
				fmt.Println(err.Error())
				os.Exit(1)
			}
			docs = append(docs, found...)
		}

		noSort, err := cmd.Flags().GetBool("no-sort")
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		if !noSort {
			docs.Sort()
		}

		for _, doc := range docs {
			data, err := doc.MarshalIndent(0, "  ")
			if err != nil {
				fmt.Println(err.Error())
				os.Exit(1)
			}

			fmt.Print(string(data) + "\n")
		}

	},
}

FmtCmd prints present configuration info

View Source
var PackageCmd = &cobra.Command{
	Use:     "package",
	Aliases: []string{"pkg"},
	Short:   "exctract and execute outline documents from a go package against a template",
	Long:    ``,
	Run: func(cmd *cobra.Command, args []string) {
		funcMap := template.FuncMap{
			"split":          strings.Split,
			"trim":           strings.TrimSpace,
			"replace_all":    strings.ReplaceAll,
			"sanitizeAnchor": sanitizeAnchor,
		}

		t := template.Must(template.New("mdIndex").Funcs(funcMap).Parse(mdIndex))

		str, err := cmd.Flags().GetString("template")
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		ctx, err := cmd.Flags().GetString("context-dir")
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		if str != "" {
			b, err := ioutil.ReadFile(str)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}

			t, err = template.New("mdIndex").Funcs(funcMap).Parse(string(b))
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
		}

		docs := map[string]*lib.Doc{}
		for _, pkg := range args {
			pkg, err := parseutil.PackageAST(pkg)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}

			for _, f := range pkg.Files {
				for _, c := range f.Comments {
					buf := strings.NewReader(c.Text())
					read, err := lib.Parse(buf)
					if err != nil {
						fmt.Println(err.Error())
						os.Exit(1)
					}

					for _, doc := range read {
						if found, ok := docs[doc.Name]; ok {
							merge(found, doc)
							continue
						}

						docs[doc.Name] = doc
					}
				}
			}
		}

		noSort, err := cmd.Flags().GetBool("no-sort")
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}

		var list lib.Docs
		for _, doc := range docs {
			list = append(list, doc)
		}

		if !noSort {
			list.Sort()
		}

		if ctx != "" {
			os.Chdir(ctx)
		}

		if err := t.Execute(os.Stdout, list); err != nil {
			fmt.Println(err.Error())
			os.Exit(1)
		}
	},
}

PackageCmd extracts and execute outline documents from a go package against a template",

View Source
var RootCmd = &cobra.Command{
	Short: "outline is a tool for outlining software packages",
	PersistentPreRun: func(cmd *cobra.Command, args []string) {
		if debug, err := cmd.Flags().GetBool("debug"); err == nil && debug {
			logrus.SetLevel(logrus.DebugLevel)
		}
	},
}

RootCmd is the walk command

View Source
var TemplateCmd = &cobra.Command{
	Use:     "template",
	Aliases: []string{"md"},
	Short:   "execute outline documents against a template",
	Long:    ``,
	Run: func(cmd *cobra.Command, args []string) {
		t := template.Must(template.New("mdIndex").Parse(mdIndex))

		str, err := cmd.Flags().GetString("template")
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		if str != "" {
			t, err = template.ParseFiles(str)
			if err != nil {
				fmt.Println(err)
				os.Exit(1)
			}
		}

		var docs lib.Docs
		for _, fp := range args {
			f, err := os.Open(fp)
			if err != nil {
				fmt.Println(err.Error())
				os.Exit(1)
			}

			read, err := lib.Parse(f)
			if err != nil {
				fmt.Println(err.Error())
				os.Exit(1)
			}
			docs = append(docs, read...)
		}

		noSort, err := cmd.Flags().GetBool("no-sort")
		if err != nil {
			fmt.Println(err)
			os.Exit(1)
		}
		if !noSort {
			docs.Sort()
		}

		if err := t.Execute(os.Stdout, docs); err != nil {
			fmt.Println(err.Error())
			os.Exit(1)
		}
	},
}

TemplateCmd parses outline documents & executes them against a template

Functions

func Execute

func Execute()

Execute adds all child commands to the root command sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL