Skip to content

Commit

Permalink
Initiated integration of configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbn committed Dec 7, 2022
1 parent 6ac24be commit b835119
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 49 deletions.
6 changes: 6 additions & 0 deletions examples/stevedore-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "../stevedore-config.schema.json",
"exclusion": false,
"verbosity": true,
"color inversion": true
}
127 changes: 78 additions & 49 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
Expand All @@ -20,18 +21,52 @@ var (
includedColor = color.FgHiRed
)

type config struct {
Verbose bool
Debug bool
Color bool
Nocolor bool
Ignorefile string
Excluded bool
Included bool
Invertcolors bool
}

// main function is a wrapper on the realMain function and emits OS exit code based on wrapped function
func main() {
os.Exit(realMain())
}

func realMain() int {

var config config
configFile := ".stevedore.json"

if _, err := os.Stat(configFile); !errors.Is(err, fs.ErrNotExist) {
if config.Debug {
fmt.Println("stevedore configuration file found")
}
jsonData, err := os.ReadFile(configFile)

if err != nil {
fmt.Printf("unable to read %s file, ignoring - %v\n", configFile, err)
}

err = json.Unmarshal(jsonData, &config)
if err != nil {
fmt.Println("error unmarshalling JSON configuration:", err)
}

} else {
if config.Debug {
fmt.Println("No stevedore configuration file found")
}
}

flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)

var verbose bool
flag.BoolVar(&verbose, "verbose", false, "emit verbose output")
flag.BoolVar(&verbose, "v", false, "emit verbose output")
flag.BoolVar(&config.Verbose, "verbose", false, "emit verbose output")
flag.BoolVar(&config.Verbose, "v", false, "emit verbose output")

