Skip to content

Commit

Permalink
remove deprecated command line options (-version and -once)
Browse files Browse the repository at this point in the history
use subcommand instead
  • Loading branch information
Songmu committed Feb 20, 2016
1 parent 87375ec commit b608c88
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 79 deletions.
63 changes: 9 additions & 54 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ func (r *roleFullnamesFlag) Set(input string) error {
return nil
}

type otherOptions struct {
printVersion bool
runOnce bool
}

var logger = logging.GetLogger("main")

const (
Expand Down Expand Up @@ -72,34 +67,23 @@ func doVersion(_ []string) int {
}

func doConfigtest(argv []string) int {
conf, otherOpts := resolveConfig(argv)
if conf == nil || otherOpts != nil {
conf := resolveConfig(argv)
if conf == nil {
return exitStatusError
}
fmt.Fprintf(os.Stderr, "%s Syntax OK\n", conf.Conffile)
return exitStatusOK
}

func doMain(argv []string) int {
conf, otherOpts := resolveConfig(argv)
conf := resolveConfig(argv)
if conf == nil {
return exitStatusError
}
if otherOpts != nil && otherOpts.printVersion {
return doVersion([]string{})
}

if conf.Verbose {
logging.SetLogLevel(logging.DEBUG)
}

logger.Infof("Starting mackerel-agent version:%s, rev:%s, apibase:%s", version.VERSION, version.GITCOMMIT, conf.Apibase)

if otherOpts != nil && otherOpts.runOnce {
command.RunOnce(conf)
return exitStatusOK
}

return start(conf)
}

Expand Down Expand Up @@ -143,7 +127,7 @@ func doRetire(argv []string) int {
func doOnce(argv []string) int {
// dirty hack `resolveConfig` required apikey so fill up
argvOpt := append(argv, "-apikey=dummy")
conf, _ := resolveConfig(argvOpt)
conf := resolveConfig(argvOpt)
if conf == nil {
return exitStatusError
}
Expand Down Expand Up @@ -185,28 +169,17 @@ func resolveConfigForRetire(argv []string) (*config.Config, bool, error) {
}
optArgs = append(optArgs, v)
}
conf, otherOpts := resolveConfig(optArgs)
conf := resolveConfig(optArgs)
if conf == nil {
printRetireUsage()
}

if otherOpts != nil {
msg := "can't use -vesion/-once option in retire"
logger.Errorf(msg)
return nil, isForce, fmt.Errorf(msg)
}

return conf, isForce, nil
}

// resolveConfig parses command line arguments and loads config file to
// return config.Config information.
// As a special case, if `-version` flag is given it stops processing
// and return true for the second return value.
func resolveConfig(argv []string) (*config.Config, *otherOptions) {
func resolveConfig(argv []string) *config.Config {
conf := &config.Config{}
otherOptions := &otherOptions{}

fs := flag.NewFlagSet("mackerel-agent", flag.ExitOnError)

var (
Expand All @@ -226,30 +199,13 @@ func resolveConfig(argv []string) (*config.Config, *otherOptions) {
// but we call it "role" here for ease.
fs.Var(&roleFullnames, "role", "Set this host's roles (format: <service>:<role>)")

// flags for otherOpts
var (
runOnce = fs.Bool("once", false, "(DEPRECATED) Show spec and metrics to stdout once")
printVersion = fs.Bool("version", false, "(DEPRECATED) Prints version and exit")
)
fs.Parse(argv)

if *printVersion {
otherOptions.printVersion = true
logger.Warningf("-version option is deprecated. use subcommand (`%% mackerel-agent version`) instead")
return conf, otherOptions
}

if *runOnce {
otherOptions.runOnce = true
logger.Warningf("-once option is deprecated. use subcommand (`%% mackerel-agent once`) instead")
return conf, otherOptions
}

conf, confErr := config.LoadConfig(*conffile)
conf.Conffile = *conffile
if confErr != nil {
logger.Criticalf("Failed to load the config file: %s", confErr)
return nil, nil
return nil
}

// overwrite config from file by config from args
Expand All @@ -258,7 +214,6 @@ func resolveConfig(argv []string) (*config.Config, *otherOptions) {
case "apibase":
conf.Apibase = *apibase
case "apikey":
logger.Warningf("-apikey option is deprecated. use config file instead")
conf.Apikey = *apikey
case "pidfile":
conf.Pidfile = *pidfile
Expand All @@ -285,9 +240,9 @@ func resolveConfig(argv []string) (*config.Config, *otherOptions) {

if conf.Apikey == "" {
logger.Criticalf("Apikey must be specified in the command-line flag or in the config file")
return nil, nil
return nil
}
return conf, nil
return conf
}

func createPidFile(pidfile string) error {
Expand Down
26 changes: 1 addition & 25 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ diagnostic=false
confFile.Sync()
confFile.Close()
defer os.Remove(confFile.Name())
mergedConfig, _ := resolveConfig([]string{"-conf=" + confFile.Name(), "-role=My-Service:default,INVALID#SERVICE", "-verbose", "-diagnostic"})
mergedConfig := resolveConfig([]string{"-conf=" + confFile.Name(), "-role=My-Service:default,INVALID#SERVICE", "-verbose", "-diagnostic"})

t.Logf(" apibase: %v", mergedConfig.Apibase)
t.Logf(" apikey: %v", mergedConfig.Apikey)
Expand All @@ -57,30 +57,6 @@ diagnostic=false
}
}

func TestParseFlagsPrintVersion(t *testing.T) {
config, otherOptions := resolveConfig([]string{"-version"})

if config.Verbose != false {
t.Error("with -version args, variables of config should have default values")
}

if otherOptions.printVersion == false {
t.Error("with -version args, printVersion should be true")
}
}

func TestParseFlagsRunOnce(t *testing.T) {
config, otherOptions := resolveConfig([]string{"-once"})

if config.Verbose != false {
t.Error("with -version args, variables of config should have default values")
}

if otherOptions.runOnce == false {
t.Error("with -once args, RunOnce should be true")
}
}

func TestDetectForce(t *testing.T) {
// prepare dummy config
confFile, err := ioutil.TempFile("", "mackerel-config-test")
Expand Down

0 comments on commit b608c88

Please sign in to comment.