Skip to content

Commit

Permalink
Address golangci-lint report.
Browse files Browse the repository at this point in the history
Code clean up to improve quality as per the lint report.

Signed-off-by: Ritesh H Shukla <[email protected]>
  • Loading branch information
Ritesh H Shukla committed Nov 29, 2018
1 parent 9265a1a commit a71b827
Show file tree
Hide file tree
Showing 24 changed files with 142 additions and 232 deletions.
5 changes: 3 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ linters-settings:
maligned:
suggest-new: true
dupl:
threshold: 100
threshold: 200
goconst:
min-len: 2
min-occurrences: 2
Expand All @@ -18,4 +18,5 @@ linters:
disable:
- maligned
- lll

- gochecknoinits
- gochecknoglobals
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ clean:

.PHONY: test
## Setup, run all tests and clean
test: setup runtests clean
test: clean setup runtests clean

.PHONY: runtests
runtests:
Expand All @@ -89,3 +89,8 @@ runtests:
## Run gofmt on the cmd and pkg packages
gofmt:
@gofmt -s -w ./cmd ./pkg

.PHONY: check
## Runs static code analysis checks (golangci-lint)
check: gofmt
@golangci-lint run --max-same-issues 0 --verbose
17 changes: 10 additions & 7 deletions cmd/datamon/cmd/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ Every bundle is an entry in the history of a repository at a point in time.
}

var bundleOptions struct {
Id string
ID string
DataPath string
}

var bundleID = "bundle"
var destination = "destination"

func init() {
rootCmd.AddCommand(bundleCmd)
}

