Skip to content

Commit

Permalink
Merge pull request #2 from jeffsvajlenko/mvp
Browse files Browse the repository at this point in the history
Mvp
  • Loading branch information
jeffsvajlenko committed Jan 1, 2022
2 parents c7b1e4b + 01378bb commit 7df1bb2
Show file tree
Hide file tree
Showing 87 changed files with 60,318 additions and 13 deletions.
4 changes: 0 additions & 4 deletions Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ vet:
go vet ./...

build:
go build -o bin/ ./cmd/goduped.go
go build -o bin/goduped ./main.go
91 changes: 91 additions & 0 deletions cmd/detect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright © 2021 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"GoDupeDetector/internal/detection"
"GoDupeDetector/internal/parsing"
"GoDupeDetector/internal/printer"
"bufio"
"errors"
"fmt"
"log"
"os"

"github.com/spf13/cobra"
)

// detectCmd represents the detect command
var detectCmd = &cobra.Command{
Use: "detect",
Short: "Runs clone detection.",
Long: `Runs clone detection.`,
RunE: func(cmd *cobra.Command, args []string) error {

input, _ := cmd.Flags().GetString("input")
output, _ := cmd.Flags().GetString("output")

finfo, err := os.Stat(input)
if finfo == nil {
return errors.New("Input directory does not exist or is invalid.")
}
if !finfo.IsDir() {
return errors.New("Input must be a directory.")
}

finfo, err = os.Stat(output)
if finfo != nil {
return errors.New("Output file already exists or is a directory.")
}
outf, err := os.Create(output)
if err != nil {
return fmt.Errorf("Failed to create output file with error: %s", err.Error())
}
defer outf.Close()

fmt.Print("Collecting go files...")
files, err := parsing.FileList(input)
if err != nil {
log.Fatal(err)
}
fmt.Printf(" %d files collected.\n", len(files))

fmt.Print("Parsing...")
pset, _ := parsing.Parse(files)
fmt.Println(" Done!")

fmt.Print("Detecting...")
cset, _ := detection.DetectClones(pset, 0.70)
fmt.Printf(" %d clones detected.\n", len(cset.Clones))

fmt.Println("Outputting...")
bout := bufio.NewWriter(outf)
err = printer.PrintCloneReport(pset, cset, bout)

if err != nil {
log.Fatal(err)
}

return nil
},
}

func init() {
rootCmd.AddCommand(detectCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// detectCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// detectCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
detectCmd.Flags().String("input", "", "Input source folder.")
detectCmd.MarkFlagRequired("input")
detectCmd.Flags().String("output", "", "Output report file.")
detectCmd.MarkFlagRequired("output")
}
7 changes: 0 additions & 7 deletions cmd/goduped.go

This file was deleted.

46 changes: 46 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright © 2021 Jeffrey Svajlenko
*/
package cmd

import (
"fmt"
"os"

"github.com/spf13/cobra"
)

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "goduped",
Short: "GoDupeDetector: A golang clone detector.",
Long: "GoDupeDetector: A golang clone detector.",
// Uncomment the following line if your bare application
// has an action associated with it:
//Run: func(cmd *cobra.Command, args []string) {
//},
}

// 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.
func Execute() error {
return rootCmd.Execute()
}

func init() {
// 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", "", "config file (default is $HOME/.GoDupeDetector.yaml)")

// Cobra also supports local flags, which will only run
// when this action is called directly.
//rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func er(msg interface{}) {
fmt.Println("Error:", msg)
os.Exit(1)
}
35 changes: 35 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

// versionCmd represents the version command
var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the version of GoDupeDetector",
Long: `Prints the version of GoDupeDetector:`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("v0.0.1")
},
}

func init() {
rootCmd.AddCommand(versionCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// versionCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// versionCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
28 changes: 27 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
module "GoDupeDetector"
module GoDupeDetector

go 1.17

require github.com/spf13/cobra v1.3.0

require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20211210111614-af8b64212486 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.2 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.10.1 // indirect
)
Loading

0 comments on commit 7df1bb2

Please sign in to comment.