diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..fce3962 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,25 @@ +name: publish docker image +on: + push: + tags: + - "*" + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + # Allow goreleaser to access older tag information. + fetch-depth: 0 + - uses: actions/setup-go@v4 + with: + go-version-file: 'go.mod' + cache: true + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v5 + with: + args: release --rm-dist + env: + # GitHub sets the GITHUB_TOKEN secret automatically. + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/cmd/healthcheck_dns.go b/cmd/healthcheck_dns.go index 286b4e2..5e55f95 100644 --- a/cmd/healthcheck_dns.go +++ b/cmd/healthcheck_dns.go @@ -61,7 +61,7 @@ func createDNSHealthcheckCmd() *cobra.Command { createDNSHealthcheck.PersistentFlags().StringVar(&description, "description", "", "healthcheck description") - createDNSHealthcheck.PersistentFlags().StringSliceVar(&labels, "labels", []string{}, "healthchecks labels (example: foo=bar)") + createDNSHealthcheck.PersistentFlags().StringSliceVar(&labels, "labels", []string{}, "healthcheck labels (example: foo=bar)") createDNSHealthcheck.PersistentFlags().StringVar(&interval, "interval", "60s", "healthcheck interval (examples: 30s, 3m)") diff --git a/cmd/pushgateway.go b/cmd/pushgateway.go new file mode 100644 index 0000000..bf823f5 --- /dev/null +++ b/cmd/pushgateway.go @@ -0,0 +1,175 @@ +package cmd + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "os" + + goclient "github.com/appclacks/go-client" + "github.com/cheynewallace/tabby" + "github.com/spf13/cobra" +) + +func createPushgatewayMetricCmd() *cobra.Command { + var name string + var description string + var labels []string + var ttl string + var metricType string + var value float32 + + var createPushgatewayMetric = &cobra.Command{ + Use: "create", + Short: "Create (or update) a new metric in the push gateway. ", + Run: func(cmd *cobra.Command, args []string) { + client := buildClient() + labelsMap, err := toMap(labels) + exitIfError(err) + + payload := goclient.CreateOrUpdatePushgatewayMetricInput{ + Name: name, + Description: description, + Labels: labelsMap, + TTL: ttl, + Type: metricType, + Value: value, + } + ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) + defer cancel() + result, err := client.CreateOrUpdatePushgatewayMetric(ctx, payload) + exitIfError(err) + if outputFormat == "json" { + json, err := json.Marshal(result) + exitIfError(err) + fmt.Println(string(json)) + os.Exit(0) + } + t := tabby.New() + t.AddHeader("Messages") + for _, message := range result.Messages { + t.AddLine(message) + } + t.Print() + os.Exit(0) + }, + } + createPushgatewayMetric.PersistentFlags().StringVar(&name, "name", "", "metric name") + err := createPushgatewayMetric.MarkPersistentFlagRequired("name") + exitIfError(err) + + createPushgatewayMetric.PersistentFlags().StringVar(&description, "description", "", "metric description") + + createPushgatewayMetric.PersistentFlags().StringSliceVar(&labels, "labels", []string{}, "metric labels (example: foo=bar)") + + createPushgatewayMetric.PersistentFlags().StringVar(&ttl, "ttl", "", "metric timeout") + + createPushgatewayMetric.PersistentFlags().StringVar(&metricType, "type", "", "metric type") + + createPushgatewayMetric.PersistentFlags().Float32Var(&value, "value", 0, "metric value") + err = createPushgatewayMetric.MarkPersistentFlagRequired("value") + exitIfError(err) + + return createPushgatewayMetric +} + +func listPushgatewayMetricsCmd() *cobra.Command { + var listPushgatewayMetrics = &cobra.Command{ + Use: "list", + Short: "List push gateway metrics", + Run: func(cmd *cobra.Command, args []string) { + client := buildClient() + ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) + defer cancel() + result, err := client.ListPushgatewayMetrics(ctx) + exitIfError(err) + if outputFormat == "json" { + json, err := json.Marshal(result) + exitIfError(err) + fmt.Println(string(json)) + os.Exit(0) + } + t := tabby.New() + t.AddHeader("ID", "Name", "Description", "Type", "Labels", "TTL", "Created At", "Expires At", "Value") + for _, metric := range result.Result { + jsonLabels, err := json.Marshal(metric.Labels) + exitIfError(err) + t.AddLine(metric.ID, metric.Name, metric.Description, metric.Type, string(jsonLabels), metric.TTL, metric.CreatedAt, metric.ExpiresAt, metric.Value) + } + t.Print() + os.Exit(0) + }, + } + return listPushgatewayMetrics +} + +func deletePushgatewayMetricCmd() *cobra.Command { + var metricID string + var metricName string + var deleteMetric = &cobra.Command{ + Use: "delete", + Short: "Delete a push gateway metric by name or by ID", + Run: func(cmd *cobra.Command, args []string) { + identifier := metricID + if identifier == "" { + identifier = metricName + } + if identifier == "" { + exitIfError(errors.New("you should pass the metric name or ID (using --name or --id) you want to delete")) + } + input := goclient.DeletePushgatewayMetricInput{ + Identifier: identifier, + } + client := buildClient() + ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) + defer cancel() + result, err := client.DeletePushgatewayMetric(ctx, input) + exitIfError(err) + if outputFormat == "json" { + json, err := json.Marshal(result) + exitIfError(err) + fmt.Println(string(json)) + os.Exit(0) + } + t := tabby.New() + t.AddHeader("Messages") + for _, message := range result.Messages { + t.AddLine(message) + } + t.Print() + os.Exit(0) + }, + } + deleteMetric.PersistentFlags().StringVar(&metricID, "id", "", "Metric ID") + deleteMetric.PersistentFlags().StringVar(&metricName, "name", "", "Metric Name") + return deleteMetric +} + +func deleteAllPushgatewayMetricsCmd() *cobra.Command { + var deleteMetric = &cobra.Command{ + Use: "delete-all", + Short: "Delete all push gateway metrics", + Run: func(cmd *cobra.Command, args []string) { + client := buildClient() + ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout) + defer cancel() + result, err := client.DeleteAllPushgatewayMetrics(ctx) + exitIfError(err) + if outputFormat == "json" { + json, err := json.Marshal(result) + exitIfError(err) + fmt.Println(string(json)) + os.Exit(0) + } + t := tabby.New() + t.AddHeader("Messages") + for _, message := range result.Messages { + t.AddLine(message) + } + t.Print() + os.Exit(0) + }, + } + return deleteMetric +} diff --git a/cmd/root.go b/cmd/root.go index b407796..73c4e35 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -59,6 +59,15 @@ func Execute() error { command.AddCommand(createCommandHealthcheckCmd()) command.AddCommand(updateCommandHealthcheckCmd()) + var pushgateway = &cobra.Command{ + Use: "pushgateway", + Short: "Manage pushgateway metrics", + } + pushgateway.AddCommand(createPushgatewayMetricCmd()) + pushgateway.AddCommand(listPushgatewayMetricsCmd()) + pushgateway.AddCommand(deletePushgatewayMetricCmd()) + pushgateway.AddCommand(deleteAllPushgatewayMetricsCmd()) + healthcheck.AddCommand(dns) healthcheck.AddCommand(command) healthcheck.AddCommand(tls) @@ -66,6 +75,7 @@ func Execute() error { healthcheck.AddCommand(http) rootCmd.AddCommand(healthcheck) + rootCmd.AddCommand(pushgateway) return rootCmd.Execute() } diff --git a/go.mod b/go.mod index abb5981..8bfa5f0 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/appclacks/cli go 1.22.0 require ( - github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7 + github.com/appclacks/go-client v0.0.0-20240704120510-358af014f040 github.com/cheynewallace/tabby v1.1.1 github.com/spf13/cobra v1.8.0 ) diff --git a/go.sum b/go.sum index 10dadd1..151dc04 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,18 @@ github.com/appclacks/go-client v0.0.0-20240602132011-291c0d8ca225 h1:Bi2DlXudM58 github.com/appclacks/go-client v0.0.0-20240602132011-291c0d8ca225/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU= github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7 h1:kTNVmMjdtr/X0ljUCyDCXUzDYnP3JL5fdDMwBSUveOg= github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU= +github.com/appclacks/go-client v0.0.0-20240618204225-51a556fa72ba h1://S8sLRrM/2eJLfYXcFOpsP7eDGwKDrHj0rqlXj6uSo= +github.com/appclacks/go-client v0.0.0-20240618204225-51a556fa72ba/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU= +github.com/appclacks/go-client v0.0.0-20240703201525-421246966bba h1:alp3NoMSmJ9bAw6Ubo02F3z4aBVM5HIkXplKOuaj4mc= +github.com/appclacks/go-client v0.0.0-20240703201525-421246966bba/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU= +github.com/appclacks/go-client v0.0.0-20240703203820-f7d948812083 h1:ESPN+6uiockjKr2XnTRQBkleMQsT5PH9S/CjVUIUdqg= +github.com/appclacks/go-client v0.0.0-20240703203820-f7d948812083/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU= +github.com/appclacks/go-client v0.0.0-20240703204325-c8a3257d9e08 h1:vjf4pQGIx8BCj1+wEsNBR04lyVzje74O7ozJQn6JO3w= +github.com/appclacks/go-client v0.0.0-20240703204325-c8a3257d9e08/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU= +github.com/appclacks/go-client v0.0.0-20240703205617-8b0425724375 h1:eVJ+l4qRByZ2L9O4xl2HC009T7TMw2z5EPnf+ArUH3A= +github.com/appclacks/go-client v0.0.0-20240703205617-8b0425724375/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU= +github.com/appclacks/go-client v0.0.0-20240704120510-358af014f040 h1:1Y8zxzWnIJJTN35I1m19Pf3Li7eRLQHAoAjjAta5ck4= +github.com/appclacks/go-client v0.0.0-20240704120510-358af014f040/go.mod h1:ZOQEaU5H5BTLZr326dMjvVbB+pdkeRX3emyu4cEq9GU= github.com/cheynewallace/tabby v1.1.1 h1:JvUR8waht4Y0S3JF17G6Vhyt+FRhnqVCkk8l4YrOU54= github.com/cheynewallace/tabby v1.1.1/go.mod h1:Pba/6cUL8uYqvOc9RkyvFbHGrQ9wShyrn6/S/1OYVys= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= diff --git a/vendor/github.com/appclacks/go-client/healthcheck_command.go b/vendor/github.com/appclacks/go-client/healthcheck_command.go index 5ed75ce..85e0215 100644 --- a/vendor/github.com/appclacks/go-client/healthcheck_command.go +++ b/vendor/github.com/appclacks/go-client/healthcheck_command.go @@ -8,7 +8,7 @@ import ( type HealthcheckCommandDefinition struct { Command string `json:"command" validate:"required,max=512,min=1"` - Arguments []string `json:"arguments"` + Arguments []string `json:"arguments,omitempty"` } type CreateCommandHealthcheckInput struct { diff --git a/vendor/github.com/appclacks/go-client/pushgateway.go b/vendor/github.com/appclacks/go-client/pushgateway.go new file mode 100644 index 0000000..15ed98d --- /dev/null +++ b/vendor/github.com/appclacks/go-client/pushgateway.go @@ -0,0 +1,73 @@ +package client + +import ( + "context" + "fmt" + "net/http" + "time" +) + +type PushgatewayMetric struct { + ID string `json:"id"` + Name string `json:"name"` + Description string `json:"description,omitempty"` + Labels map[string]string `json:"labels,omitempty"` + TTL string `json:"ttl"` + Type string `json:"type"` + CreatedAt time.Time `json:"created_at"` + ExpiresAt *time.Time `json:"expires_at,omitempty"` + Value float32 `json:"value"` +} + +type CreateOrUpdatePushgatewayMetricInput struct { + Name string `json:"name" validate:"required,max=255,min=1"` + Description string `json:"description,omitempty"` + Labels map[string]string `json:"labels" description:"Healthcheck labels" validate:"dive,keys,max=255,min=1,endkeys,max=255,min=1"` + TTL string `json:"ttl"` + Type string `json:"type" validate:"omitempty,oneof=counter gauge histogram summary"` + Value float32 `json:"value" validate:"required"` +} + +type DeletePushgatewayMetricInput struct { + Identifier string `param:"identifier" validate:"required"` +} + +type ListPushgatewayMetricsOutput struct { + Result []PushgatewayMetric `json:"result"` +} + +func (c *Client) CreateOrUpdatePushgatewayMetric(ctx context.Context, input CreateOrUpdatePushgatewayMetricInput) (Response, error) { + var result Response + _, err := c.sendRequest(ctx, "/api/v1/pushgateway", http.MethodPost, input, &result, nil) + if err != nil { + return Response{}, err + } + return result, nil +} + +func (c *Client) DeletePushgatewayMetric(ctx context.Context, input DeletePushgatewayMetricInput) (Response, error) { + var result Response + _, err := c.sendRequest(ctx, fmt.Sprintf("/api/v1/pushgateway/%s", input.Identifier), http.MethodDelete, nil, &result, nil) + if err != nil { + return Response{}, err + } + return result, nil +} + +func (c *Client) ListPushgatewayMetrics(ctx context.Context) (ListPushgatewayMetricsOutput, error) { + var result ListPushgatewayMetricsOutput + _, err := c.sendRequest(ctx, "/api/v1/pushgateway", http.MethodGet, nil, &result, nil) + if err != nil { + return ListPushgatewayMetricsOutput{}, err + } + return result, nil +} + +func (c *Client) DeleteAllPushgatewayMetrics(ctx context.Context) (Response, error) { + var result Response + _, err := c.sendRequest(ctx, "/api/v1/pushgateway", http.MethodDelete, nil, &result, nil) + if err != nil { + return Response{}, err + } + return result, nil +} diff --git a/vendor/modules.txt b/vendor/modules.txt index e51d64b..9658ddf 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1,4 +1,4 @@ -# github.com/appclacks/go-client v0.0.0-20240602163908-0de41c983ab7 +# github.com/appclacks/go-client v0.0.0-20240704120510-358af014f040 ## explicit; go 1.22.0 github.com/appclacks/go-client # github.com/cheynewallace/tabby v1.1.1