"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "go/pkg/conf/GetArgs.go" between
Netspoc-6.025.tar.gz and Netspoc-6.026.tar.gz

About: NetSPoC is a network security policy compiler (using its own description language) to manage all the packet filter devices inside your network topology.

GetArgs.go  (Netspoc-6.025):GetArgs.go  (Netspoc-6.026)
skipping to change at line 29 skipping to change at line 29
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License along You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc., with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
import ( import (
"fmt" "fmt"
"github.com/hknutzen/Netspoc/go/pkg/diag"
"github.com/hknutzen/Netspoc/go/pkg/fileop" "github.com/hknutzen/Netspoc/go/pkg/fileop"
"github.com/octago/sflags" "github.com/octago/sflags"
"github.com/octago/sflags/gen/gpflag" "github.com/octago/sflags/gen/gpflag"
flag "github.com/spf13/pflag" flag "github.com/spf13/pflag"
"io/ioutil" "io/ioutil"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
skipping to change at line 104 skipping to change at line 105
CheckUnusedOwners TriState CheckUnusedOwners TriState
CheckUnusedProtocols TriState CheckUnusedProtocols TriState
AutoDefaultRoute bool AutoDefaultRoute bool
ConcurrencyPass1 int ConcurrencyPass1 int
ConcurrencyPass2 int ConcurrencyPass2 int
IgnoreFiles *regexp.Regexp IgnoreFiles *regexp.Regexp
IPV6 bool `flag:"ipv6 6"` IPV6 bool `flag:"ipv6 6"`
MaxErrors int `flag:"max_errors m"` MaxErrors int `flag:"max_errors m"`
Verbose bool `flag:"verbose v"` Verbose bool `flag:"verbose v"`
TimeStamps bool `flag:"time_stamps t"` TimeStamps bool `flag:"time_stamps t"`
StartTime int64
Pipe bool
} }
type invertedFlag map[string]*struct { type invertedFlag map[string]*struct {
short string short string
orig string orig string
} }
var invertedFlags = invertedFlag{ var invertedFlags = invertedFlag{
"quiet": {short: "q", orig: "verbose"}, "quiet": {short: "q", orig: "verbose"},
} }
skipping to change at line 192 skipping to change at line 191
// Abort after this many errors. // Abort after this many errors.
MaxErrors: 10, MaxErrors: 10,
// Print progress messages. // Print progress messages.
Verbose: true, Verbose: true,
// Print progress messages with time stamps. // Print progress messages with time stamps.
// Print "finished" with time stamp when finished. // Print "finished" with time stamp when finished.
TimeStamps: false, TimeStamps: false,
// Use this value when printing passed time span.
StartTime: 0,
// Pass 1 writes processed device names to STDOUT,
// pass 2 reads to be processed device names from STDIN.
Pipe: false,
} }
err := gpflag.ParseTo(cfg, fs, sflags.FlagDivider("_")) err := gpflag.ParseTo(cfg, fs, sflags.FlagDivider("_"))
if err != nil { if err != nil {
panic(err) panic(err)
} }
for name, spec := range invertedFlags { for name, spec := range invertedFlags {
origFlag := fs.Lookup(spec.orig) origFlag := fs.Lookup(spec.orig)
inverted := invFlag{origFlag} inverted := invFlag{origFlag}
flag := fs.VarPF(inverted, name, spec.short, "") flag := fs.VarPF(inverted, name, spec.short, "")
flag.NoOptDefVal = "true" flag.NoOptDefVal = "true"
} }
return cfg return cfg
} }
func showErr(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, "Error: "+format+"\n", args...)
}
func usage(format string, args ...interface{}) { func usage(format string, args ...interface{}) {
showErr(format, args...) diag.Err(format, args...)
flag.Usage() flag.Usage()
} }
// Read names of input file/directory and output directory from // Read names of input file/directory and output directory from
// passed command line arguments. // passed command line arguments.
func parseArgs(fs *flag.FlagSet) (string, string, bool) { func parseArgs(fs *flag.FlagSet) (string, string, bool) {
mainFile := fs.Arg(0) mainFile := fs.Arg(0)
if mainFile == "" || fs.Arg(2) != "" { if mainFile == "" || fs.Arg(2) != "" {
usage("Expected 2 args, got %v", fs.Args()) usage("Expected 2 args, got %v", fs.Args())
return "", "", true return "", "", true
skipping to change at line 244 skipping to change at line 232
// If outDir is missing, no code is generated. // If outDir is missing, no code is generated.
outDir := fs.Arg(1) outDir := fs.Arg(1)
// Strip trailing slash for nicer messages. // Strip trailing slash for nicer messages.
strings.TrimSuffix(mainFile, "/") strings.TrimSuffix(mainFile, "/")
strings.TrimSuffix(outDir, "/") strings.TrimSuffix(outDir, "/")
return mainFile, outDir, false return mainFile, outDir, false
} }
// Reads "key = value;" pairs from config file. // Reads "key = value;" pairs from config file.
// "key;" is read as "key = ;"
// Trailing ";" is optional. // Trailing ";" is optional.
// Comment lines starting with "#" are ignored. // Comment lines starting with "#" are ignored.
func readConfig(filename string) (map[string]string, bool) { func readConfig(filename string) (map[string]string, error) {
bytes, err := ioutil.ReadFile(filename) bytes, err := ioutil.ReadFile(filename)
if err != nil { if err != nil {
showErr("%v", err) return nil, err
return nil, true
} }
lines := strings.Split(string(bytes), "\n") lines := strings.Split(string(bytes), "\n")
result := make(map[string]string) result := make(map[string]string)
for _, line := range lines { for _, line := range lines {
trimmed := strings.TrimSpace(line) line := strings.TrimSpace(line)
if trimmed == "" || trimmed[0] == '#' { line = strings.TrimSuffix(line, ";")
if line == "" || line[0] == '#' {
continue continue
} }
parts := strings.Split(line, "=") parts := strings.SplitN(line, "=", 2)
if len(parts) != 2 { key := parts[0]
showErr("Unexpected line in %s: %s", filename, line) val := ""
return nil, true if len(parts) == 2 {
val = parts[1]
} }
key, val := parts[0], parts[1]
key = strings.TrimSpace(key) key = strings.TrimSpace(key)
val = strings.TrimSpace(val) val = strings.TrimSpace(val)
val = strings.TrimSuffix(val, ";")
result[key] = val result[key] = val
} }
return result, false return result, nil
} }
// parseFile parses the specified configuration file and populates unset flags // parseFile parses the specified configuration file and populates unset flags
// in fs based on the contents of the file. // in fs based on the contents of the file.
// Hidden flags are not set from file. // Hidden flags are not set from file.
func parseFile(filename string, fs *flag.FlagSet) bool { func parseFile(filename string, fs *flag.FlagSet) error {
isSet := make(map[*flag.Flag]bool) isSet := make(map[*flag.Flag]bool)
config, abort := readConfig(filename) config, err := readConfig(filename)
if abort { if err != nil {
return abort return err
} }
fs.Visit(func(f *flag.Flag) { fs.Visit(func(f *flag.Flag) {
isSet[f] = true isSet[f] = true
}) })
hasErr := false var errList []string
addErr := func(format string, args ...interface{}) {
errList = append(errList, fmt.Sprintf(format, args...))
}
fs.VisitAll(func(f *flag.Flag) { fs.VisitAll(func(f *flag.Flag) {
// Ignore inverted flag. // Ignore inverted flag.
if inv, found := invertedFlags[f.Name]; found { if inv, found := invertedFlags[f.Name]; found {
if isSet[f] { if isSet[f] {
// Ignore inverted value from file. // Ignore inverted value from file.
delete(config, inv.orig) delete(config, inv.orig)
} }
return return
} }
val, found := config[f.Name] val, found := config[f.Name]
if !found { if !found {
return return
} }
delete(config, f.Name) delete(config, f.Name)
if isSet[f] { if isSet[f] {
return return
} }
err := f.Value.Set(val) if err := f.Value.Set(val); err != nil {
if err != nil { addErr("bad value in '%s = %s'", f.Name, val)
hasErr = true
showErr("Invalid value for %s in %s: %s", f.Name, filenam
e, val)
} }
}) })
for name := range config { for name := range config {
showErr("Invalid keyword in %s: %s", filename, name) addErr("bad keyword '%s'", name)
hasErr = true }
if errList != nil {
return fmt.Errorf("Invalid line in %s:\n - %s",
filename, strings.Join(errList, "\n - "))
} }
return hasErr return nil
} }
func addConfigFromFile(inDir string, fs *flag.FlagSet) bool { func addConfigFromFile(inDir string, fs *flag.FlagSet) error {
path := inDir + "/config" path := inDir + "/config"
if !fileop.IsRegular(path) { if !fileop.IsRegular(path) {
return false return nil
} }
return parseFile(path, fs) return parseFile(path, fs)
} }
func setStartTime() { func setStartTime() {
if Conf.StartTime != 0 { StartTime = time.Now()
StartTime = time.Unix(Conf.StartTime, 0)
} else {
StartTime = time.Now()
}
} }
var Conf *Config var Conf *Config
var StartTime time.Time var StartTime time.Time
func GetArgs() (string, string, bool) { func GetArgs() (string, string, bool) {
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError) fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
// Setup custom usage function. // Setup custom usage function.
flag.Usage = func() { flag.Usage = func() {
skipping to change at line 359 skipping to change at line 347
if err == flag.ErrHelp { if err == flag.ErrHelp {
return "", "", true return "", "", true
} }
usage("%v", err) usage("%v", err)
return "", "", true return "", "", true
} }
inPath, outDir, abort := parseArgs(fs) inPath, outDir, abort := parseArgs(fs)
if abort { if abort {
return "", "", true return "", "", true
} }
if abort := addConfigFromFile(inPath, fs); abort { if err := addConfigFromFile(inPath, fs); err != nil {
diag.Err("%v", err)
return "", "", true return "", "", true
} }
setStartTime() setStartTime()
return inPath, outDir, false return inPath, outDir, false
} }
func ConfigFromArgsAndFile(args []string, path string) { func ConfigFromArgsAndFile(args []string, path string) {
fs := flag.NewFlagSet("", flag.ExitOnError) fs := flag.NewFlagSet("", flag.ExitOnError)
Conf = defaultOptions(fs) Conf = defaultOptions(fs)
fs.Parse(args) // No check for error needed, because arguments are fixed. // No check for error needed, because arguments are fixed.
fs.Parse(args)
// Ignore errors, only pass1 needs to check them.
addConfigFromFile(path, fs) addConfigFromFile(path, fs)
setStartTime() setStartTime()
} }
 End of changes. 24 change blocks. 
48 lines changed or deleted 38 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)