From 7a0482b794519f00c0e5d7c1f0f5b21dbc01eb4e Mon Sep 17 00:00:00 2001 From: Grzegorz Antoniak Date: Fri, 2 Oct 2020 10:32:52 +0200 Subject: [PATCH] Add support for `--config` option. The --config option takes a string argument that is a configuration file path. The syntax of the configuration is defined by the go-ini package, and it's a simple INI-like file where a sequence or key=value pairs are specified, each pair in a new line. Example of a configuration file: username=your.username password=your.password url=https://confluence.yourcompany.com Using a configuration file makes the tool more convenient to use in continuous integration systems, as well as ensures that the provided password will not be stored in any of shell's command history log files. Command line options can still be used to override whatever is specified inside the configuration file. --- main.go | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 5eedd3f..97e7bc4 100644 --- a/main.go +++ b/main.go @@ -2,18 +2,47 @@ package main import ( "flag" + "github.com/go-ini/ini" + "log" "github.com/philproctor/confluence-cli/client" "github.com/philproctor/confluence-cli/command" ) +func parseIniConfig(iniFilePath string, config *client.ConfluenceConfig) error { + var cfg, err = ini.Load(iniFilePath) + if err != nil { + return err + } + + config.Username = cfg.Section("").Key("username").String() + config.Password = cfg.Section("").Key("password").String() + config.URL = cfg.Section("").Key("url").String() + + var debug, debugErr = cfg.Section("").Key("debug").Bool() + if debugErr != nil { + return err + } + + config.Debug = debug + return nil +} + func main() { - var config = client.ConfluenceConfig{} var options = command.OperationOptions{} + var config = client.ConfluenceConfig{} + var configFilePath string + + var username string + var password string + var url string - flag.StringVar(&config.Username, "u", "", "Confluence username") - flag.StringVar(&config.Password, "p", "", "Confluence password") - flag.StringVar(&config.URL, "s", "", "The base URL of the Confluence page") + flag.StringVar(&username, "u", "", "Confluence username") + flag.StringVar(&password, "p", "", "Confluence password") + flag.StringVar(&url, "s", "", "The base URL of the Confluence page") + flag.StringVar(&configFilePath, "config", "", "Path to the configuration " + + "file that contains authentication credentials. Command-line parameters: " + + "`-u`, `-p` and `-s` will override settings found inside the config file.") flag.StringVar(&options.Title, "t", "", "Title to use for a new page") flag.StringVar(&options.SpaceKey, "k", "", "Space key to use") flag.StringVar(&options.Filepath, "f", "", "Path to the file to upload as the page contents") @@ -26,5 +55,16 @@ func main() { flag.BoolVar(&options.CleanAdoc, "clean-adoc", false, "If the attached HTML was generated by AsciiDoc, attempt to normalize for Confluence") flag.Parse() + if configFilePath != "" { + var err = parseIniConfig(configFilePath, &config) + if err != nil { + log.Fatal(err) + } + } + + if username != "" { config.Username = username } + if password != "" { config.Password = password } + if url != "" { config.URL = url } + command.Run(flag.Arg(0), &config, &options) }