github.com/mfpierre/corectl@v0.5.6/utils.go (about) 1 // Copyright 2015 - António Meireles <antonio.meireles@reformi.st> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 16 package main 17 18 import ( 19 "fmt" 20 "github.com/spf13/cobra" 21 "github.com/spf13/cobra/doc" 22 "os" 23 ) 24 25 var ( 26 utilsCmd = &cobra.Command{ 27 Use: "utils", 28 Short: "Some developer focused utilies", 29 Run: utilsCommand, 30 Hidden: true, 31 } 32 manCmd = &cobra.Command{ 33 Use: "mkMan", 34 Short: "Generates man pages", 35 PreRunE: defaultPreRunE, 36 Run: manCommand, 37 } 38 mkdownCmd = &cobra.Command{ 39 Use: "mkMkdown", 40 Short: "Generates Markdown documentation", 41 PreRunE: defaultPreRunE, 42 Run: mkdownCommand, 43 } 44 ) 45 46 func utilsCommand(cmd *cobra.Command, args []string) { 47 cmd.Help() 48 } 49 50 func manCommand(cmd *cobra.Command, args []string) { 51 header := &doc.GenManHeader{ 52 Title: "corectl", Source: " ", 53 } 54 manDir := "documentation/man/" 55 // check if target dir exists.if not create it 56 if _, err := os.Stat(manDir); os.IsNotExist(err) { 57 if err = os.MkdirAll(manDir, 0644); err != nil { 58 fmt.Printf("failed to create %s. "+ 59 "No manual pages generated\n", manDir) 60 } 61 } 62 doc.GenManTree(RootCmd, header, manDir) 63 } 64 65 func mkdownCommand(cmd *cobra.Command, args []string) { 66 markdownDir := "documentation/markdown/" 67 // check if target dir exists.if not create it 68 if _, err := os.Stat(markdownDir); os.IsNotExist(err) { 69 if err = os.MkdirAll(markdownDir, 0644); err != nil { 70 fmt.Printf("failed to create %s. "+ 71 "No markdown documentation generated\n", markdownDir) 72 } 73 } 74 doc.GenMarkdownTree(RootCmd, 75 engine.pwd+"/documentation/markdown/") 76 } 77 78 func init() { 79 utilsCmd.AddCommand(manCmd) 80 utilsCmd.AddCommand(mkdownCmd) 81 RootCmd.AddCommand(utilsCmd) 82 }