-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Switch amtool to kingpin #976
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6f7d047
Switch cmd/amtool to kingpin
carlpett d98a05e
Touch-ups
carlpett c4706f0
Implement long help
carlpett 3431d21
Add missing short-form of --output
carlpett e8190b9
Fix backwards compatibility for config file options
carlpett 09a2e74
Fix vendoring
carlpett 4efe840
Review fixes
carlpett 3848aae
Fix flag word order
carlpett File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,11 @@ import ( | |
"errors" | ||
"fmt" | ||
"net/http" | ||
"path" | ||
"time" | ||
|
||
"github.com/alecthomas/kingpin" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same import issue as above |
||
"github.com/prometheus/alertmanager/cli/format" | ||
"github.com/prometheus/alertmanager/config" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
// Config is the response type of alertmanager config endpoint | ||
|
@@ -43,31 +41,21 @@ type alertmanagerStatusResponse struct { | |
Error string `json:"error,omitempty"` | ||
} | ||
|
||
// alertCmd represents the alert command | ||
var configCmd = &cobra.Command{ | ||
Use: "config", | ||
Short: "View the running config", | ||
Long: `View current config | ||
// configCmd represents the config command | ||
var configCmd = app.Command("config", "View the running config").Action(queryConfig) | ||
|
||
func init() { | ||
longHelpText["config"] = `View current config | ||
The amount of output is controlled by the output selection flag: | ||
- Simple: Print just the running config | ||
- Extended: Print the running config as well as uptime and all version info | ||
- Json: Print entire config object as json`, | ||
RunE: queryConfig, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(configCmd) | ||
- Json: Print entire config object as json` | ||
} | ||
|
||
func fetchConfig() (Config, error) { | ||
configResponse := alertmanagerStatusResponse{} | ||
u, err := GetAlertmanagerURL() | ||
if err != nil { | ||
return Config{}, err | ||
} | ||
|
||
u.Path = path.Join(u.Path, "/api/v1/status") | ||
u := GetAlertmanagerURL("/api/v1/status") | ||
res, err := http.Get(u.String()) | ||
if err != nil { | ||
return Config{}, err | ||
|
@@ -87,13 +75,13 @@ func fetchConfig() (Config, error) { | |
return configResponse.Data, nil | ||
} | ||
|
||
func queryConfig(cmd *cobra.Command, args []string) error { | ||
func queryConfig(element *kingpin.ParseElement, ctx *kingpin.ParseContext) error { | ||
config, err := fetchConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
formatter, found := format.Formatters[viper.GetString("output")] | ||
formatter, found := format.Formatters[*output] | ||
if !found { | ||
return errors.New("unknown output formatter") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,22 @@ import ( | |
"io" | ||
"time" | ||
|
||
"github.com/alecthomas/kingpin" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import |
||
"github.com/prometheus/alertmanager/config" | ||
"github.com/prometheus/alertmanager/dispatch" | ||
"github.com/prometheus/alertmanager/types" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const DefaultDateFormat = "2006-01-02 15:04:05 MST" | ||
|
||
var ( | ||
dateFormat *string | ||
) | ||
|
||
func InitFormatFlags(app *kingpin.Application) { | ||
dateFormat = app.Flag("date.format", "Format of date output").Default(DefaultDateFormat).String() | ||
} | ||
|
||
// Config representation | ||
// Need to get this moved to the prometheus/common/model repo having is duplicated here is smelly | ||
type Config struct { | ||
|
@@ -46,6 +54,5 @@ type Formatter interface { | |
var Formatters = map[string]Formatter{} | ||
|
||
func FormatDate(input time.Time) string { | ||
dateformat := viper.GetString("date.format") | ||
return input.Format(dateformat) | ||
return input.Format(*dateFormat) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use
gopkg.in/alecthomas/kingpin.v2
, like in prometheus (and as recommended in the repo)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, no :) We need to use the v3-branch, since the pull request I made to kingpin to add support for config files is only present on that branch.