Skip to content

Commit

Permalink
Convert netbackup to use pflag.
Browse files Browse the repository at this point in the history
- The change to pflag allows for cleaner use of multiple string
  config files (upcoming change).
- Also removed flag.go and moved the flag parsing routines to main.go.
  • Loading branch information
marcopaganini committed Oct 6, 2023
1 parent b1dc0e3 commit e232878
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 80 deletions.
79 changes: 0 additions & 79 deletions flags.go

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ go 1.13
require (
github.com/BurntSushi/toml v0.3.1
github.com/marcopaganini/logger v0.1.2
github.com/spf13/pflag v1.0.5 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/marcopaganini/logger v0.1.2 h1:K4uStpilfOjBnaWLmha6vqXnWPbVAj8znYpbtRd1Dgw=
github.com/marcopaganini/logger v0.1.2/go.mod h1:T/hVVIfV/lgkMPXyGzzGo86fC83BgltnNX0FMvIAlu4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
44 changes: 43 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/marcopaganini/logger"
"github.com/marcopaganini/netbackup/config"
"github.com/spf13/pflag"
)

const (
Expand All @@ -40,10 +41,51 @@ var (
// Build is filled by go build -ldflags during build.
Build string

// Generic logging object
// Generic logging object.
log *logger.Logger

// Command-line options.
opt struct {
config string
dryrun bool
help bool
verbose int
version bool
}
)

// Returns a formatted error message including the program's usage.
func usage() {
fmt.Printf("netbackup version %s\n\n", Build)
fmt.Printf("Usage %s:\n", os.Args[0])
pflag.PrintDefaults()
fmt.Println("")
}

// Parse the command line and set the global opt variable. Return error if the
// basic sanity checking of flags fails.
func parseFlags() error {
// Parse command line
pflag.StringVarP(&opt.config, "config", "c", "", "Config File")
pflag.BoolVarP(&opt.dryrun, "dry-run", "n", false, "Dry-run mode")
pflag.BoolVarP(&opt.dryrun, "help", "h", false, "Quick help")
pflag.CountVarP(&opt.verbose, "verbose", "v", "Verbose mode (use multiple times to increase level)")
pflag.BoolVarP(&opt.version, "version", "V", false, "Show version (build) number and exit")
pflag.Parse()

// Help
if opt.help {
usage()
}

// Config is mandatory
if opt.config == "" && !opt.version {
usage()
return fmt.Errorf("Configuration file must be specified with --config=config_filename")
}
return nil
}

// logPath constructs the name for the output log using the the name and
// the current system date.
func logPath(name string, logDir string) string {
Expand Down

0 comments on commit e232878

Please sign in to comment.