Skip to content

Commit

Permalink
Added initial code
Browse files Browse the repository at this point in the history
  • Loading branch information
iamabhishek-dubey committed Nov 9, 2020
1 parent 66c2fc9 commit 14b18f7
Show file tree
Hide file tree
Showing 12 changed files with 464 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dockerfile-inspector
config.yaml
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM ubuntu:latest

2 changes: 2 additions & 0 deletions Dockerfile.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FROM ubuntu:latest

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
# Dockerfile Inspector

## Dockerfile Inspector
28 changes: 28 additions & 0 deletions cmd/output.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package cmd

import (
"bufio"
"dockerfile-inspector/pkg/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)

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

var outputCmd = &cobra.Command{
Use: "output",
Short: "Runs dockerfile-inspector to generate ouput in stdout",
Long: `Runs dockerfile-inspector to generate ouput in stdout`,
Run: func(cmd *cobra.Command, args []string) {
dockerFileContent, err := os.Open(dockerFilePath)
if err != nil {
logrus.Errorf("Error parsing config at %s: %v", dockerFilePath, err)
os.Exit(1)
}
content := bufio.NewScanner(dockerFileContent)
utils.DockerFileCheckStatus("FROM .*:latest", content)
},
}
70 changes: 70 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package cmd

import (
"flag"
"os"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var (
dockerFilePath string
logLevel string
logFmt string
version string
dockerFileContent *os.File
)

func init() {
rootCmd.PersistentFlags().StringVarP(&dockerFilePath, "docker.file", "d", "Dockerfile", "Location of Dockerfile.")
rootCmd.PersistentFlags().StringVarP(&logLevel, "log.level", "", logrus.InfoLevel.String(), "dockerfile-inspector logging level.")
rootCmd.PersistentFlags().StringVarP(&logFmt, "log.format", "", "text", "dockerfile-inspector log format.")
flag.Parse()
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
}

var rootCmd = &cobra.Command{
Use: "dockerfile-inspector",
Short: "dockerfile-inspector",
Long: `A tool for checking Dockerfile best practices.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
parsedLevel, err := logrus.ParseLevel(logLevel)
if err != nil {
logrus.Errorf("log-level flag has invalid value %s", logLevel)
} else {
logrus.SetLevel(parsedLevel)
}
if logFmt == "json" {
logrus.SetFormatter(&logrus.JSONFormatter{})
} else {
logrus.SetFormatter(&logrus.TextFormatter{
DisableColors: true,
FullTimestamp: true,
})
}
dockerFileContent, err = os.Open(dockerFilePath)
if err != nil {
logrus.Errorf("Error parsing config at %s: %v", dockerFilePath, err)
os.Exit(1)
}

},
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
if err != nil {
logrus.Error(err)
}
os.Exit(1)
},
}

// Execute the stuff
func Execute(VERSION string) {
version = VERSION
if err := rootCmd.Execute(); err != nil {
logrus.Error(err)
os.Exit(1)
}
}
21 changes: 21 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

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

var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the current version.",
Long: `Prints the current version of dockerfile-inspector.`,
Run: func(cmd *cobra.Command, args []string) {
versionOutput := fmt.Sprintf("dockerfile-inspector %s", version)
fmt.Println(versionOutput)
},
}
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module dockerfile-inspector

go 1.15

require (
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5
)
302 changes: 302 additions & 0 deletions go.sum

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions pkg/utils/regex_checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package utils

import (
"bufio"
"fmt"
log "github.com/sirupsen/logrus"
"regexp"
)

// DockerFileCheckStatus will return the status of check
func DockerFileCheckStatus(check string, content *bufio.Scanner) {
r, err := regexp.Compile(check)
if err != nil {
log.Errorf("Unable to compile the regular expression: %v", err)
}
line := 1
for content.Scan() {
if r.MatchString(content.Text()) {
fmt.Println(content.Text(), line)
}
line++
}
}
2 changes: 2 additions & 0 deletions rules/images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package rule

2 changes: 2 additions & 0 deletions rules/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package rules

0 comments on commit 14b18f7

Please sign in to comment.