Skip to content

Commit

Permalink
Merge pull request #25 from jonasbn/issue11
Browse files Browse the repository at this point in the history
issue 11 - configuration file support
  • Loading branch information
jonasbn committed Feb 21, 2023
2 parents ae4f1d6 + 84d93a2 commit a38a9e5
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 59 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
}
146 changes: 87 additions & 59 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
Fullpath bool
Nofullpath 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,34 +75,26 @@ 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")

var fullPath bool
flag.BoolVar(&fullPath, "fullpath", true, "emits files and directories with full path")
flag.BoolVar(&fullPath, "f", true, "emits files and directories with full path")
flag.BoolVar(&config.Fullpath, "fullpath", true, "emits files and directories with full path")
flag.BoolVar(&config.Fullpath, "f", true, "emits files and directories with full path")

var noFullPath bool
flag.BoolVar(&noFullPath, "nofullpath", false, "emits files and directories without full path")
flag.BoolVar(&config.Nofullpath, "nofullpath", false, "emits files and directories without full path")

nocolorEnv := os.Getenv("NO_COLOR")

Expand Down Expand Up @@ -100,37 +127,38 @@ func realMain() int {
fmt.Printf("ignorelines: \n%s\n\n", ignoreLines)
}

} else if ignoreFile == "" {
ignoreFile = path + "/.dockerignore"
} else 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("fullpath: ", fullPath)
fmt.Println("verbose: ", config.Verbose)
fmt.Println("excluded: ", config.Excluded)
fmt.Println("included: ", config.Included)
fmt.Println("fullpath: ", config.Fullpath)
fmt.Println("nofullpath: ", config.Nofullpath)
fmt.Println("tail: ", flag.Args())
fmt.Println("ENV: ", nocolorEnv)
}
Expand All @@ -141,25 +169,25 @@ 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 {
ignoredColor, includedColor = includedColor, ignoredColor
}

if noFullPath {
fullPath = false
if config.Nofullpath {
config.Fullpath = false
}

var err error
Expand Down Expand Up @@ -189,50 +217,50 @@ 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
}

var entry string

if fullPath {
if config.Fullpath {
entry = path
} else {
entry = info.Name()
}

if path == "." {
if verbose {
if config.Verbose {
fmt.Printf("%s is ignored, but traversed by stevedore by default\n", entry)
}
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", entry)
} else {
fmt.Printf("%s\n", entry)
}
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", entry)
} else {
fmt.Printf("%s\n", entry)
Expand Down
50 changes: 50 additions & 0 deletions stevedore-config.schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"$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"
},
"fullpath": {
"description": "enable full path of encountered files and directories",
"type": "boolean"
},
"nofullpath": {
"description": "disable full path of encountered files and directories",
"type": "boolean"
}
},
"required": []
}

0 comments on commit a38a9e5

Please sign in to comment.