Skip to content

Commit

Permalink
Add cmd collect (#6)
Browse files Browse the repository at this point in the history
* golangtransformer complete with transform code

* fix: removed kubernetes dep

* feat: add command collect
  • Loading branch information
Prakhar-Agarwal-byte authored Oct 31, 2023
1 parent 876e008 commit db8dfa3
Show file tree
Hide file tree
Showing 14 changed files with 1,909 additions and 1 deletion.
88 changes: 88 additions & 0 deletions cmd/collect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright IBM Corporation 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cmd

import (
"os"
"path/filepath"
"strings"

"github.com/konveyor/move2kube-wasm/lib"
"github.com/konveyor/move2kube-wasm/types"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type collectFlags struct {
annotations string
outpath string
srcpath string
}

func collectHandler(flags collectFlags) {
var err error
annotations := flags.annotations
outpath := flags.outpath
srcpath := flags.srcpath

if outpath != "" {
if outpath, err = filepath.Abs(outpath); err != nil {
logrus.Fatalf("Failed to make the output directory path '%s' absolute. Error: %q", outpath, err)
}
}
if srcpath != "" {
srcpath, err = filepath.Abs(srcpath)
if err != nil {
logrus.Fatalf("Failed to make the source directory path '%s' absolute. Error: %q", srcpath, err)
}
fi, err := os.Stat(srcpath)
if os.IsNotExist(err) {
logrus.Fatalf("Source directory '%s' does not exist. Error: %q", srcpath, err)
} else if err != nil {
logrus.Fatalf("Error while accessing directory: '%s' . Error: %q", srcpath, err)
} else if !fi.IsDir() {
logrus.Fatalf("Source path is a file, expected '%s' to be a directory.", srcpath)
}
}
outpath = filepath.Join(filepath.Clean(outpath), types.AppNameShort+"_collect")
if annotations == "" {
lib.Collect(srcpath, outpath, []string{})
} else {
lib.Collect(srcpath, outpath, strings.Split(annotations, ","))
}
logrus.Infof("Collect Output in [%s]. Copy this directory into the source directory to be used for planning.", outpath)
}

// GetCollectCommand returns a command to collect information from running applications
func GetCollectCommand() *cobra.Command {
viper.AutomaticEnv()

flags := collectFlags{}
collectCmd := &cobra.Command{
Use: "collect",
Short: "Collect and process metadata from multiple sources.",
Long: "Collect metadata from multiple sources (cluster, image repo etc.), filter and summarize it into a yaml.",
Run: func(*cobra.Command, []string) { collectHandler(flags) },
}

collectCmd.Flags().StringVarP(&flags.annotations, "annotations", "a", "", "Specify annotations to select collector subset.")
collectCmd.Flags().StringVarP(&flags.outpath, outputFlag, "o", ".", "Specify output directory for collect.")
collectCmd.Flags().StringVarP(&flags.srcpath, sourceFlag, "s", "", "Specify source directory for the artifacts to be considered while collecting.")

return collectCmd
}
109 changes: 109 additions & 0 deletions cmd/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright IBM Corporation 2021
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package cmd

import (
"bytes"
"os"
"path/filepath"
"strings"
"text/template"

"github.com/konveyor/move2kube-wasm/common"
"github.com/konveyor/move2kube-wasm/types"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
"github.com/spf13/viper"
)

type generateFlags struct {
outputDir string
}

const docsTemplateStr = `---
layout: default
title: {{ .Title }}
permalink: /commands{{ .URL }}
{{- if .Parent }}
parent: {{ .Parent -}}
{{ end }}
{{ .Extra -}}
---
{{ .Notes -}}
`

func generateHandler(flags generateFlags) {
docsTemplate := template.Must(template.New("gen-docs").Parse(docsTemplateStr))
filePrepender := func(filename string) string {
data := struct {
Title string
URL string
Parent string
Extra string
Notes string
}{}
commandsParent := "Commands"
data.Title = strings.TrimPrefix(strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)), types.AppName+"_")
data.URL = "/" + data.Title
data.Parent = commandsParent
if data.Title == types.AppName {
data.Title = commandsParent
data.URL = ""
data.Parent = ""
data.Extra = "has_children: true\nnav_order: 4\nhas_toc: false\n"
data.Notes = "\n###### This documentation was generated by running `" + types.AppName + " docs`\n\n"
}
output := bytes.Buffer{}
if err := docsTemplate.Execute(&output, data); err != nil {
logrus.Errorf("failed to generate the documentation for the filename %s using the template. Error: %q", filename, err)
return ""
}
return output.String()
}
linkHandler := func(filename string) string {
link := strings.TrimPrefix(strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)), types.AppName+"_")
if link == types.AppName {
return "/commands"
}
return "/commands/" + link
}
logrus.Infof("generating the documentation for all the commands into the directory at the path: %s", flags.outputDir)
if err := os.MkdirAll(flags.outputDir, common.DefaultDirectoryPermission); err != nil {
logrus.Fatalf("error while making the output directory at path %s to store the documentation. Error: %q", flags.outputDir, err)
}
rootCmd := GetRootCmd()
if err := doc.GenMarkdownTreeCustom(rootCmd, flags.outputDir, filePrepender, linkHandler); err != nil {
logrus.Fatalf("error while generating documentation. Error: %q", err)
}
}

// GetGenerateDocsCommand returns a command to generate the documentation for all the commands
func GetGenerateDocsCommand() *cobra.Command {
viper.AutomaticEnv()
flags := generateFlags{}
generateDocsCmd := &cobra.Command{
Hidden: true,
Use: "docs",
Short: "Generate the documentation for the commands",
Long: "Generate the documentation for the commands. The documentation is in markdown format.",
Run: func(_ *cobra.Command, __ []string) { generateHandler(flags) },
}
generateDocsCmd.Flags().StringVarP(&flags.outputDir, "output", "o", "commands", "Path to the directory where the documentation will be generated.")
return generateDocsCmd
}
Loading

0 comments on commit db8dfa3

Please sign in to comment.