Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert netbackup to use pflag. #2

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading