suggest.go 1.1 KB
Newer Older
P
Phodal Huang 已提交
1 2 3
package cmd

import (
P
Phodal Huang 已提交
4
	"encoding/json"
P
Phodal Huang 已提交
5
	"github.com/olekukonko/tablewriter"
P
Phodal Huang 已提交
6
	"github.com/phodal/coca/cmd/cmd_util"
P
Phodal Huang 已提交
7
	"github.com/phodal/coca/cmd/config"
P
Phodal Huang 已提交
8
	"github.com/phodal/coca/core/context/suggest"
P
Phodal Huang 已提交
9
	"github.com/spf13/cobra"
P
Phodal Huang 已提交
10
	"log"
P
Phodal Huang 已提交
11
	"os"
P
Phodal Huang 已提交
12 13 14
)

var (
P
Phodal Huang 已提交
15
	suggestConfig ApiCmdConfig
P
Phodal Huang 已提交
16 17 18 19
)

var suggestCmd = &cobra.Command{
	Use:   "suggest",
P
Phodal Huang 已提交
20
	Short: "find usable Design Patterns from code",
P
Phodal Huang 已提交
21 22
	Long:  ``,
	Run: func(cmd *cobra.Command, args []string) {
P
Phodal Huang 已提交
23
		parsedDeps = nil
P
Phodal Huang 已提交
24
		depFile := cmd_util.ReadFile(apiCmdConfig.DependencePath)
P
Phodal Huang 已提交
25 26 27 28 29 30
		if depFile == nil {
			log.Fatal("lost deps")
		}

		_ = json.Unmarshal(depFile, &parsedDeps)

P
Phodal Huang 已提交
31 32 33 34 35 36 37 38
		app := suggest.NewSuggestApp()
		results := app.AnalysisPath(parsedDeps)

		table := tablewriter.NewWriter(os.Stdout)
		table.SetHeader([]string{"Class", "Pattern", "Reason"})

		for _, result := range results {
			table.Append([]string{result.Class, result.Pattern, result.Reason})
P
Phodal Huang 已提交
39
		}
P
Phodal Huang 已提交
40 41

		table.Render()
P
Phodal Huang 已提交
42 43 44 45 46 47
	},
}

func init() {
	rootCmd.AddCommand(suggestCmd)

P
Phodal Huang 已提交
48
	suggestCmd.PersistentFlags().StringVarP(&suggestConfig.DependencePath, "dependence", "d", config.CocaConfig.ReporterPath+"/deps.json", "get dependence D")
P
Phodal Huang 已提交
49
}