Fix flag parsing after the configuration file

Fixes #52
This commit is contained in:
Mariano Cano 2019-03-18 12:38:19 -07:00
parent 8a05cdde52
commit 6592c4784b

View file

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -143,40 +144,12 @@ intermediate private key.`,
} }
app.Action = func(ctx *cli.Context) error { app.Action = func(ctx *cli.Context) error {
passFile := ctx.String("password-file") // Hack to be able to run a the top action as a subcommand
cmd := cli.Command{Name: "start", Action: startAction, Flags: app.Flags}
// If zero cmd line args show help, if >1 cmd line args show error. set := flag.NewFlagSet(app.Name, flag.ContinueOnError)
if ctx.NArg() == 0 { set.Parse(os.Args)
return cli.ShowAppHelp(ctx) ctx = cli.NewContext(app, set, nil)
} return cmd.Run(ctx)
if err := errs.NumberOfArguments(ctx, 1); err != nil {
return err
}
configFile := ctx.Args().Get(0)
config, err := authority.LoadConfiguration(configFile)
if err != nil {
fatal(err)
}
var password []byte
if passFile != "" {
if password, err = ioutil.ReadFile(passFile); err != nil {
fatal(errors.Wrapf(err, "error reading %s", passFile))
}
password = bytes.TrimRightFunc(password, unicode.IsSpace)
}
srv, err := ca.New(config, ca.WithConfigFile(configFile), ca.WithPassword(password))
if err != nil {
fatal(err)
}
go ca.StopReloaderHandler(srv)
if err = srv.Run(); err != nil && err != http.ErrServerClosed {
fatal(err)
}
return nil
} }
if err := app.Run(os.Args); err != nil { if err := app.Run(os.Args); err != nil {
@ -189,6 +162,43 @@ intermediate private key.`,
} }
} }
func startAction(ctx *cli.Context) error {
passFile := ctx.String("password-file")
// If zero cmd line args show help, if >1 cmd line args show error.
if ctx.NArg() == 0 {
return cli.ShowAppHelp(ctx)
}
if err := errs.NumberOfArguments(ctx, 1); err != nil {
return err
}
configFile := ctx.Args().Get(0)
config, err := authority.LoadConfiguration(configFile)
if err != nil {
fatal(err)
}
var password []byte
if passFile != "" {
if password, err = ioutil.ReadFile(passFile); err != nil {
fatal(errors.Wrapf(err, "error reading %s", passFile))
}
password = bytes.TrimRightFunc(password, unicode.IsSpace)
}
srv, err := ca.New(config, ca.WithConfigFile(configFile), ca.WithPassword(password))
if err != nil {
fatal(err)
}
go ca.StopReloaderHandler(srv)
if err = srv.Run(); err != nil && err != http.ErrServerClosed {
fatal(err)
}
return nil
}
// fatal writes the passed error on the standard error and exits with the exit // fatal writes the passed error on the standard error and exits with the exit
// code 1. If the environment variable STEPDEBUG is set to 1 it shows the // code 1. If the environment variable STEPDEBUG is set to 1 it shows the
// stack trace of the error. // stack trace of the error.