Skip to content

Commit

Permalink
Merge pull request #582 from mackerelio/read-from-file-for-annotation…
Browse files Browse the repository at this point in the history
…-description

read annotation's description from file or stdin
  • Loading branch information
Arthur1 committed Jul 21, 2023
2 parents 348eb86 + a05fef2 commit 289b472
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions annotations/command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package annotations

import (
"io"
"os"

"github.com/mackerelio/mackerel-client-go"
Expand All @@ -22,14 +23,15 @@ var Command = cli.Command{
{
Name: "create",
Usage: "create a graph annotation",
ArgsUsage: "--title <title> [--description <descriptio>] --from <from> --to <to> --service|-s <service> [--role|-r <role>]",
ArgsUsage: "--title <title> [--description <description>] [--description-file <file-path>] --from <from> --to <to> --service|-s <service> [--role|-r <role>]",
Description: `
Creates a graph annotation.
`,
Action: doAnnotationsCreate,
Flags: []cli.Flag{
cli.StringFlag{Name: "title", Usage: "Title for annotation"},
cli.StringFlag{Name: "description", Usage: "Description for annotation"},
cli.StringFlag{Name: "description-file", Usage: `Read description text for annotation from file (use "-" to read from stdin)`},
cli.IntFlag{Name: "from", Usage: "Starting time (epoch seconds)"},
cli.IntFlag{Name: "to", Usage: "Ending time (epoch seconds)"},
cli.StringFlag{Name: "service, s", Usage: "Service name for annotation"},
Expand Down Expand Up @@ -58,7 +60,7 @@ var Command = cli.Command{
{
Name: "update",
Usage: "update annotation",
ArgsUsage: "--id <id> [--title <title>] [--description <descriptio>] --from <from> --to <to> --service|-s <service> [--role|-r <role>]",
ArgsUsage: "--id <id> [--title <title>] [--description <description>] [--description-file <file-path>] --from <from> --to <to> --service|-s <service> [--role|-r <role>]",
Description: `
Updates an annotation
`,
Expand All @@ -68,6 +70,7 @@ var Command = cli.Command{
cli.StringFlag{Name: "service, s", Usage: "Service name for annotation"},
cli.StringFlag{Name: "title", Usage: "Title for annotation"},
cli.StringFlag{Name: "description", Usage: "Description for annotation"},
cli.StringFlag{Name: "description-file", Usage: `Read description text for annotation from file (use "-" to read from stdin)`},
cli.IntFlag{Name: "from", Usage: "Starting time (epoch seconds)"},
cli.IntFlag{Name: "to", Usage: "Ending time (epoch seconds)"},
cli.StringSliceFlag{
Expand Down Expand Up @@ -95,6 +98,7 @@ var Command = cli.Command{
func doAnnotationsCreate(c *cli.Context) error {
title := c.String("title")
description := c.String("description")
descriptionFile := c.String("description-file")
from := c.Int64("from")
to := c.Int64("to")
service := c.String("service")
Expand All @@ -120,6 +124,25 @@ func doAnnotationsCreate(c *cli.Context) error {
return cli.NewExitError("`to` is a required field to create a graph annotation.", 1)
}

if description != "" && descriptionFile != "" {
_ = cli.ShowCommandHelp(c, "create")
return cli.NewExitError("specify one of `description` or `description-file`.", 1)
}

if descriptionFile != "" {
var (
b []byte
err error
)
if descriptionFile == "-" {
b, err = io.ReadAll(os.Stdin)
} else {
b, err = os.ReadFile(descriptionFile)
}
logger.DieIf(err)
description = string(b)
}

client := mackerelclient.NewFromContext(c)
annotation, err := client.CreateGraphAnnotation(&mackerel.GraphAnnotation{
Title: title,
Expand Down Expand Up @@ -167,6 +190,7 @@ func doAnnotationsUpdate(c *cli.Context) error {
annotationID := c.String("id")
title := c.String("title")
description := c.String("description")
descriptionFile := c.String("description-file")
from := c.Int64("from")
to := c.Int64("to")
service := c.String("service")
Expand All @@ -192,6 +216,25 @@ func doAnnotationsUpdate(c *cli.Context) error {
return cli.NewExitError("`to` is a required field to update a graph annotation.", 1)
}

if description != "" && descriptionFile != "" {
_ = cli.ShowCommandHelp(c, "create")
return cli.NewExitError("specify one of `description` or `description-file`.", 1)
}

if descriptionFile != "" {
var (
b []byte
err error
)
if descriptionFile == "-" {
b, err = io.ReadAll(os.Stdin)
} else {
b, err = os.ReadFile(descriptionFile)
}
logger.DieIf(err)
description = string(b)
}

client := mackerelclient.NewFromContext(c)
annotation, err := client.UpdateGraphAnnotation(annotationID, &mackerel.GraphAnnotation{
Title: title,
Expand Down

0 comments on commit 289b472

Please sign in to comment.