Browse Source

Added logging library, config and scaffold for dockerfile parser

master
Drew Short 3 years ago
parent
commit
c8d3021fb0
  1. 49
      cmd/root.go
  2. 15
      go.mod
  3. 1181
      go.sum
  4. 30
      internal/parser/dockerfile.go
  5. 18
      internal/parser/parser.go
  6. 4
      main.go

49
cmd/root.go

@ -18,14 +18,32 @@ package cmd
import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/thediveo/enumflag"
"os"
"github.com/spf13/viper"
)
// https://pkg.go.dev/github.com/thediveo/enumflag#section-readme
// Custom enum for supported log output types
type LogFormat enumflag.Flag
const (
TEXT LogFormat = iota
JSON
)
var LogFormatIds = map[LogFormat][]string{
TEXT: {"text"},
JSON: {"json"},
}
var configFile string
var remoteURL string
var logVerbosity int
var logFormat LogFormat
// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
@ -51,7 +69,7 @@ func Execute() {
}
func init() {
cobra.OnInitialize(initConfig)
cobra.OnInitialize(initConfig, initLogging)
// Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here,
@ -59,6 +77,8 @@ func init() {
rootCmd.PersistentFlags().StringVar(&configFile, "config", "", "config file (default is $HOME/.pinned-package-updater.yaml OR ./.pinned-package-updater.yaml)")
rootCmd.PersistentFlags().StringVar(&remoteURL, "remote", "", "remote url for distributed mode (example: https://ppu.example.com/)")
rootCmd.PersistentFlags().CountVarP(&logVerbosity, "verbose", "v", "verbose (-v, or -vv)")
rootCmd.PersistentFlags().VarP(enumflag.New(&logFormat, "logFormat", LogFormatIds, enumflag.EnumCaseInsensitive), "format", "", "The logging format to use [text, json]")
// Cobra also supports local flags, which will only run
// when this action is called directly.
@ -94,3 +114,30 @@ func initConfig() {
_, _ = fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed())
}
}
func initLogging() {
switch verbosity := logVerbosity; {
case verbosity <= 0:
log.SetLevel(log.ErrorLevel)
case verbosity == 1:
log.SetLevel(log.InfoLevel)
case verbosity == 2:
log.SetLevel(log.DebugLevel)
case verbosity >= 3:
log.SetLevel(log.TraceLevel)
default:
log.SetLevel(log.ErrorLevel)
}
switch logFormat {
case TEXT:
log.SetFormatter(&log.TextFormatter{})
case JSON:
log.SetFormatter(&log.JSONFormatter{})
default:
log.SetFormatter(&log.TextFormatter{})
}
log.Debugf("Set the logging level to %s", log.GetLevel().String())
log.Debugf("Set the logging formatter to '%s'", LogFormatIds[logFormat][0])
}

15
go.mod

@ -3,21 +3,32 @@ module sothr.com/warricksothr/pinned-package-updater
go 1.17
require (
github.com/moby/buildkit v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/viper v1.8.1
github.com/thediveo/enumflag v0.10.1
)
require (
github.com/containerd/typeurl v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.2.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.8.1 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.5 // indirect
google.golang.org/protobuf v1.26.0 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

1181
go.sum
File diff suppressed because it is too large
View File

30
internal/parser/dockerfile.go

@ -0,0 +1,30 @@
/*
Copyright © 2021 Drew Short <warricks@sothr.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package parser implements the required components for working with image definitions
package parser
import (
"github.com/moby/buildkit/frontend/dockerfile/parser"
"os"
)
func parse(dockerfilePath string) {
dockerfile, err := os.Open(dockerfilePath)
defer dockerfile.Close()
parseResult, err := parser.Parse(dockerfile)
}

18
internal/parser/parser.go

@ -0,0 +1,18 @@
/*
Copyright © 2021 Drew Short <warricks@sothr.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package parser implements the required components for working with image definitions
package parser

4
main.go

@ -15,7 +15,9 @@ limitations under the License.
*/
package main
import "sothr.com/warricksothr/pinned-package-updater/cmd"
import (
"sothr.com/warricksothr/pinned-package-updater/cmd"
)
func main() {
cmd.Execute()

Loading…
Cancel
Save