func addBundleFlag(cmd *cobra.Command) error {
cmd.Flags().StringVarP(&bundleOptions.Id, "hash", "i", "", "The hash id for the bundle")
return cmd.MarkFlagRequired("hash")
func addBundleFlag(cmd *cobra.Command) string {
cmd.Flags().StringVarP(&bundleOptions.ID, bundleID, "i", "", "The hash id for the bundle")
return bundleID
}

func addDataPathFlag(cmd *cobra.Command) error {
cmd.Flags().StringVarP(&bundleOptions.DataPath, "destination", "d", "", "The path to the download folder")
return cmd.MarkFlagRequired("destination")
func addDataPathFlag(cmd *cobra.Command) string {
cmd.Flags().StringVarP(&bundleOptions.DataPath, destination, "d", "", "The path to the download folder")
return destination
}
21 changes: 15 additions & 6 deletions cmd/datamon/cmd/bundle_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ package cmd

import (
"context"
"log"

"github.com/oneconcern/datamon/pkg/core"
"github.com/oneconcern/datamon/pkg/storage/gcs"
"github.com/oneconcern/datamon/pkg/storage/localfs"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"log"
)

// downloadBundleCmd is the command to download a specific bundle from Datamon and model it locally. The primary purpose
Expand All @@ -24,7 +25,7 @@ var downloadBundleCmd = &cobra.Command{

sourceStore := gcs.New(repoParams.Bucket)
destinationSource := localfs.New(afero.NewBasePathFs(afero.NewOsFs(), bundleOptions.DataPath))
archiveBundle, err := core.NewArchiveBundle(repoParams.RepoName, bundleOptions.Id, sourceStore)
archiveBundle, err := core.NewArchiveBundle(repoParams.RepoName, bundleOptions.ID, sourceStore)
if err != nil {
log.Fatalln(err)
}
Expand All @@ -36,15 +37,23 @@ var downloadBundleCmd = &cobra.Command{
}

func init() {

// Source
addBucketNameFlag(downloadBundleCmd)
addRepoNameOptionFlag(downloadBundleCmd)
requiredFlags := []string{addBucketNameFlag(downloadBundleCmd)}
requiredFlags = append(requiredFlags, addRepoNameOptionFlag(downloadBundleCmd))

// Bundle to download
addBundleFlag(downloadBundleCmd)
requiredFlags = append(requiredFlags, addBundleFlag(downloadBundleCmd))

// Destination
addDataPathFlag(downloadBundleCmd)
requiredFlags = append(requiredFlags, addDataPathFlag(downloadBundleCmd))

for _, flag := range requiredFlags {
err := downloadBundleCmd.MarkFlagRequired(flag)
if err != nil {
log.Fatalln(err)
}
}

bundleCmd.AddCommand(downloadBundleCmd)
}
23 changes: 14 additions & 9 deletions cmd/datamon/cmd/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@ import (
"github.com/spf13/cobra"
)

const bash = "bash"
const zsh = "zsh"

// completionCmd represents the completion command
var completionCmd = &cobra.Command{
Use: "completion SHELL",
Short: "generate completions for the tpt command",
Short: "generate completions for the datamon command",
Long: `Generate completions for your shell
For bash add the following line to your ~/.bashrc
eval "$(tpt completion bash)"
eval "$(datamon completion bash)"
For zsh add generate a file:
tpt completion zsh > /usr/local/share/zsh/site-functions/_tpt
datamon completion zsh > /usr/local/share/zsh/site-functions/_datamon
`,
ValidArgs: []string{"bash", "zsh"},
ValidArgs: []string{bash, zsh},
Args: cobra.OnlyValidArgs,

Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -34,16 +37,18 @@ var completionCmd = &cobra.Command{
os.Exit(1)
}
shell := args[0]
if shell != "bash" && shell != "zsh" {
if shell != bash && shell != zsh {
// #nosec
fmt.Fprintln(os.Stderr, "the only supported shells are bash and zsh")
}
if shell == "bash" {
rootCmd.GenBashCompletion(os.Stdout)
if shell == bash {
err := rootCmd.GenBashCompletion(os.Stdout)
fmt.Fprintln(os.Stderr, "failed to generate bash completion:", err)
}

if shell == "zsh" {
rootCmd.GenZshCompletion(os.Stdout)
if shell == zsh {
err := rootCmd.GenZshCompletion(os.Stdout)
fmt.Fprintln(os.Stderr, "failed to generate zsh completion:", err)
}
},
}
Expand Down
1 change: 1 addition & 0 deletions cmd/datamon/cmd/fschecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
)

// DieIfNotAccessible exits the process if the path is not accessible.
func DieIfNotAccessible(path string) {
_, err := os.Stat(path)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion cmd/datamon/cmd/model_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package cmd
import (
"bytes"
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/oneconcern/datamon/pkg/config"
"github.com/oneconcern/datamon/pkg/kubeless"
"github.com/oneconcern/datamon/pkg/storage/sthree"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"io/ioutil"
"log"
"os"
Expand Down Expand Up @@ -36,7 +38,11 @@ var deployCmd = &cobra.Command{
log.Printf("Error while reading config file: %s", err)
}

viper.ReadConfig(bytes.NewBuffer(configFileBytes))
err = viper.ReadConfig(bytes.NewBuffer(configFileBytes))
if err != nil {
log.Fatalln(err)
}

processor := config.Processor{}

err = viper.Unmarshal(&processor)
Expand Down
20 changes: 9 additions & 11 deletions cmd/datamon/cmd/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
package cmd

import (
"github.com/oneconcern/pipelines/pkg/log"
"github.com/spf13/cobra"
)

var logger log.Factory

var repoParams struct {
Bucket string
RepoName string
}

func addRepoNameOptionFlag(cmd *cobra.Command) error {
flags := cmd.Flags()
flags.StringVarP(&repoParams.RepoName, "name", "n", "", "The name of this repository")
return cmd.MarkFlagRequired("name")
var name = "name"
var bucket = "bucket"

func addRepoNameOptionFlag(cmd *cobra.Command) string {
cmd.Flags().StringVarP(&repoParams.RepoName, name, "n", "", "The name of this repository")
return name
}

func addBucketNameFlag(cmd *cobra.Command) error {
flags := cmd.Flags()
flags.StringVarP(&repoParams.RepoName, "bucket", "b", "", "The name of the bucket used by datamon")
return cmd.MarkFlagRequired("bucket")
func addBucketNameFlag(cmd *cobra.Command) string {
cmd.Flags().StringVarP(&repoParams.RepoName, bucket, "b", "", "The name of the bucket used by datamon")
return bucket
}
129 changes: 0 additions & 129 deletions cmd/datamon/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,10 @@ package cmd

import (
"fmt"
"io"
"log"
"os"

"github.com/json-iterator/go"
"github.com/spf13/cobra"
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"
)

var (
cfgFile string
format string
)

// rootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -45,123 +36,3 @@ func Execute() {
func init() {
log.SetFlags(0)
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if os.Getenv("DATAMON_CONFIG") != "" {
// Use config file from the flag.
viper.SetConfigFile(os.Getenv("DATAMON_CONFIG"))
} else {
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME/.datamon")
viper.AddConfigPath("/etc/datamon")
viper.SetConfigName(".datamon")
}

viper.AutomaticEnv() // read in environment variables that match
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
log.Println("Using config file:", viper.ConfigFileUsed())
}
}

func print(data interface{}) error {
return formatters[format].Format(os.Stdout, data)
}

func printe(data interface{}) error {
return formatters[format].Format(os.Stderr, data)
}

// A Formatter is used to render output. They take an interface and output a byte array
//
// This byte array should be suitable for writing to a stream directly.
type Formatter interface {
Format(io.Writer, interface{}) error
}

// FormatterFunc provides a way to use functions as a formatter interface
type FormatterFunc func(io.Writer, interface{}) error

// Format the data with the function
func (f FormatterFunc) Format(w io.Writer, data interface{}) error {
return f(w, data)
}

// JSONFormatter for printing as pretified json
func JSONFormatter() FormatterFunc {
return func(w io.Writer, data interface{}) error {
enc := jsoniter.NewEncoder(w)
enc.SetIndent("", " ")
return enc.Encode(data)
}
}

// CompactJSONFormatter for priting as compact json
func CompactJSONFormatter() FormatterFunc {
return func(w io.Writer, data interface{}) error {
return jsoniter.NewEncoder(w).Encode(data)
}
}

// YAMLFormatter for printing as yaml
func YAMLFormatter() FormatterFunc {
return func(w io.Writer, data interface{}) error {
enc := yaml.NewEncoder(w)
defer enc.Close()
if err := enc.Encode(data); err != nil {
return err
}
return enc.Close()
}
}

var formatters map[string]Formatter

func initDefaultFormatters() {
if formatters == nil {
formatters = make(map[string]Formatter)
formatters["json"] = JSONFormatter()
formatters["compactjson"] = CompactJSONFormatter()
formatters["yaml"] = YAMLFormatter()
}
}

func knownFormatters() []string {
res := make([]string, len(formatters))
var i int
for k := range formatters {
res[i] = k
i++
}
return res
}

func addFormatFlag(cmd *cobra.Command, defaultValue string, extraFormatters ...map[string]Formatter) error {
initDefaultFormatters()
if defaultValue == "" {
defaultValue = "yaml"
}
cmd.Flags().StringVarP(&format, "output", "o", "", "the output format to use")
prevPreRunE := cmd.PreRunE
cmd.PreRunE = func(cmd *cobra.Command, args []string) error {
if format == "" {
format = defaultValue
}
for _, ef := range extraFormatters {
for k, v := range ef {
formatters[k] = v
}
}
if prevPreRunE != nil {
if err := prevPreRunE(cmd, args); err != nil {
return err
}
}
if _, ok := formatters[format]; !ok {
return fmt.Errorf("%q is not a known output format, use one of: %v", format, knownFormatters())
}
return nil
}
return nil
}
Loading

0 comments on commit a71b827

Please sign in to comment.