var debug bool
flag.BoolVar(&debug, "debug", false, "emit debug messages")
Expand All @@ -40,27 +75,21 @@ func realMain() int {
flag.BoolVar(&stdin, "stdin", false, "read from ignore file from STDIN")
flag.BoolVar(&stdin, "s", false, "read from ignore file from STDIN")

var colorOutput bool
flag.BoolVar(&colorOutput, "color", true, "enable colors")
flag.BoolVar(&colorOutput, "c", true, "enable colors")
flag.BoolVar(&config.Color, "color", true, "enable colors")
flag.BoolVar(&config.Color, "c", true, "enable colors")

var nocolorOutput bool
flag.BoolVar(&nocolorOutput, "nocolor", false, "disable use of colors")
flag.BoolVar(&nocolorOutput, "n", false, "disable use of colors")
flag.BoolVar(&config.Nocolor, "nocolor", false, "disable use of colors")
flag.BoolVar(&config.Nocolor, "n", false, "disable use of colors")

var ignoreFile string
flag.StringVar(&ignoreFile, "ignorefile", "", "a path to an specific ignore file")
flag.StringVar(&ignoreFile, "i", "", "a path to an specific ignore file")
flag.StringVar(&config.Ignorefile, "ignorefile", "", "a path to an specific ignore file")
flag.StringVar(&config.Ignorefile, "i", "", "a path to an specific ignore file")

var excluded bool
flag.BoolVar(&excluded, "excluded", false, "only output excluded files")
flag.BoolVar(&excluded, "x", false, "only output excluded files")
flag.BoolVar(&config.Excluded, "excluded", false, "only output excluded files")
flag.BoolVar(&config.Excluded, "x", false, "only output excluded files")

var included bool
flag.BoolVar(&included, "included", false, "only output included files")
flag.BoolVar(&config.Included, "included", false, "only output included files")

var colorOutputInverted bool
flag.BoolVar(&colorOutputInverted, "invertcolors", false, "inverts the used color")
flag.BoolVar(&config.Invertcolors, "invertcolors", false, "inverts the used color")

nocolorEnv := os.Getenv("NO_COLOR")

Expand Down Expand Up @@ -95,37 +124,37 @@ func realMain() int {

} else {

if ignoreFile == "" {
ignoreFile = path + "/.dockerignore"
if config.Ignorefile == "" {
config.Ignorefile = path + "/.dockerignore"

if debug {
fmt.Println("path: ", path)
fmt.Println("ignoreFile: ", ignoreFile)
fmt.Println("ignoreFile: ", config.Ignorefile)
}
}
}

if excluded {
included = false
if config.Excluded {
config.Included = false
}

if included {
excluded = false
if config.Included {
config.Excluded = false
}

if !included && !excluded {
included = true
excluded = true
if !config.Included && !config.Excluded {
config.Included = true
config.Excluded = true
}

if debug {
fmt.Println("color: ", colorOutput)
fmt.Println("nocolor: ", nocolorOutput)
fmt.Println("ignoreFile: ", ignoreFile)
fmt.Println("color: ", config.Color)
fmt.Println("nocolor: ", config.Nocolor)
fmt.Println("ignoreFile: ", config.Ignorefile)
fmt.Println("debug: ", debug)
fmt.Println("verbose: ", verbose)
fmt.Println("excluded: ", excluded)
fmt.Println("included: ", included)
fmt.Println("verbose: ", config.Verbose)
fmt.Println("excluded: ", config.Excluded)
fmt.Println("included: ", config.Included)
fmt.Println("tail: ", flag.Args())
fmt.Println("ENV: ", nocolorEnv)
}
Expand All @@ -136,20 +165,20 @@ func realMain() int {
ignoreObject = ignore.CompileIgnoreLines(ignoreLines)
} else {
var err error
ignoreObject, err = ignore.CompileIgnoreFile(ignoreFile)
ignoreObject, err = ignore.CompileIgnoreFile(config.Ignorefile)

if err != nil {
log.Fatalf("unable to read %s file", ignoreFile)
log.Fatalf("unable to read %s file", config.Ignorefile)
return 1
}
}

if nocolorOutput || nocolorEnv != "" || nocolorEnv == "1" {
colorOutput = false
colorOutputInverted = false
if config.Nocolor || nocolorEnv != "" || nocolorEnv == "1" {
config.Color = false
config.Invertcolors = false
}

if colorOutputInverted {
if config.Invertcolors {
tmpColor := ignoredColor
ignoredColor = includedColor
includedColor = tmpColor
Expand Down Expand Up @@ -184,35 +213,35 @@ func realMain() int {
return err
}
if ownIgnoreObject.MatchesPath(path) && info.IsDir() {
if verbose {
if config.Verbose {
fmt.Printf("%s have been ignored by stevedore, no traversal\n", info.Name())
}
return filepath.SkipDir
} else if ownIgnoreObject.MatchesPath(path) {
if verbose {
if config.Verbose {
fmt.Printf("%s have been ignored by stevedore\n", info.Name())
}
return nil
}

if ignoreObject.MatchesPath(path) {
if excluded {
if colorOutput {
if config.Excluded {
if config.Color {
color.Set(ignoredColor)
}
if verbose {
if config.Verbose {
fmt.Printf("path %s ignored and is not included in Docker image\n", info.Name())
} else {
fmt.Printf("%s\n", info.Name())
}
color.Unset()
}
} else {
if included {
if colorOutput {
if config.Included {
if config.Color {
color.Set(includedColor)
}
if verbose {
if config.Verbose {
fmt.Printf("path %s not ignored and is included in Docker image\n", info.Name())
} else {
fmt.Printf("%s\n", info.Name())
Expand Down
42 changes: 42 additions & 0 deletions stevedore-config.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/jonasbn/stevedore/stevedore-config.schema.json",
"title": "stevedore-config",
"description": "Configuration for stevedore",
"type": "object",
"properties": {
"ignorefile": {
"description": "path to a global ignore file",
"type": "string"
},
"exclusion": {
"description": "enable exclusion output",
"type": "boolean"
},
"inclusion": {
"description": "enable inclusion output",
"type": "boolean"
},
"colors": {
"description": "color palette",
"type": "string"
},
"color output": {
"description": "enabled colorized output",
"type": "boolean"
},
"color inversion": {
"description": "invert the colorized output",
"type": "boolean"
},
"verbosity": {
"description": "enable verbose output",
"type": "boolean"
},
"debug": {
"description": "enable debug output",
"type": "boolean"
}
},
"required": []
}

0 comments on commit b835119

Please sign in to comment.