grim/convey

aaf0c394ff02
Parents 2c339dc89014
Children 43e2260fa9f8
clean up the complexity of the main function
  • +127 -95
    main.go
  • --- a/main.go Sun Jan 14 04:39:16 2018 -0600
    +++ b/main.go Sun Jan 14 05:12:00 2018 -0600
    @@ -51,19 +51,21 @@
    dockerConfig = app.Flag("docker-config", "Location of docker client config files").String()
    env = app.Flag("env", "Set environment variables").Short('e').Strings()
    forceSequential = app.Flag("force-sequential", "Don't run anything concurrently").Short('S').Default("False").Bool()
    - graphviz = app.Flag("graphviz", "Output a graphviz diagram of the config file").Short('g').Hidden().Default("False").Bool()
    keep = app.Flag("keep", "Keep the workspace volume").Short('k').Hidden().Default("False").Bool()
    - listEnvironment = app.Flag("list-environment", "List the environment variables that are available").Short('E').Hidden().Default("false").Bool()
    - listMetaPlans = app.Flag("list-meta-plans", "List the meta plans that are available").Short('M').Hidden().Default("false").Bool()
    - listPlans = app.Flag("list-plans", "List the plans that are available").Short('P').Hidden().Default("false").Bool()
    - listTasks = app.Flag("list-tasks", "List the supported tasks").Short('L').Hidden().Default("false").Bool()
    memory = app.Flag("memory", "The amount of memory to give the run task").Short('m').String()
    - showConfig = app.Flag("show-config", "Show a dump of the config file").Short('C').Hidden().Default("false").Bool()
    sshAgent = app.Flag("ssh-agent", "A shortcut for --ssh-identity=*").Default("false").Bool()
    sshIdentities = app.Flag("ssh-identity", "Enable ssh-agent for the given identities").Strings()
    planTimeout = app.Flag("timeout", "The maximum amount of time a plan can run. 0 to disable. Units must be specified.").Default("15m").Duration()
    verbose = app.Flag("verbose", "Be more verbose").Short('v').Default("False").Bool()
    - disableDeprecated = app.Flag("disable-deprecated", "Allow the user of deprecated features").Default("False").Bool()
    + disableDeprecated = app.Flag("disable-deprecated", "Allow the use of deprecated features").Default("False").Bool()
    +
    + // deprecated options
    + graphviz = app.Flag("graphviz", "Output a graphviz diagram of the config file").Short('g').Hidden().Default("False").Bool()
    + listEnvironment = app.Flag("list-environment", "List the environment variables that are available").Short('E').Hidden().Default("false").Bool()
    + listMetaPlans = app.Flag("list-meta-plans", "List the meta plans that are available").Short('M').Hidden().Default("false").Bool()
    + listPlans = app.Flag("list-plans", "List the plans that are available").Short('P').Hidden().Default("false").Bool()
    + listTasks = app.Flag("list-tasks", "List the supported tasks").Short('L').Hidden().Default("false").Bool()
    + showConfig = app.Flag("show-config", "Show a dump of the config file").Short('C').Hidden().Default("false").Bool()
    // simple commands
    _ = app.Command("config", "Show a dump of the config file")
    @@ -82,6 +84,118 @@
    _ = ls.Command("tasks", "List the tasks defined in the configuration file.")
    )
    +// determineLoader will return the config loader to use.
    +func determineLoader() config.Loader {
    + switch *configLoader {
    + case "bitbucket":
    + return &bitbucket.Loader{}
    + default:
    + return &convey.Loader{}
    + }
    +}
    +
    +// determineRunner will return the runner that handles the given command.
    +func determineRunner(command string) runners.Runner {
    + switch command {
    + case "graphviz":
    + return &runners.Graphviz{}
    + case "list environment":
    + return &runners.ListEnvironment{}
    + case "list metaplans":
    + return &runners.ListMetaPlans{}
    + case "list plans":
    + return &runners.ListPlans{}
    + case "list tasks":
    + return &runners.ListTasks{}
    + case "config":
    + return &runners.ShowConfig{}
    + default:
    + return &runners.Default{}
    + }
    +}
    +
    +// handleDeprecatedCommands will check deprecated command line flags and return
    +// the current command that they should run.
    +func handleDeprecatedCommands() string {
    + if *disableDeprecated {
    + return ""
    + }
    +
    + command := ""
    +
    + if *graphviz {
    + fmt.Printf("--graphviz is deprecated, use 'convey graphviz` instead")
    + command = "graphviz"
    + } else if *listEnvironment {
    + fmt.Printf("--list-environment is deprecated, use 'convey list environment` instead")
    + command = "list environment"
    + } else if *listMetaPlans {
    + fmt.Printf("--list-metaplans is deprecated, use `convey list metaplans` instead")
    + command = "list metaplans"
    + } else if *listPlans {
    + fmt.Printf("--list-plans is deprecated, use `convey list plans` instead")
    + command = "list plans"
    + } else if *listTasks {
    + fmt.Printf("--list-tasks is deprecated, use `convey list tasks` instead")
    + command = "list tasks"
    + } else if *showConfig {
    + fmt.Printf("--list-plans is deprecated, use `convey config` instead")
    + command = "config"
    + }
    +
    + return command
    +}
    +
    +// resolvePlans will run through all of the plans in the config and resolve
    +// their names via the loader.
    +func resolvePlans(cfg *config.Config, loader config.Loader, st *state.State) []string {
    + realPlans := []string{}
    +
    + for _, planName := range *planNames {
    + if metaPlan, found := cfg.MetaPlans[planName]; found {
    + realPlans = append(realPlans, metaPlan.Plans...)
    + } else {
    + realPlans = append(realPlans, loader.ResolvePlanName(planName, cfg, st))
    + }
    + }
    +
    + return realPlans
    +}
    +
    +// loadConfig will load the config and it's path or an error.
    +func loadConfig(loader config.Loader) (*config.Config, string, error) {
    + // if a config file was not provided search for the loader's default file
    + if *configFile == "" {
    + for _, filename := range loader.Filenames() {
    + if _, err := os.Stat(filename); os.IsNotExist(err) {
    + continue
    + }
    +
    + *configFile = filename
    + break
    + }
    + }
    +
    + // now make sure we found a config file
    + if *configFile == "" {
    + err := fmt.Errorf("config file not found, looking for %s\n", strings.Join(loader.Filenames(), ","))
    + return nil, "", err
    + }
    +
    + // figure out the path to the config file
    + cfgPath, err := filepath.Abs(filepath.Dir(*configFile))
    + if err != nil {
    + return nil, "", err
    + }
    +
    + cfg, err := config.LoadFile(*configFile, loader, *disableDeprecated)
    + if err != nil {
    + return nil, "", err
    + }
    +
    + return cfg, cfgPath, nil
    +}
    +
    func gomain() int {
    args := os.Args[1:]
    @@ -99,40 +213,9 @@
    defer gomol.ShutdownLoggers()
    // now load the config
    - var loader config.Loader
    - switch *configLoader {
    - case "convey":
    - loader = &convey.Loader{}
    - case "bitbucket":
    - loader = &bitbucket.Loader{}
    - }
    -
    - // if a config file was not provided search for the loader's default file
    - if *configFile == "" {
    - for _, filename := range loader.Filenames() {
    - if _, err := os.Stat(filename); os.IsNotExist(err) {
    - continue
    - }
    + loader := determineLoader()
    - *configFile = filename
    - break
    - }
    - }
    -
    - // now make sure we found a config file
    - if *configFile == "" {
    - fmt.Printf("config file not found, looking for %s\n", strings.Join(loader.Filenames(), ","))
    - return 1
    - }
    -
    - // figure out the path to the config file
    - cfgPath, err := filepath.Abs(filepath.Dir(*configFile))
    - if err != nil {
    - fmt.Printf("%#v\n", err)
    - return 1
    - }
    -
    - cfg, err := config.LoadFile(*configFile, loader, *disableDeprecated)
    + cfg, cfgPath, err := loadConfig(loader)
    if err != nil {
    fmt.Printf("%s\n", err)
    return 1
    @@ -182,69 +265,18 @@
    return 1
    }
    - if *graphviz {
    - if *disableDeprecated {
    - fmt.Printf("--graphviz is deprecated, use 'convey graphviz` instead")
    - command = "graphviz"
    - }
    - } else if *listEnvironment {
    - if *disableDeprecated {
    - fmt.Printf("--list-environment is deprecated, use 'convey list environment` instead")
    - command = "list environment"
    - }
    - } else if *listMetaPlans {
    - if *disableDeprecated {
    - fmt.Printf("--list-metaplans is deprecated, use `convey list metaplans` instead")
    - command = "list metaplans"
    - }
    - } else if *listPlans {
    - if *disableDeprecated {
    - fmt.Printf("--list-plans is deprecated, use `convey list plans` instead")
    - command = "list plans"
    - }
    - } else if *listTasks {
    - if *disableDeprecated {
    - fmt.Printf("--list-tasks is deprecated, use `convey list tasks` instead")
    - command = "list tasks"
    - }
    - } else if *showConfig {
    - if *disableDeprecated {
    - fmt.Printf("--list-plans is deprecated, use `convey config` instead")
    - command = "config"
    - }
    + if commandOverride := handleDeprecatedCommands(); commandOverride != "" {
    + command = commandOverride
    }
    - var runner runners.Runner
    - switch command {
    - case "graphviz":
    - runner = &runners.Graphviz{}
    - case "list environment":
    - runner = &runners.ListEnvironment{}
    - case "list metaplans":
    - runner = &runners.ListMetaPlans{}
    - case "list plans":
    - runner = &runners.ListPlans{}
    - case "list tasks":
    - runner = &runners.ListTasks{}
    - case "config":
    - runner = &runners.ShowConfig{}
    - default:
    - runner = &runners.Default{}
    - }
    + runner := determineRunner(command)
    if len(*planNames) == 0 {
    *planNames = []string{loader.DefaultPlan()}
    }
    // resolve the plan name with the load and options
    - realPlans := []string{}
    - for _, planName := range *planNames {
    - if metaPlan, found := cfg.MetaPlans[planName]; found {
    - realPlans = append(realPlans, metaPlan.Plans...)
    - } else {
    - realPlans = append(realPlans, loader.ResolvePlanName(planName, cfg, st))
    - }
    - }
    + realPlans := resolvePlans(cfg, loader, st)
    return runner.Run(cfg, realPlans, *env, st)
    }