-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
66c2fc9
commit 14b18f7
Showing
12 changed files
with
464 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
dockerfile-inspector | ||
config.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FROM ubuntu:latest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FROM ubuntu:latest | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
# Dockerfile Inspector | ||
|
||
## Dockerfile Inspector |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package rule | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package rules | ||
|